Skip to content

Commit

Permalink
do something on read complete
Browse files Browse the repository at this point in the history
  • Loading branch information
andrewmurraydavid committed Oct 14, 2024
1 parent 87e8306 commit 0e319d6
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 12 deletions.
23 changes: 21 additions & 2 deletions src/components/NotificationsCenter/Notification.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,29 @@ import { ReadNotificationDocument } from './ReadNotification.graphql';

interface NotificationProps {
notification: NotificationFragment;
onReadComplete?: () => void;
}

export function Notification({ notification }: NotificationProps) {
const [markAsRead] = useMutation(ReadNotificationDocument);
export function Notification({
notification,
onReadComplete,
}: NotificationProps) {
const [markAsRead] = useMutation(ReadNotificationDocument, {
onCompleted: () => {
onReadComplete?.();
},
// update: onUpdateChangeFragment({
// id: 'notifications',
// fragment: NotificationListFragmentDoc,
// updater: (cached) => {
// return {
// ...cached,
// totalUnread: 11,
// };
// },
// }),
});

const { id, unread, content, createdAt } = notification;

return (
Expand Down
14 changes: 9 additions & 5 deletions src/components/NotificationsCenter/NotificationList.graphql
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
query NotificationList($input: NotificationListInput) {
notifications(input: $input) {
...Pagination
totalUnread
items {
...notification
}
...notificationList
}
}

fragment notificationList on NotificationList {
...Pagination
totalUnread
items {
...notification
}
}
17 changes: 12 additions & 5 deletions src/components/NotificationsCenter/NotificationsCenter.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,13 @@ export const NotificationCenter = () => {
const [anchorEl, setAnchorEl] = useState<null | HTMLElement>(null);
const isPopoverOpen = Boolean(anchorEl);

const { data, loadMore, loading } = useListQuery(NotificationListDocument, {
pollInterval: 60_000,
listAt: (data) => data.notifications,
});
const { data, loadMore, loading, refetch } = useListQuery(
NotificationListDocument,
{
pollInterval: 60_000,
listAt: (data) => data.notifications,
}
);

const notifications = useMemo(() => (data ? data.items : []), [data]);

Expand Down Expand Up @@ -62,7 +65,11 @@ export const NotificationCenter = () => {
}}
>
{notifications.map((notification) => (
<Notification key={notification.id} notification={notification} />
<Notification
key={notification.id}
notification={notification}
onReadComplete={() => void refetch()}
/>
))}
{data?.hasMore && (
<ProgressButton progress={loading} onClick={() => loadMore()}>
Expand Down

0 comments on commit 0e319d6

Please sign in to comment.