feature/IO-3291-Tasks-Notifications: Checkpoint

This commit is contained in:
Dave Richer
2025-07-10 13:18:01 -04:00
parent 79e379b61a
commit 1127864ba9
10 changed files with 749 additions and 470 deletions

View File

@@ -1,4 +1,3 @@
// SocketProvider.js
import { useEffect, useRef, useState } from "react";
import SocketIO from "socket.io-client";
import { auth } from "../../firebase/firebase.utils";
@@ -16,7 +15,14 @@ import {
import { useMutation } from "@apollo/client";
import { useTranslation } from "react-i18next";
import { useSplitTreatments } from "@splitsoftware/splitio-react";
import { SocketContext, INITIAL_NOTIFICATIONS } from "./useSocket.js";
import { INITIAL_NOTIFICATIONS, SocketContext } from "./useSocket.js";
import {
QUERY_MY_TASKS_COUNT,
QUERY_TASKS_NO_DUE_DATE_PAGINATED,
QUERY_TASKS_WITH_DUE_DATES
} from "../../graphql/tasks.queries";
const LIMIT = 50; // Tasks per page for no-due-date tasks
/**
* Socket Provider - Scenario Notifications / Web Socket related items
@@ -168,6 +174,103 @@ const SocketProvider = ({ children, bodyshop, navigate, currentUser }) => {
case "alert-update":
store.dispatch(addAlerts(message.payload));
break;
case "task-created":
case "task-updated":
case "task-deleted":
const payload = message.payload;
const assignedToId = bodyshop?.employees?.find((e) => e.user_email === currentUser?.email)?.id;
if (!assignedToId || payload.assigned_to !== assignedToId) return;
// Handle due date tasks cache update
const dueVars = {
bodyshop: bodyshop?.id,
assigned_to: assignedToId,
order: [{ due_date: "asc" }, { created_at: "desc" }]
};
let hasDueCache = false;
try {
client.readQuery({ query: QUERY_TASKS_WITH_DUE_DATES, variables: dueVars });
hasDueCache = true;
} catch (e) {}
if (hasDueCache) {
client
.query({ query: QUERY_TASKS_WITH_DUE_DATES, variables: dueVars, fetchPolicy: "network-only" })
.then(({ data }) => {
client.writeQuery({ query: QUERY_TASKS_WITH_DUE_DATES, variables: dueVars, data });
})
.catch((e) => console.error("Task due refetch error:", e));
}
// Handle no due date tasks cache update
const originalNoDueVars = {
bodyshop: bodyshop?.id,
assigned_to: assignedToId,
order: [{ created_at: "desc" }],
limit: LIMIT,
offset: 0
};
let currentLength = 0;
try {
const existing = client.readQuery({
query: QUERY_TASKS_NO_DUE_DATE_PAGINATED,
variables: originalNoDueVars
});
currentLength = existing?.tasks?.length || 0;
} catch (e) {}
if (currentLength > 0) {
let fetchLimit = currentLength;
if (message.type === "task-created") {
fetchLimit += 1;
} else if (message.type === "task-deleted") {
fetchLimit = Math.max(currentLength - 1, 0);
}
if (fetchLimit > 0) {
const fetchVars = { ...originalNoDueVars, limit: fetchLimit };
client
.query({
query: QUERY_TASKS_NO_DUE_DATE_PAGINATED,
variables: fetchVars,
fetchPolicy: "network-only"
})
.then(({ data }) => {
client.writeQuery({
query: QUERY_TASKS_NO_DUE_DATE_PAGINATED,
variables: originalNoDueVars,
data: {
tasks: data.tasks,
tasks_aggregate: data.tasks_aggregate
}
});
})
.catch((e) => console.error("Task no due refetch error:", e));
} else {
client.writeQuery({
query: QUERY_TASKS_NO_DUE_DATE_PAGINATED,
variables: originalNoDueVars,
data: { tasks: [], tasks_aggregate: { aggregate: { count: 0 } } }
});
}
}
// Handle task count cache update
const countVars = {
assigned_to: assignedToId,
bodyshopid: bodyshop?.id
};
let hasCountCache = false;
try {
client.readQuery({ query: QUERY_MY_TASKS_COUNT, variables: countVars });
hasCountCache = true;
} catch (e) {}
if (hasCountCache) {
client
.query({ query: QUERY_MY_TASKS_COUNT, variables: countVars, fetchPolicy: "network-only" })
.then(({ data }) => {
client.writeQuery({ query: QUERY_MY_TASKS_COUNT, variables: countVars, data });
})
.catch((e) => console.error("Task count refetch error:", e));
}
break;
default:
break;
}