Skip to content

Commit

Permalink
Merge pull request #107 from kthcloud/beta
Browse files Browse the repository at this point in the history
Fix notification badge
  • Loading branch information
pierrelefevre authored Nov 22, 2023
2 parents 58919d4 + f7215d4 commit 27e011a
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 10 deletions.
12 changes: 12 additions & 0 deletions src/contexts/ResourceContext.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ export const ResourceContextProvider = ({ children }) => {
const [jobs, setJobs] = useState([]);
const [user, setUser] = useState(null);
const [notifications, setNotifications] = useState([]);
const [unread, setUnread] = useState(0);
const [teams, setTeams] = useState([]);
const [zones, setZones] = useState([]);
const [initialLoad, setInitialLoad] = useState(false);
Expand Down Expand Up @@ -127,6 +128,15 @@ export const ResourceContextProvider = ({ children }) => {
try {
const notifications = await getNotifications(keycloak.token);
setNotifications(notifications);

let u = 0;
notifications.forEach((n) => {
if (!n.readAt) {
u++;
}
});

setUnread(u);
} catch (error) {
errorHandler(error).forEach((e) =>
enqueueSnackbar("Error fetching notifications: " + e, {
Expand Down Expand Up @@ -245,6 +255,8 @@ export const ResourceContextProvider = ({ children }) => {
setUser,
notifications,
setNotifications,
unread,
setUnread,
teams,
setTeams,
zones,
Expand Down
8 changes: 2 additions & 6 deletions src/layouts/dashboard/Menu.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ export default function Menu() {

const [open, setOpen] = useState(null);

const { notifications } = useResource();
const { unread } = useResource();

const handleOpen = (event) => {
setOpen(event.currentTarget);
Expand Down Expand Up @@ -129,11 +129,7 @@ export default function Menu() {
component={RouterLink}
onClick={handleClose}
>
{t("inbox") +
" " +
(notifications.length > 0
? "(" + notifications.length + ")"
: "")}
{t("inbox") + " " + (unread > 0 ? "(" + unread + ")" : "")}
</MenuItem>
<MenuItem
to={"/teams"}
Expand Down
8 changes: 4 additions & 4 deletions src/layouts/dashboard/ProfileButton.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import { Link } from "react-router-dom";
import { useTranslation } from "react-i18next";

const ProfileButton = () => {
const { user, notifications } = useResource();
const { user, unread } = useResource();
const [userAvatar, setUserAvatar] = useState(null);
const [hasFetched, setHasFetched] = useState(false);
const { t } = useTranslation();
Expand Down Expand Up @@ -66,7 +66,7 @@ const ProfileButton = () => {
component={Link}
to="/inbox"
>
{`${t("inbox")} (${notifications?.length})`}
{`${t("inbox")} (${unread})`}
</Button>
<Button
startIcon={<Iconify icon="mdi:account-group" />}
Expand All @@ -80,8 +80,8 @@ const ProfileButton = () => {
}
>
<Badge
invisible={notifications?.length === 0}
badgeContent={notifications.length}
invisible={unread === 0}
badgeContent={unread}
color="primary"
sx={{
display: {
Expand Down

0 comments on commit 27e011a

Please sign in to comment.