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