feature/IO-3096-GlobalNotifications - Checkpoint
This commit is contained in:
@@ -6,7 +6,7 @@ import { store } from "../../redux/store";
|
||||
import { addAlerts, setWssStatus } from "../../redux/application/application.actions";
|
||||
import client from "../../utils/GraphQLClient";
|
||||
import { useNotification } from "../Notifications/notificationContext.jsx";
|
||||
import { gql } from "@apollo/client";
|
||||
import { GET_NOTIFICATIONS } from "../../graphql/notifications.queries.js";
|
||||
|
||||
const SocketContext = createContext(null);
|
||||
|
||||
@@ -87,8 +87,6 @@ export const SocketProvider = ({ children, bodyshop }) => {
|
||||
const handleNotification = (data) => {
|
||||
const { jobId, jobRoNumber, notificationId, associationId, notifications } = data;
|
||||
|
||||
console.log(`Got RO ${jobRoNumber}`);
|
||||
|
||||
const newNotification = {
|
||||
__typename: "notifications",
|
||||
id: notificationId,
|
||||
@@ -98,27 +96,19 @@ export const SocketProvider = ({ children, bodyshop }) => {
|
||||
fcm_text: notifications.map((notif) => notif.body).join(". ") + ".",
|
||||
scenario_meta: JSON.stringify(notifications.map((notif) => notif.variables || {})),
|
||||
created_at: new Date(notifications[0].timestamp).toISOString(),
|
||||
read: null
|
||||
read: null,
|
||||
job: {
|
||||
ro_number: jobRoNumber
|
||||
}
|
||||
};
|
||||
|
||||
const baseVariables = { limit: 20, offset: 0, where: {} };
|
||||
|
||||
try {
|
||||
const existingNotifications =
|
||||
client.cache.readQuery({
|
||||
query: gql`
|
||||
query GetNotifications {
|
||||
notifications(order_by: { created_at: desc }) {
|
||||
__typename
|
||||
id
|
||||
jobid
|
||||
associationid
|
||||
scenario_text
|
||||
fcm_text
|
||||
scenario_meta
|
||||
created_at
|
||||
read
|
||||
}
|
||||
}
|
||||
`
|
||||
query: GET_NOTIFICATIONS,
|
||||
variables: baseVariables
|
||||
})?.notifications || [];
|
||||
|
||||
if (existingNotifications.some((n) => n.id === newNotification.id)) {
|
||||
@@ -126,21 +116,8 @@ export const SocketProvider = ({ children, bodyshop }) => {
|
||||
}
|
||||
|
||||
client.cache.writeQuery({
|
||||
query: gql`
|
||||
query GetNotifications {
|
||||
notifications(order_by: { created_at: desc }) {
|
||||
__typename
|
||||
id
|
||||
jobid
|
||||
associationid
|
||||
scenario_text
|
||||
fcm_text
|
||||
scenario_meta
|
||||
created_at
|
||||
read
|
||||
}
|
||||
}
|
||||
`,
|
||||
query: GET_NOTIFICATIONS,
|
||||
variables: baseVariables,
|
||||
data: {
|
||||
notifications: [newNotification, ...existingNotifications].sort(
|
||||
(a, b) => new Date(b.created_at) - new Date(a.created_at)
|
||||
@@ -149,6 +126,27 @@ export const SocketProvider = ({ children, bodyshop }) => {
|
||||
broadcast: true
|
||||
});
|
||||
|
||||
// Handle showUnreadOnly case
|
||||
const unreadVariables = { ...baseVariables, where: { read: { _is_null: true } } };
|
||||
const unreadNotifications =
|
||||
client.cache.readQuery({
|
||||
query: GET_NOTIFICATIONS,
|
||||
variables: unreadVariables
|
||||
})?.notifications || [];
|
||||
|
||||
if (newNotification.read === null && !unreadNotifications.some((n) => n.id === newNotification.id)) {
|
||||
client.cache.writeQuery({
|
||||
query: GET_NOTIFICATIONS,
|
||||
variables: unreadVariables,
|
||||
data: {
|
||||
notifications: [newNotification, ...unreadNotifications].sort(
|
||||
(a, b) => new Date(b.created_at) - new Date(a.created_at)
|
||||
)
|
||||
},
|
||||
broadcast: true
|
||||
});
|
||||
}
|
||||
|
||||
client.cache.modify({
|
||||
id: "ROOT_QUERY",
|
||||
fields: {
|
||||
|
||||
Reference in New Issue
Block a user