Skip to content

Commit

Permalink
feat: handle progress bar for external resources synchro
Browse files Browse the repository at this point in the history
  • Loading branch information
QRuhier committed Oct 14, 2024
1 parent 7bf7e18 commit 284bc80
Show file tree
Hide file tree
Showing 7 changed files with 70 additions and 3 deletions.
19 changes: 19 additions & 0 deletions drama-queen/src/core/usecases/synchronizeData/selectors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -58,12 +74,14 @@ const main = createSelector(
surveyUnitProgress,
nomenclatureProgress,
surveyProgress,
externalResourcesProgress,
uploadProgress,
(
state,
surveyUnitProgress,
nomenclatureProgress,
surveyProgress,
externalResourcesProgress,
uploadProgress
) => {
switch (state.stateDescription) {
Expand All @@ -86,6 +104,7 @@ const main = createSelector(
surveyUnitProgress,
nomenclatureProgress,
surveyProgress,
externalResourcesProgress,
}
}
}
Expand Down
26 changes: 26 additions & 0 deletions drama-queen/src/core/usecases/synchronizeData/state.ts
Original file line number Diff line number Diff line change
@@ -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

Expand Down Expand Up @@ -30,6 +31,8 @@ export namespace State {
nomenclatureCompleted: number
totalSurvey: number
surveyCompleted: number
totalExternalResources?: number
externalResourcesCompleted: number
}
}
}
Expand All @@ -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: () =>
Expand Down Expand Up @@ -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')
Expand Down
15 changes: 12 additions & 3 deletions drama-queen/src/core/usecases/synchronizeData/thunks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {
Expand Down Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions drama-queen/src/i18n/resources/en.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ export const translations: Translations<'en'> = {
surveyUnitsProgress: 'Survey units',
questionnairesProgress: 'Questionnaires',
nomenclaturesProgress: 'Nomenclatures',
externalResourcesProgress: 'External resources',
uploadingData: 'Sending data...',
},
visualizeMessage: {
Expand Down
1 change: 1 addition & 0 deletions drama-queen/src/i18n/resources/fr.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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: {
Expand Down
1 change: 1 addition & 0 deletions drama-queen/src/i18n/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ export type SynchronizeMessage =
| 'surveyUnitsProgress'
| 'questionnairesProgress'
| 'nomenclaturesProgress'
| 'externalResourcesProgress'
| 'uploadingData'

export type VisualizeMessage =
Expand Down
10 changes: 10 additions & 0 deletions drama-queen/src/ui/pages/synchronize/SynchronizeData.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ export function SynchronizeData() {
nomenclatureProgress,
surveyProgress,
surveyUnitProgress,
externalResourcesProgress,
uploadProgress,
} = useCoreState('synchronizeData', 'main')

Expand Down Expand Up @@ -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')}
/>
Expand Down

0 comments on commit 284bc80

Please sign in to comment.