From 3edbdb9e303c13ccf8ce04530d5b9b895044a2c1 Mon Sep 17 00:00:00 2001 From: Dylan Decrulle <81740200+ddecrulle@users.noreply.github.com> Date: Fri, 12 Jan 2024 10:12:31 +0100 Subject: [PATCH] wip surveyUnitId route --- drama-queen/src/core/usecases/index.ts | 2 ++ drama-queen/src/core/usecases/surveyUnit.ts | 18 ++++++++++++++++ .../pages/queenMapping/SurveyUnitMapping.tsx | 5 +++++ .../src/ui/routing/loader/surveyUnitLoader.ts | 21 +++++++++++++++++++ drama-queen/src/ui/routing/routes.tsx | 6 ++++-- 5 files changed, 50 insertions(+), 2 deletions(-) create mode 100644 drama-queen/src/core/usecases/surveyUnit.ts create mode 100644 drama-queen/src/ui/routing/loader/surveyUnitLoader.ts diff --git a/drama-queen/src/core/usecases/index.ts b/drama-queen/src/core/usecases/index.ts index 340f8d56..7e2f267a 100644 --- a/drama-queen/src/core/usecases/index.ts +++ b/drama-queen/src/core/usecases/index.ts @@ -1,8 +1,10 @@ import * as userAuthentication from './userAuthentication' import * as synchronizeData from './synchronizeData' import * as visualizeSurvey from './visualizeSurvey/visualizeSurvey' +import * as surveyUnit from './surveyUnit' export const usecases = { userAuthentication, synchronizeData, visualizeSurvey, + surveyUnit, } diff --git a/drama-queen/src/core/usecases/surveyUnit.ts b/drama-queen/src/core/usecases/surveyUnit.ts new file mode 100644 index 00000000..fff5f48f --- /dev/null +++ b/drama-queen/src/core/usecases/surveyUnit.ts @@ -0,0 +1,18 @@ +import { assert } from 'tsafe/assert' +import type { Thunks } from 'core/bootstrap' + +export const name = 'surveyUnit' + +export const reducer = null + +export const thunks = { + getSurveyWithSurveyUnit: + (params: { surveyUnitId: string }) => + async (...args) => { + const [, , { dataStore }] = args + const { surveyUnitId } = params + + const surveyUnit = await dataStore.getSurveyUnit(surveyUnitId) + return surveyUnit?.questionnaireId + }, +} satisfies Thunks diff --git a/drama-queen/src/ui/pages/queenMapping/SurveyUnitMapping.tsx b/drama-queen/src/ui/pages/queenMapping/SurveyUnitMapping.tsx index bc7a0607..2f37927b 100644 --- a/drama-queen/src/ui/pages/queenMapping/SurveyUnitMapping.tsx +++ b/drama-queen/src/ui/pages/queenMapping/SurveyUnitMapping.tsx @@ -7,6 +7,11 @@ type Params = { } export function SurveyUnitMapping() { + // When this component is displayed this means that there is an Error + /** + * I proposed to delete this component in order to make a generic error route, and that loader pass messageError directly + */ + const { readonly, id } = useParams() return
Survey Unit Mapping id : {id}
diff --git a/drama-queen/src/ui/routing/loader/surveyUnitLoader.ts b/drama-queen/src/ui/routing/loader/surveyUnitLoader.ts new file mode 100644 index 00000000..37fde393 --- /dev/null +++ b/drama-queen/src/ui/routing/loader/surveyUnitLoader.ts @@ -0,0 +1,21 @@ +import { prCore } from 'bootstrap' +import { redirect, type LoaderFunctionArgs } from 'react-router-dom' +import { assert } from 'tsafe' + +export async function surveyUnitLoader({ params }: LoaderFunctionArgs) { + const { surveyUnit } = (await prCore).functions + + const surveyUnitId = params.surveyUnitId + + //surveyUnitId can't be undefined here (needed to match route) + assert(surveyUnitId !== undefined) + + const questionnaireId = await surveyUnit.getSurveyWithSurveyUnit({ + surveyUnitId, + }) + + //TODO handle case when questionnaireId is undefined + return redirect( + `/questionnaire/${questionnaireId}/survey-unit/${surveyUnitId}` + ) +} diff --git a/drama-queen/src/ui/routing/routes.tsx b/drama-queen/src/ui/routing/routes.tsx index b442832c..8a763de9 100644 --- a/drama-queen/src/ui/routing/routes.tsx +++ b/drama-queen/src/ui/routing/routes.tsx @@ -9,12 +9,14 @@ import { Visualize } from 'ui/pages/visualize/Visualize' import { Orchestrator } from 'ui/components/orchestrator/Orchestrator' import { protectedRouteLoader } from './loader/protectedLoader' import { visualizeLoader } from './loader/visualizeLoader' +import { surveyUnitLoader } from './loader/surveyUnitLoader' //ReadOnly path is a bad pattern must be change (affects pearl,moog,queen) export const routes: RouteObject[] = [ { - path: `/:${READ_ONLY}?/survey-unit/:id`, - element: , + path: '/survey-unit/:surveyUnitId', + Component: SurveyUnitMapping, + loader: surveyUnitLoader }, { path: `/:${READ_ONLY}?/questionnaire/:questionnaireId/survey-unit/:surveyUnitId`,