IO-2924-Refactor-Production-board-to-use-Socket-Provider: Finalize

Signed-off-by: Dave Richer <dave@imexsystems.ca>
This commit is contained in:
Dave Richer
2024-09-25 21:34:43 -04:00
parent 60d1396011
commit c661fce8f1
2 changed files with 37 additions and 17 deletions

View File

@@ -81,11 +81,31 @@ function ProductionBoardKanbanContainer({ bodyshop, currentUser, subscriptionTyp
return; return;
} }
const handleJobChanged = async (jobChangedData) => { const handleJobUpdates = async (jobChangedData) => {
const jobId = jobChangedData.jobId; const jobId = jobChangedData.id;
const existingJob = data?.jobs.find((job) => job.id === jobId);
// Access the existing cache for QUERY_JOBS_IN_PRODUCTION
const existingJobsCache = client.readQuery({
query: QUERY_JOBS_IN_PRODUCTION
});
const existingJobs = existingJobsCache?.jobs || [];
// Check if the job already exists in the cached jobs
const existingJob = existingJobs.find((job) => job.id === jobId);
if (existingJob) { if (existingJob) {
// If the job exists, we update the cache without making any additional queries
client.writeQuery({
query: QUERY_JOBS_IN_PRODUCTION,
data: {
jobs: existingJobs.map((job) =>
job.id === jobId ? { ...existingJob, ...jobChangedData, __typename: "Job" } : job
)
}
});
} else {
// If the job doesn't exist, fetch it from the server and then add it to the cache
try { try {
const { data: jobData } = await client.query({ const { data: jobData } = await client.query({
query: GET_JOB_BY_PK, query: GET_JOB_BY_PK,
@@ -93,17 +113,12 @@ function ProductionBoardKanbanContainer({ bodyshop, currentUser, subscriptionTyp
fetchPolicy: "network-only" fetchPolicy: "network-only"
}); });
client.writeFragment({ // Add the job to the existing cached jobs
id: client.cache.identify({ __typename: "Job", id: jobId }), client.writeQuery({
fragment: gql` query: QUERY_JOBS_IN_PRODUCTION,
fragment UpdatedJob on Job { data: {
id jobs: [...existingJobs, { ...jobData.job, __typename: "Job" }]
status }
updatedAt
# ... include other fields you need to update
}
`,
data: jobData.job
}); });
} catch (error) { } catch (error) {
console.error(`Error fetching job ${jobId}: ${error.message}`); console.error(`Error fetching job ${jobId}: ${error.message}`);
@@ -112,11 +127,11 @@ function ProductionBoardKanbanContainer({ bodyshop, currentUser, subscriptionTyp
}; };
// Listen for 'job-changed' events // Listen for 'job-changed' events
socket.on("job-changed", handleJobChanged); socket.on("job-updated", handleJobUpdates);
// Clean up on unmount or when dependencies change // Clean up on unmount or when dependencies change
return () => { return () => {
socket.off("job-changed", handleJobChanged); socket.off("job-updated", handleJobUpdates);
}; };
}, [subscriptionEnabled, socket, bodyshop, data, client]); }, [subscriptionEnabled, socket, bodyshop, data, client]);

View File

@@ -24,7 +24,12 @@ const jobUpdated = async (req, res) => {
const bodyshopID = updatedJob.shopid; const bodyshopID = updatedJob.shopid;
// Emit the job-updated event only to the room corresponding to the bodyshop // Emit the job-updated event only to the room corresponding to the bodyshop
ioRedis.to(bodyshopID).emit("job-updated", updatedJob);
// Note: We are only sending the ID, because that is all that is needed
// What we should do is prevent hasura from sending anything more than it
// but the rewrite templates are currently borked.
ioRedis.to(bodyshopID).emit("job-updated", { id: updatedJob.id });
return res.json({ message: "Job updated and event emitted" }); return res.json({ message: "Job updated and event emitted" });
}; };