Skip to content

Commit

Permalink
Sample support page code
Browse files Browse the repository at this point in the history
  • Loading branch information
lsolcher committed Aug 15, 2024
1 parent f76750a commit 2211458
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import * as fs from "node:fs";
import * as path from "node:path";
import { PDFBool, PDFDocument, PDFName } from "pdf-lib";
import { assessment } from "resources/content";
import { getAnswersFromCookie } from "utils/cookies.server";
import { deleteCookie, getAnswersFromCookie } from "utils/cookies.server";
import trackCustomEvent from "utils/trackCustomEvent.server";

export const FIELD_NAME_POLICY_TITLE = "Titel des Regelungsvorhabens";
Expand All @@ -28,6 +28,7 @@ interface UserInput {

const POSITIVE_RESULT = "yes";
const NEGATIVE_RESULT = "no";
let savedAnswers = {};

const createPreCheckPDF = async function (
userInput: UserInput,
Expand Down Expand Up @@ -104,13 +105,18 @@ export function loader({ request }: LoaderFunctionArgs) {

export async function action({ params, request }: ActionFunctionArgs) {
const { fileName } = params;
const { answers } = await getAnswersFromCookie(request);
let { answers } = await getAnswersFromCookie(request);
const formData = await request.formData();
const { title, negativeReasoning } = Object.fromEntries(formData);

if (Object.keys(answers).length === 0) {
if (
Object.keys(answers).length === 0 &&
Object.keys(savedAnswers).length === 0
) {
throw new Response("No answers available in cookies", { status: 409 });

Check warning on line 115 in packages/dito/app/routes/vorpruefung.ergebnis.einschaetzung.$fileName[.pdf].tsx

View workflow job for this annotation

GitHub Actions / check-test-build-deploy (dito) / check-and-test-shared / check-and-test

Expected an error object to be thrown

Check warning on line 115 in packages/dito/app/routes/vorpruefung.ergebnis.einschaetzung.$fileName[.pdf].tsx

View workflow job for this annotation

GitHub Actions / check-test-build-deploy (dito) / check-and-test-shared / check-and-test

Expected an error object to be thrown
}
if (Object.keys(answers).length === 0) {
answers = savedAnswers;
}

if (typeof title !== "string" || title === "") {
throw new Response(assessment.form.policyTitleRequired, { status: 400 });

Check warning on line 122 in packages/dito/app/routes/vorpruefung.ergebnis.einschaetzung.$fileName[.pdf].tsx

View workflow job for this annotation

GitHub Actions / check-test-build-deploy (dito) / check-and-test-shared / check-and-test

Expected an error object to be thrown

Check warning on line 122 in packages/dito/app/routes/vorpruefung.ergebnis.einschaetzung.$fileName[.pdf].tsx

View workflow job for this annotation

GitHub Actions / check-test-build-deploy (dito) / check-and-test-shared / check-and-test

Expected an error object to be thrown
Expand Down Expand Up @@ -161,13 +167,15 @@ export async function action({ params, request }: ActionFunctionArgs) {
name: "Download Vorprüfung",
props: { result },
});

savedAnswers = answers;
const cookieDeletionHeader = await deleteCookie();
return new Response(pdfData, {
status: 200,
headers: {
"Content-Type": "application/pdf",
"Content-Disposition": `attachment; filename="${fileName}.pdf"`,
"Content-Length": `${pdfData.byteLength}`,
...cookieDeletionHeader.headers,
},
});
}
6 changes: 6 additions & 0 deletions packages/dito/app/utils/cookies.server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,9 @@ export const getAnswersFromCookie = async (request: Request) => {
export const getHeaderFromCookie = async (cookie: { answers: Answers }) => {
return { headers: { "Set-Cookie": await userAnswers.serialize(cookie) } };
};

export const deleteCookie = async () => {
return {
headers: { "Set-Cookie": await userAnswers.serialize("", { maxAge: 0 }) },
};
};

0 comments on commit 2211458

Please sign in to comment.