feature/IO-3096-GlobalNotifications - add Dayjs, minor packages on backend

This commit is contained in:
Dave Richer
2025-02-28 11:13:08 -05:00
parent 5bd6f0453d
commit 9b871149ac
5 changed files with 251 additions and 245 deletions

View File

@@ -12,12 +12,13 @@ import {
MARK_NOTIFICATION_READ
} from "../../graphql/notifications.queries.js";
import { gql, useMutation } from "@apollo/client";
import day from "../../utils/day.js";
const SocketContext = createContext(null);
export const INITIAL_NOTIFICATIONS = 10;
const INITIAL_NOTIFICATIONS = 10;
export const SocketProvider = ({ children, bodyshop, navigate, currentUser }) => {
const SocketProvider = ({ children, bodyshop, navigate, currentUser }) => {
const socketRef = useRef(null);
const [clientId, setClientId] = useState(null);
const [isConnected, setIsConnected] = useState(false);
@@ -26,7 +27,7 @@ export const SocketProvider = ({ children, bodyshop, navigate, currentUser }) =>
const [markNotificationRead] = useMutation(MARK_NOTIFICATION_READ, {
update: (cache, { data: { update_notifications } }) => {
const timestamp = new Date().toISOString();
const timestamp = day().toISOString();
const updatedNotification = update_notifications.returning[0];
cache.modify({
@@ -79,7 +80,7 @@ export const SocketProvider = ({ children, bodyshop, navigate, currentUser }) =>
const [markAllNotificationsRead] = useMutation(MARK_ALL_NOTIFICATIONS_READ, {
variables: { associationid: userAssociationId },
update: (cache) => {
const timestamp = new Date().toISOString();
const timestamp = day().toISOString();
cache.modify({
fields: {
notifications(existing = [], { readField }) {
@@ -202,7 +203,7 @@ export const SocketProvider = ({ children, bodyshop, navigate, currentUser }) =>
scenario_text: JSON.stringify(notifications.map((notif) => notif.body)),
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(),
created_at: day(notifications[0].timestamp).toISOString(),
read: null,
job: { ro_number: jobRoNumber }
};
@@ -224,8 +225,8 @@ export const SocketProvider = ({ children, bodyshop, navigate, currentUser }) =>
query: GET_NOTIFICATIONS,
variables: baseVariables,
data: {
notifications: [newNotification, ...existingNotifications].sort(
(a, b) => new Date(b.created_at) - new Date(a.created_at)
notifications: [newNotification, ...existingNotifications].sort((a, b) =>
day(b.created_at).diff(day(a.created_at))
)
},
broadcast: true
@@ -444,11 +445,11 @@ export const SocketProvider = ({ children, bodyshop, navigate, currentUser }) =>
);
};
export const useSocket = () => {
const useSocket = () => {
const context = useContext(SocketContext);
// NOTE: Not sure if we absolutely require this, does cause slipups on dev env
if (!context) throw new Error("useSocket must be used within a SocketProvider");
return context;
};
export default SocketContext;
export { SocketContext, SocketProvider, INITIAL_NOTIFICATIONS, useSocket };