-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add edit questionnaire response page
- Loading branch information
1 parent
b1dc6c7
commit 98683c6
Showing
4 changed files
with
115 additions
and
2 deletions.
There are no files selected for viewing
71 changes: 71 additions & 0 deletions
71
aidbox-forms-smart-launch-2/src/app/(authorized)/questionnaire-responses/[id]/page.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
import { getCurrentAidbox } from "@/lib/server/smart"; | ||
import { PageHeader } from "@/components/page-header"; | ||
import { Questionnaire, QuestionnaireResponse } from "fhir/r4"; | ||
import { QuestionnaireEditor } from "@/components/questionnaire-editor"; | ||
import { getFirst } from "@/lib/utils"; | ||
import { QuestionnaireResponseEditor } from "@/components/questionnaire-response-editor"; | ||
|
||
interface PageProps { | ||
params: Promise<{ | ||
id: string; | ||
}>; | ||
} | ||
|
||
export default async function EditQuestionnaireResponsePage({ | ||
params, | ||
}: PageProps) { | ||
const aidbox = await getCurrentAidbox(); | ||
const { id } = await params; | ||
|
||
const questionnaireResponse = await aidbox | ||
.get(`fhir/QuestionnaireResponse/${id}`, {}) | ||
.json<QuestionnaireResponse>(); | ||
|
||
if (!questionnaireResponse) { | ||
throw new Error("Questionnaire response not found"); | ||
} | ||
|
||
const [url, version] = questionnaireResponse.questionnaire?.split("|") || []; | ||
|
||
const questionnaire = await aidbox | ||
.get(`fhir/Questionnaire?url=${url}&${version ? `version=${version}` : ""}`) | ||
.json<Questionnaire>() | ||
.then(getFirst); | ||
|
||
if (!questionnaire) { | ||
throw new Error("Questionnaire not found"); | ||
} | ||
|
||
async function saveQuestionnaireResponse( | ||
questionnaireResponse: QuestionnaireResponse, | ||
) { | ||
"use server"; | ||
|
||
const aidbox = await getCurrentAidbox(); | ||
return aidbox | ||
.put(`fhir/QuestionnaireResponse/${id}`, { | ||
json: questionnaireResponse, | ||
}) | ||
.json<QuestionnaireResponse>(); | ||
} | ||
|
||
return ( | ||
<> | ||
<PageHeader | ||
items={[ | ||
{ href: "/", label: "Home" }, | ||
{ | ||
href: "/questionnaire-responses", | ||
label: "Questionnaire responses", | ||
}, | ||
{ label: "Form filler" }, | ||
]} | ||
/> | ||
<QuestionnaireResponseEditor | ||
questionnaire={questionnaire} | ||
questionnaireResponse={questionnaireResponse} | ||
onSaveAction={saveQuestionnaireResponse} | ||
/> | ||
</> | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
41 changes: 41 additions & 0 deletions
41
aidbox-forms-smart-launch-2/src/components/questionnaire-response-editor.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
"use client"; | ||
|
||
import { FormsBuilder } from "@/components/forms-builder"; | ||
import { Questionnaire, QuestionnaireResponse } from "fhir/r4"; | ||
import { Suspense, useTransition } from "react"; | ||
import { Spinner } from "@/components/spinner"; | ||
import { FormsRenderer } from "@/components/forms-renderer"; | ||
|
||
interface QuestionnaireResponseEditorProps { | ||
questionnaire: Questionnaire; | ||
questionnaireResponse: QuestionnaireResponse; | ||
onSaveAction: ( | ||
questionnaireResponse: QuestionnaireResponse, | ||
) => Promise<QuestionnaireResponse>; | ||
} | ||
|
||
export function QuestionnaireResponseEditor({ | ||
questionnaire, | ||
questionnaireResponse, | ||
onSaveAction, | ||
}: QuestionnaireResponseEditorProps) { | ||
const [, startTransition] = useTransition(); | ||
|
||
return ( | ||
<Suspense fallback={<Spinner expand="true" />}> | ||
<FormsRenderer | ||
questionnaire={questionnaire} | ||
questionnaireResponse={questionnaireResponse} | ||
onChange={(updatedQuestionnaireResponse) => { | ||
startTransition(async () => { | ||
try { | ||
await onSaveAction(updatedQuestionnaireResponse); | ||
} catch (error) { | ||
console.error("Failed to save questionnaire response:", error); | ||
} | ||
}); | ||
}} | ||
/> | ||
</Suspense> | ||
); | ||
} |