-
Notifications
You must be signed in to change notification settings - Fork 583
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(ONYX-620): Activity List: Implement management action sheet (#9716)
* feat: add ActionSheet * chore: address review comments
- Loading branch information
Showing
4 changed files
with
138 additions
and
97 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
87 changes: 87 additions & 0 deletions
87
src/app/Scenes/Activity/hooks/useMarkAllNotificationsAsRead.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
import { captureMessage } from "@sentry/react-native" | ||
import { useMarkAllNotificationsAsReadMutation } from "__generated__/useMarkAllNotificationsAsReadMutation.graphql" | ||
import { notificationTypes } from "app/Scenes/Activity/types" | ||
import { getNotificationTypes } from "app/Scenes/Activity/utils/getNotificationTypes" | ||
import { ConnectionHandler, UseMutationConfig, graphql, useMutation } from "react-relay" | ||
|
||
export const useMarkAllNotificationsAsRead = () => { | ||
const [commit, mutationInProgress] = | ||
useMutation<useMarkAllNotificationsAsReadMutation>(MarkAllAsReadMutation) | ||
|
||
const markAllNotificationsAsRead = () => { | ||
try { | ||
commit({ | ||
variables: {}, | ||
updater: (store) => { | ||
markAllAsReadMutationUpdater(store) | ||
}, | ||
optimisticUpdater: (store) => { | ||
markAllAsReadMutationUpdater(store) | ||
}, | ||
onCompleted: (response) => { | ||
const errorMessage = | ||
response.markAllNotificationsAsRead?.responseOrError?.mutationError?.message | ||
if (errorMessage) { | ||
throw new Error(errorMessage) | ||
} | ||
}, | ||
}) | ||
} catch (e) { | ||
if (__DEV__) { | ||
console.error(e) | ||
} else { | ||
captureMessage(`ActivityMarkAllAsReadSection ${JSON.stringify(e)}`) | ||
} | ||
} | ||
} | ||
|
||
return { markAllNotificationsAsRead, mutationInProgress } | ||
} | ||
|
||
const markAllAsReadMutationUpdater = ( | ||
store: Parameters< | ||
NonNullable<UseMutationConfig<useMarkAllNotificationsAsReadMutation>["updater"]> | ||
>[0] | ||
) => { | ||
const root = store.getRoot() | ||
const me = root.getLinkedRecord("me") | ||
const viewer = root.getLinkedRecord("viewer") | ||
|
||
if (!me || !viewer) { | ||
return | ||
} | ||
|
||
notificationTypes.forEach((type) => { | ||
const key = "ActivityList_notificationsConnection" | ||
const connection = ConnectionHandler.getConnection(viewer, key, { | ||
notificationTypes: getNotificationTypes(type), | ||
}) | ||
const edges = connection?.getLinkedRecords("edges") | ||
|
||
// Set unread notifications count to 0 | ||
me.setValue(0, "unreadNotificationsCount") | ||
|
||
// Mark all notifications as read | ||
edges?.forEach((edge) => { | ||
const node = edge.getLinkedRecord("node") | ||
node?.setValue(false, "isUnread") | ||
}) | ||
}) | ||
} | ||
|
||
const MarkAllAsReadMutation = graphql` | ||
mutation useMarkAllNotificationsAsReadMutation { | ||
markAllNotificationsAsRead(input: {}) { | ||
responseOrError { | ||
... on MarkAllNotificationsAsReadSuccess { | ||
success | ||
} | ||
... on MarkAllNotificationsAsReadFailure { | ||
mutationError { | ||
message | ||
} | ||
} | ||
} | ||
} | ||
} | ||
` |