diff --git a/drama-queen/src/core/usecases/synchronizeData/selectors.ts b/drama-queen/src/core/usecases/synchronizeData/selectors.ts index 0b3afaf7..81f22f55 100644 --- a/drama-queen/src/core/usecases/synchronizeData/selectors.ts +++ b/drama-queen/src/core/usecases/synchronizeData/selectors.ts @@ -40,6 +40,22 @@ const surveyProgress = createSelector(downloadingState, (state) => { return (state.surveyCompleted * 100) / state.totalSurvey }) +const externalResourcesProgress = createSelector(downloadingState, (state) => { + if (state === undefined) { + return undefined + } + // if there is no external resources, we don't show the progress bar + if (state.totalExternalResources === undefined) { + return undefined + } + if ( + state.externalResourcesCompleted === 0 && + state.totalExternalResources === 0 + ) + return 100 + return (state.externalResourcesCompleted * 100) / state.totalExternalResources +}) + const uploadProgress = createSelector(state, (state) => { if (state.stateDescription !== 'running') { return undefined @@ -58,12 +74,14 @@ const main = createSelector( surveyUnitProgress, nomenclatureProgress, surveyProgress, + externalResourcesProgress, uploadProgress, ( state, surveyUnitProgress, nomenclatureProgress, surveyProgress, + externalResourcesProgress, uploadProgress ) => { switch (state.stateDescription) { @@ -86,6 +104,7 @@ const main = createSelector( surveyUnitProgress, nomenclatureProgress, surveyProgress, + externalResourcesProgress, } } } diff --git a/drama-queen/src/core/usecases/synchronizeData/state.ts b/drama-queen/src/core/usecases/synchronizeData/state.ts index 3dbeed3e..c04c14fc 100644 --- a/drama-queen/src/core/usecases/synchronizeData/state.ts +++ b/drama-queen/src/core/usecases/synchronizeData/state.ts @@ -1,6 +1,7 @@ import { createUsecaseActions } from 'redux-clean-architecture' import { id } from 'tsafe/id' import { assert } from 'tsafe/assert' +import { externalResourcesUrl } from './thunks' export type State = State.NotRunning | State.Running @@ -30,6 +31,8 @@ export namespace State { nomenclatureCompleted: number totalSurvey: number surveyCompleted: number + totalExternalResources?: number + externalResourcesCompleted: number } } } @@ -54,6 +57,11 @@ export const { reducer, actions } = createUsecaseActions({ nomenclatureCompleted: 0, totalSurvey: Infinity, surveyCompleted: 0, + // for total external resources, we make difference for displaying progress bar between : + // 0 : external synchro is triggered but there is no needed questionnaire so we want a fullfilled progress bar + // undefined : external synchro is not triggered so we don't want the progress bar + totalExternalResources: externalResourcesUrl ? Infinity : undefined, + externalResourcesCompleted: 0, }) ), runningUpload: () => @@ -116,6 +124,24 @@ export const { reducer, actions } = createUsecaseActions({ nomenclatureCompleted: state.nomenclatureCompleted + 1, } }, + setDownloadTotalExternalResources: ( + state, + { payload }: { payload: { totalExternalResources: number } } + ) => { + const { totalExternalResources } = payload + assert(state.stateDescription === 'running' && state.type === 'download') + return { + ...state, + totalExternalResources, + } + }, + downloadExternalResourceCompleted: (state) => { + assert(state.stateDescription === 'running' && state.type === 'download') + return { + ...state, + externalResourcesCompleted: state.externalResourcesCompleted + 1, + } + }, setUploadTotal: (state, { payload }: { payload: { total: number } }) => { const { total } = payload assert(state.stateDescription === 'running' && state.type === 'upload') diff --git a/drama-queen/src/core/usecases/synchronizeData/thunks.ts b/drama-queen/src/core/usecases/synchronizeData/thunks.ts index 711d472d..865f38e6 100644 --- a/drama-queen/src/core/usecases/synchronizeData/thunks.ts +++ b/drama-queen/src/core/usecases/synchronizeData/thunks.ts @@ -8,7 +8,7 @@ import { getResourcesFromExternalQuestionnaire, } from 'core/tools/externalResources' -const externalResourcesUrl = import.meta.env.VITE_EXTERNAL_RESOURCES_URL +export const externalResourcesUrl = import.meta.env.VITE_EXTERNAL_RESOURCES_URL const externalResourcesRootCacheName = 'cache-root-external' export const thunks = { @@ -196,14 +196,23 @@ export const thunks = { externalQuestionnaires ) + // set the total of needed external questionnaires for progress bar + dispatch( + actions.setDownloadTotalExternalResources({ + totalExternalResources: neededQuestionnaires.length, + }) + ) + // add in cache the missing external resources for needed questionnaires await Promise.all( - neededQuestionnaires.map((questionnaire) => + neededQuestionnaires.map((questionnaire) => { getResourcesFromExternalQuestionnaire( externalResourcesUrl, questionnaire + ).then(() => + dispatch(actions.downloadExternalResourceCompleted()) ) - ) + }) ) // delete the cache of every not needed external questionnaires diff --git a/drama-queen/src/i18n/resources/en.ts b/drama-queen/src/i18n/resources/en.ts index 1bf1b535..c5024a2b 100644 --- a/drama-queen/src/i18n/resources/en.ts +++ b/drama-queen/src/i18n/resources/en.ts @@ -77,6 +77,7 @@ export const translations: Translations<'en'> = { surveyUnitsProgress: 'Survey units', questionnairesProgress: 'Questionnaires', nomenclaturesProgress: 'Nomenclatures', + externalResourcesProgress: 'External resources', uploadingData: 'Sending data...', }, visualizeMessage: { diff --git a/drama-queen/src/i18n/resources/fr.ts b/drama-queen/src/i18n/resources/fr.ts index ba77118a..d22983ca 100644 --- a/drama-queen/src/i18n/resources/fr.ts +++ b/drama-queen/src/i18n/resources/fr.ts @@ -78,6 +78,7 @@ export const translations: Translations<'fr'> = { surveyUnitsProgress: 'Unités enquêtées', questionnairesProgress: 'Questionnaires', nomenclaturesProgress: 'Nomenclatures', + externalResourcesProgress: 'Ressources externes', uploadingData: 'Envoi des données...', }, visualizeMessage: { diff --git a/drama-queen/src/i18n/types.ts b/drama-queen/src/i18n/types.ts index 06a67a01..7a5beac6 100644 --- a/drama-queen/src/i18n/types.ts +++ b/drama-queen/src/i18n/types.ts @@ -76,6 +76,7 @@ export type SynchronizeMessage = | 'surveyUnitsProgress' | 'questionnairesProgress' | 'nomenclaturesProgress' + | 'externalResourcesProgress' | 'uploadingData' export type VisualizeMessage = diff --git a/drama-queen/src/ui/pages/synchronize/SynchronizeData.tsx b/drama-queen/src/ui/pages/synchronize/SynchronizeData.tsx index 018bc869..36786ebc 100644 --- a/drama-queen/src/ui/pages/synchronize/SynchronizeData.tsx +++ b/drama-queen/src/ui/pages/synchronize/SynchronizeData.tsx @@ -14,6 +14,7 @@ export function SynchronizeData() { nomenclatureProgress, surveyProgress, surveyUnitProgress, + externalResourcesProgress, uploadProgress, } = useCoreState('synchronizeData', 'main') @@ -68,6 +69,15 @@ export function SynchronizeData() { progress: surveyUnitProgress, label: t('surveyUnitsProgress'), }, + // render external resources progress bar only if there are external resources + ...(externalResourcesProgress !== undefined + ? [ + { + progress: externalResourcesProgress, + label: t('externalResourcesProgress'), + }, + ] + : []), ]} syncStepTitle={t('downloadingData')} />