-
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.
- Loading branch information
1 parent
5c8ce08
commit 8264240
Showing
7 changed files
with
160 additions
and
5 deletions.
There are no files selected for viewing
46 changes: 46 additions & 0 deletions
46
aidbox-forms-smart-launch-2/src/app/(authorized)/questionnaires/[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,46 @@ | ||
import { getCurrentAidbox } from "@/lib/server/smart"; | ||
import { PageHeader } from "@/components/page-header"; | ||
import { Questionnaire } from "fhir/r4"; | ||
import { QuestionnaireEditor } from "@/components/questionnaire-editor"; | ||
|
||
interface PageProps { | ||
params: Promise<{ | ||
id: string; | ||
}>; | ||
} | ||
|
||
export default async function QuestionnairesPage({ params }: PageProps) { | ||
const aidbox = await getCurrentAidbox(); | ||
const { id } = await params; | ||
|
||
const questionnaire = await aidbox | ||
.get(`fhir/Questionnaire/${id}`, {}) | ||
.json<Questionnaire>(); | ||
|
||
async function saveQuestionnaire(questionnaire: Questionnaire) { | ||
"use server"; | ||
|
||
const aidbox = await getCurrentAidbox(); | ||
return aidbox | ||
.put(`fhir/Questionnaire/${id}`, { | ||
json: questionnaire, | ||
}) | ||
.json<Questionnaire>(); | ||
} | ||
|
||
return ( | ||
<> | ||
<PageHeader | ||
items={[ | ||
{ href: "/", label: "Home" }, | ||
{ href: "/questionnaires", label: "Questionnaires" }, | ||
{ label: "Editor" }, | ||
]} | ||
/> | ||
<QuestionnaireEditor | ||
questionnaire={questionnaire} | ||
onSaveAction={saveQuestionnaire} | ||
/> | ||
</> | ||
); | ||
} |
57 changes: 57 additions & 0 deletions
57
aidbox-forms-smart-launch-2/src/components/forms-builder.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,57 @@ | ||
"use client"; | ||
|
||
import { useEffect, useRef } from "react"; | ||
import { useAwaiter } from "@/hooks/use-awaiter"; | ||
import { Questionnaire } from "fhir/r4"; | ||
|
||
export function FormsBuilder({ | ||
questionnaire, | ||
onChange, | ||
}: { | ||
questionnaire: Questionnaire; | ||
onChange?: (questionnaire: Questionnaire) => void; | ||
}) { | ||
const ref = useRef<HTMLIFrameElement>(null); | ||
|
||
useEffect(() => { | ||
const current = ref.current; | ||
|
||
if (current) { | ||
const handler = (e: Event) => { | ||
onChange?.((e as CustomEvent<Questionnaire>).detail); | ||
}; | ||
|
||
current.addEventListener("change", handler); | ||
|
||
return () => { | ||
current.removeEventListener("change", handler); | ||
}; | ||
} | ||
}, [onChange]); | ||
|
||
useAwaiter(ref); | ||
|
||
return ( | ||
<aidbox-form-builder | ||
hide-back={true} | ||
show-share={false} | ||
hide-population={true} | ||
hide-extraction={true} | ||
hide-publish={true} | ||
hide-add-theme={true} | ||
hide-edit-theme={true} | ||
hide-save-theme={true} | ||
hide-convert={true} | ||
hide-save={true} | ||
disable-save={true} | ||
ref={ref} | ||
value={JSON.stringify(questionnaire)} | ||
style={{ | ||
width: "100%", | ||
height: "100%", | ||
border: "none", | ||
flex: 1, | ||
}} | ||
/> | ||
); | ||
} |
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
35 changes: 35 additions & 0 deletions
35
aidbox-forms-smart-launch-2/src/components/questionnaire-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,35 @@ | ||
"use client"; | ||
|
||
import { FormsBuilder } from "@/components/forms-builder"; | ||
import { Questionnaire } from "fhir/r4"; | ||
import { Suspense, useTransition } from "react"; | ||
import { Spinner } from "@/components/spinner"; | ||
|
||
interface QuestionnaireEditorProps { | ||
questionnaire: Questionnaire; | ||
onSaveAction: (questionnaire: Questionnaire) => Promise<Questionnaire>; | ||
} | ||
|
||
export function QuestionnaireEditor({ | ||
questionnaire, | ||
onSaveAction, | ||
}: QuestionnaireEditorProps) { | ||
const [, startTransition] = useTransition(); | ||
|
||
return ( | ||
<Suspense fallback={<Spinner expand="true" />}> | ||
<FormsBuilder | ||
questionnaire={questionnaire} | ||
onChange={(updatedQuestionnaire) => { | ||
startTransition(async () => { | ||
try { | ||
await onSaveAction(updatedQuestionnaire); | ||
} catch (error) { | ||
console.error("Failed to save questionnaire:", error); | ||
} | ||
}); | ||
}} | ||
/> | ||
</Suspense> | ||
); | ||
} |
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
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