From c661fce8f114af1a94a1a63a52c9218b130b0846 Mon Sep 17 00:00:00 2001 From: Dave Richer Date: Wed, 25 Sep 2024 21:34:43 -0400 Subject: [PATCH] IO-2924-Refactor-Production-board-to-use-Socket-Provider: Finalize Signed-off-by: Dave Richer --- .../production-board-kanban.container.jsx | 47 ++++++++++++------- server/job/job-updated.js | 7 ++- 2 files changed, 37 insertions(+), 17 deletions(-) diff --git a/client/src/components/production-board-kanban/production-board-kanban.container.jsx b/client/src/components/production-board-kanban/production-board-kanban.container.jsx index d6a04fedd..31220a9bd 100644 --- a/client/src/components/production-board-kanban/production-board-kanban.container.jsx +++ b/client/src/components/production-board-kanban/production-board-kanban.container.jsx @@ -81,11 +81,31 @@ function ProductionBoardKanbanContainer({ bodyshop, currentUser, subscriptionTyp return; } - const handleJobChanged = async (jobChangedData) => { - const jobId = jobChangedData.jobId; - const existingJob = data?.jobs.find((job) => job.id === jobId); + const handleJobUpdates = async (jobChangedData) => { + const jobId = jobChangedData.id; + + // 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 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 { const { data: jobData } = await client.query({ query: GET_JOB_BY_PK, @@ -93,17 +113,12 @@ function ProductionBoardKanbanContainer({ bodyshop, currentUser, subscriptionTyp fetchPolicy: "network-only" }); - client.writeFragment({ - id: client.cache.identify({ __typename: "Job", id: jobId }), - fragment: gql` - fragment UpdatedJob on Job { - id - status - updatedAt - # ... include other fields you need to update - } - `, - data: jobData.job + // Add the job to the existing cached jobs + client.writeQuery({ + query: QUERY_JOBS_IN_PRODUCTION, + data: { + jobs: [...existingJobs, { ...jobData.job, __typename: "Job" }] + } }); } catch (error) { console.error(`Error fetching job ${jobId}: ${error.message}`); @@ -112,11 +127,11 @@ function ProductionBoardKanbanContainer({ bodyshop, currentUser, subscriptionTyp }; // Listen for 'job-changed' events - socket.on("job-changed", handleJobChanged); + socket.on("job-updated", handleJobUpdates); // Clean up on unmount or when dependencies change return () => { - socket.off("job-changed", handleJobChanged); + socket.off("job-updated", handleJobUpdates); }; }, [subscriptionEnabled, socket, bodyshop, data, client]); diff --git a/server/job/job-updated.js b/server/job/job-updated.js index 87ed23640..61667e9ea 100644 --- a/server/job/job-updated.js +++ b/server/job/job-updated.js @@ -24,7 +24,12 @@ const jobUpdated = async (req, res) => { const bodyshopID = updatedJob.shopid; // 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" }); };