From d6d0a04e3a5b1a54d4c7d14f3866dff7ffcdc00b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9s=20Gonz=C3=A1lez?= Date: Wed, 17 Jan 2024 16:46:52 +0100 Subject: [PATCH] fixes error callbacks condition for project statuses --- .../projects/show/status/actions/failure.tsx | 49 ++++++++++++++++--- app/layout/projects/show/status/component.tsx | 2 +- app/layout/projects/show/status/utils.tsx | 12 ++--- app/types/api/project.ts | 1 + 4 files changed, 50 insertions(+), 14 deletions(-) diff --git a/app/layout/projects/show/status/actions/failure.tsx b/app/layout/projects/show/status/actions/failure.tsx index a85e9cfef9..629fbc2881 100644 --- a/app/layout/projects/show/status/actions/failure.tsx +++ b/app/layout/projects/show/status/actions/failure.tsx @@ -1,5 +1,7 @@ import React, { useCallback } from 'react'; +import { useQueryClient } from 'react-query'; + import { useRouter } from 'next/router'; import { useSaveProject } from 'hooks/projects'; @@ -7,7 +9,8 @@ import { useToasts } from 'hooks/toast'; export const useProjectActionsFailure = () => { const { query } = useRouter(); - const { pid } = query; + const { pid } = query as { pid: string }; + const queryClient = useQueryClient(); const projectMutation = useSaveProject({ requestConfig: { @@ -15,12 +18,16 @@ export const useProjectActionsFailure = () => { }, }); + const onSuccess = useCallback(async () => { + await queryClient.invalidateQueries(['project', pid]); + }, [queryClient, pid]); + const { addToast } = useToasts(); const onFailure = useCallback(() => { projectMutation.mutate( { - id: `${pid}`, + id: pid, data: { metadata: { cache: new Date().getTime(), @@ -28,7 +35,7 @@ export const useProjectActionsFailure = () => { }, }, { - onSuccess: () => {}, + onSuccess, onError: () => { addToast( 'onFailure', @@ -42,12 +49,12 @@ export const useProjectActionsFailure = () => { }, } ); - }, [pid, projectMutation, addToast]); + }, [pid, projectMutation, addToast, onSuccess]); const onFailureDownloadProject = useCallback(() => { projectMutation.mutate( { - id: `${pid}`, + id: pid, data: { metadata: { cache: new Date().getTime(), @@ -55,7 +62,7 @@ export const useProjectActionsFailure = () => { }, }, { - onSuccess: () => {}, + onSuccess, onError: () => { addToast( 'onFailureDownloadProject', @@ -69,11 +76,39 @@ export const useProjectActionsFailure = () => { }, } ); - }, [pid, projectMutation, addToast]); + }, [pid, projectMutation, addToast, onSuccess]); + + const onFailureCostSurface = useCallback(() => { + projectMutation.mutate( + { + id: pid, + data: { + metadata: { + cache: new Date().getTime(), + }, + }, + }, + { + onSuccess, + onError: () => { + addToast( + 'onFailureCostSurface', + <> +

There was an error processing the cost surface.

+ , + { + level: 'error', + } + ); + }, + } + ); + }, [pid, projectMutation, addToast, onSuccess]); return { default: onFailure, planningUnits: onFailure, export: onFailureDownloadProject, + costSurface: onFailureCostSurface, }; }; diff --git a/app/layout/projects/show/status/component.tsx b/app/layout/projects/show/status/component.tsx index 013a152aaa..73a65f0c63 100644 --- a/app/layout/projects/show/status/component.tsx +++ b/app/layout/projects/show/status/component.tsx @@ -39,7 +39,7 @@ export const ProjectStatus = (): JSX.Element => { const JOBS = useProjectJobs(jobs); // Failure - const JOB_FAILURE = useProjectJobFailure(JOBS, new Date(projectData?.lastModifiedAt).getTime()); + const JOB_FAILURE = useProjectJobFailure(JOBS, projectData?.metadata?.cache); const TEXT_FAILURE = useProjectTextFailure(JOB_FAILURE); // Done diff --git a/app/layout/projects/show/status/utils.tsx b/app/layout/projects/show/status/utils.tsx index bd38e94140..d6d71dd876 100644 --- a/app/layout/projects/show/status/utils.tsx +++ b/app/layout/projects/show/status/utils.tsx @@ -1,4 +1,4 @@ -import { useMemo } from 'react'; +import { MutableRefObject, useMemo } from 'react'; import groupBy from 'lodash/groupBy'; @@ -6,7 +6,7 @@ import { Job } from 'types/api/job'; import { TEXTS_FAILURE, TEXTS_RUNNING } from './constants'; -const getStatus = (arr) => { +const getStatus = (arr: Job[]): Job['status'] => { if (arr.some((d) => d.status === 'failure')) return 'failure'; if (arr.some((d) => d.status === 'running')) return 'running'; return 'done'; @@ -45,7 +45,7 @@ export const useProjectJobs = (jobs): Job[] => { }, [jobs]); }; -export const useProjectJobFailure = (jobs, lastJobCheck) => { +export const useProjectJobFailure = (jobs: Job[], lastJobCheck: number) => { return useMemo(() => { return jobs.find((j) => { const jobTimestamp = new Date(j.isoDate).getTime(); @@ -54,7 +54,7 @@ export const useProjectJobFailure = (jobs, lastJobCheck) => { }, [jobs, lastJobCheck]); }; -export const useProjectTextFailure = (JOB_FAILURE) => { +export const useProjectTextFailure = (JOB_FAILURE: Job) => { return useMemo(() => { if (JOB_FAILURE && TEXTS_FAILURE[JOB_FAILURE.kind]) { return TEXTS_FAILURE[JOB_FAILURE.kind](); @@ -77,13 +77,13 @@ export const useProjectJobDone = (jobs: Job[], lastJobCheck: number) => { }, [jobs, lastJobCheck]); }; -export const useProjectJobRunning = (jobs, JOB_FAILURE) => { +export const useProjectJobRunning = (jobs: Job[], JOB_FAILURE: Job) => { return useMemo(() => { return !JOB_FAILURE && jobs.find((j) => j.status === 'running'); }, [jobs, JOB_FAILURE]); }; -export const useProjectTextRunning = (JOB_RUNNING, JOB_DONE_REF) => { +export const useProjectTextRunning = (JOB_RUNNING: Job, JOB_DONE_REF: MutableRefObject) => { return useMemo(() => { if (JOB_RUNNING && TEXTS_RUNNING[JOB_RUNNING.kind]) { return TEXTS_RUNNING[JOB_RUNNING.kind || JOB_DONE_REF?.current?.kind](); diff --git a/app/types/api/project.ts b/app/types/api/project.ts index 05c697ec75..925ca61649 100644 --- a/app/types/api/project.ts +++ b/app/types/api/project.ts @@ -23,6 +23,7 @@ export interface Project { planningAreaName?: string; metadata?: { [key: string]: unknown; + cache: number; }; }