Skip to content

Commit

Permalink
refactor(loader): Structure code with comments
Browse files Browse the repository at this point in the history
  • Loading branch information
frederike-ramin committed Feb 15, 2024
1 parent 3a1807b commit 6b0d415
Showing 1 changed file with 20 additions and 3 deletions.
23 changes: 20 additions & 3 deletions app/routes/shared/step.server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -95,50 +95,60 @@ export const loader = async ({
// eslint-disable-next-line sonarjs/cognitive-complexity
}: LoaderFunctionArgs) => {
await throw404IfFeatureFlagEnabled(request);

// get data from request
const { pathname, searchParams } = new URL(request.url);
const returnTo = searchParams.get("returnTo") ?? undefined;
const { flowId, stepId, arrayIndex } = parsePathname(pathname);
const cookieId = request.headers.get("Cookie");

// get data from redis
const { data, id } = await getSessionForContext(flowId).getSession(cookieId);
const flowContext: Context = data; // Recast for now to get type safety
context.sessionId = getSessionForContext(flowId).getSessionId(id); // For showing in errors

// get flow controller
const currentFlow = flows[flowId];
const flowController = buildFlowController({
config: currentFlow.config,
data: flowContext,
guards: currentFlow.guards,
});

// check funnel logic -> Vorabcheck + Formular?
if (!returnTo && !flowController.isReachable(stepId))
return redirectDocument(flowController.getInitial().url);

// get migration data to display -> Formular
// Not having data here could skip the migration step
let migrationData: Record<string, unknown> = {};
if (stepId === "intro/daten-uebernahme" && "migrationSource" in currentFlow)
migrationData = await getMigrationData(flowId, currentFlow, cookieId);

// get all relevant strapi data
const pathNameWithoutArrayIndex = `/${flowId}/${stepId}`;
const lookupPath = pathNameWithoutArrayIndex.includes("persoenliche-daten")
? pathNameWithoutArrayIndex.replace("fluggastrechte", "geld-einklagen")
: pathNameWithoutArrayIndex;

const [
commonContent,
formPageContent,
parentMeta,
translations,
navTranslations,
] = await Promise.all([
fetchSingleEntry("vorab-check-common"),
fetchSingleEntry("vorab-check-common"), // TODO replace with translations
fetchCollectionEntry(currentFlow.cmsSlug, lookupPath),
fetchMeta({ filterValue: parentFromParams(pathname, params) }),
fetchTranslations(flowId),
fetchTranslations(`${flowId}/menu`),
]);

// filter user data for current step
const fieldNames = formPageContent.form.map((entry) => entry.name);
const stepData = stepDataFromFieldNames(fieldNames, data, arrayIndex);

// get array data to display in ArraySummary -> Formular + Vorabcheck?
const arrayData: ArrayCollection = Object.fromEntries(
formPageContent.pre_form.filter(isStrapiArraySummary).map((entry) => {
const possibleArray = flowContext[entry.arrayKey];
Expand All @@ -150,6 +160,8 @@ export const loader = async ({
);

// Inject heading into <legend> inside radio groups
// TODO: only do for pages with *one* select?
// TODO: We're not doing this for vorabcheck anymore -> revert acd2634b90b2edd95493fe140bd1c316a7b81ad8
formPageContent.form.forEach(({ __component, label }, idx) => {
if (
__component === "form-elements.select" &&
Expand All @@ -161,10 +173,15 @@ export const loader = async ({
}
});

// update session with csrf
const csrf = createCSRFToken();
const sessionContext = getSessionForContext("main");
const session = await csrfSessionFromRequest(csrf, request);

// update session with last valid step
session.set(lastStepKey, { [flowId]: stepId });

// set session in header
const sessionContext = getSessionForContext("main");
const headers = { "Set-Cookie": await sessionContext.commitSession(session) };

const cmsContent = structureCmsContent(formPageContent);
Expand Down

0 comments on commit 6b0d415

Please sign in to comment.