From e8cef376d6b30c7400c6d7eb04c7e9c8a5fa6c11 Mon Sep 17 00:00:00 2001 From: JORGE Date: Tue, 17 Dec 2024 10:35:48 -0400 Subject: [PATCH] [TM-1531] add bulk update call on clear --- .../Notification/FloatNotification.tsx | 23 ++++++--- src/connections/DelayedJob.ts | 48 +++++++++++++++++-- .../v3/jobService/jobServiceSchemas.ts | 4 ++ src/store/apiSlice.ts | 1 + 4 files changed, 66 insertions(+), 10 deletions(-) diff --git a/src/components/elements/Notification/FloatNotification.tsx b/src/components/elements/Notification/FloatNotification.tsx index df776995..c79c6418 100644 --- a/src/components/elements/Notification/FloatNotification.tsx +++ b/src/components/elements/Notification/FloatNotification.tsx @@ -1,9 +1,10 @@ import classNames from "classnames"; -import { useEffect, useState } from "react"; +import { useState } from "react"; import { When } from "react-if"; import Icon, { IconNames } from "@/components/extensive/Icon/Icon"; -import { useDelayedJobs } from "@/connections/DelayedJob"; +import { triggerBulkUpdate, useDelayedJobs } from "@/connections/DelayedJob"; +import { DelayedJobData, DelayedJobDto } from "@/generated/v3/jobService/jobServiceSchemas"; import Text from "../Text/Text"; @@ -21,9 +22,19 @@ const FloatNotification = () => { const [openModalNotification, setOpenModalNotification] = useState(false); const [isLoaded, { delayedJobs }] = useDelayedJobs(); - useEffect(() => { - console.log("delayedJobs", delayedJobs); - }, [isLoaded, delayedJobs]); + const clearJobs = () => { + if (delayedJobs === undefined) return; + const newJobsData: DelayedJobData[] = delayedJobs.map((job: DelayedJobDto) => { + return { + uuid: job.uuid, + type: "delayedJobs", + attributes: { + isAcknowledged: true + } + }; + }); + triggerBulkUpdate(newJobsData); + }; return (
@@ -42,7 +53,7 @@ const FloatNotification = () => { Actions Taken - + Clear completed
diff --git a/src/connections/DelayedJob.ts b/src/connections/DelayedJob.ts index 3e561ad3..c0608423 100644 --- a/src/connections/DelayedJob.ts +++ b/src/connections/DelayedJob.ts @@ -1,12 +1,17 @@ import { createSelector } from "reselect"; -import { listDelayedJobs } from "@/generated/v3/jobService/jobServiceComponents"; -import { listDelayedJobsFetchFailed } from "@/generated/v3/jobService/jobServicePredicates"; -import { DelayedJobDto } from "@/generated/v3/jobService/jobServiceSchemas"; +import { bulkUpdateJobs, listDelayedJobs } from "@/generated/v3/jobService/jobServiceComponents"; +import { + bulkUpdateJobsFetchFailed, + bulkUpdateJobsIsFetching, + listDelayedJobsFetchFailed +} from "@/generated/v3/jobService/jobServicePredicates"; +import { DelayedJobData, DelayedJobDto } from "@/generated/v3/jobService/jobServiceSchemas"; import { ApiDataStore } from "@/store/apiSlice"; import { Connection } from "@/types/connection"; -import { connectionHook } from "@/utils/connectionShortcuts"; +import { connectionHook, connectionLoader } from "@/utils/connectionShortcuts"; +// --- Delayed Jobs Connection --- type DelayedJobsConnection = { delayedJobs?: DelayedJobDto[]; isLoading: boolean; @@ -44,4 +49,39 @@ const delayedJobsConnection: Connection = { ) }; +export const triggerBulkUpdate = (jobs: DelayedJobData[]) => { + return bulkUpdateJobs({ body: { data: jobs } }); +}; + +const bulkUpdateJobsSelector = (store: ApiDataStore) => { + const bulkUpdateState = store.delayedJobs || {}; + return { + isLoading: bulkUpdateJobsIsFetching(store), + hasLoadFailed: bulkUpdateJobsFetchFailed(store) != null, + response: bulkUpdateState.response + }; +}; + +const bulkUpdateJobsConnection: Connection = { + load: async (connection, { jobs }) => { + const isLoaded = connectionBulkUpdateIsLoaded(connection); + if (!isLoaded) { + await bulkUpdateJobs({ body: { data: jobs } }); + } + }, + + isLoaded: state => !state.isLoading, + + selector: createSelector([store => bulkUpdateJobsSelector(store)], ({ isLoading, hasLoadFailed, response }) => ({ + isLoading, + hasLoadFailed, + response + })) +}; + +const connectionBulkUpdateIsLoaded = ({ isLoading, hasLoadFailed }: { isLoading: boolean; hasLoadFailed: boolean }) => { + return !isLoading && !hasLoadFailed; +}; + export const useDelayedJobs = connectionHook(delayedJobsConnection); +export const useBulkUpdateJobs = connectionLoader(bulkUpdateJobsConnection); diff --git a/src/generated/v3/jobService/jobServiceSchemas.ts b/src/generated/v3/jobService/jobServiceSchemas.ts index 23a8612a..2c8cd9a2 100644 --- a/src/generated/v3/jobService/jobServiceSchemas.ts +++ b/src/generated/v3/jobService/jobServiceSchemas.ts @@ -4,6 +4,10 @@ * @version 1.0 */ export type DelayedJobDto = { + /** + * The unique identifier for the delayed job. + */ + uuid: string; /** * The current status of the job. If the status is not pending, the payload and statusCode will be provided. */ diff --git a/src/store/apiSlice.ts b/src/store/apiSlice.ts index d1fff1cc..93ead786 100644 --- a/src/store/apiSlice.ts +++ b/src/store/apiSlice.ts @@ -45,6 +45,7 @@ export type Relationships = { }; export type StoreResource = { + id?: string; attributes: AttributeType; // We do a bit of munging on the shape from the API, removing the intermediate "data" member, and // ensuring there's always an array, to make consuming the data clientside a little smoother.