Skip to content

Commit

Permalink
Allow amending and check questionnaire usage before delete
Browse files Browse the repository at this point in the history
  • Loading branch information
olimsaidov committed Jan 30, 2025
1 parent 6b6ea14 commit 637695e
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,41 @@ export default async function QuestionnairesPage({ searchParams }: PageProps) {
const total = response.total || 0;
const totalPages = Math.ceil(total / pageSize);

async function checkQuestionnaireUsage(questionnaire: Questionnaire) {
"use server";

let total = 0;
const aidbox = await getCurrentAidbox();

if (questionnaire.url) {
const response = await aidbox
.get("fhir/QuestionnaireResponse", {
searchParams: {
_summary: 'count',
questionnaire: questionnaire.url,
},
})
.json<Bundle<QuestionnaireResponse>>();

total += response.total || 0;

if (questionnaire.version && total === 0) {
const response = await aidbox
.get("fhir/QuestionnaireResponse", {
searchParams: {
_summary: 'count',
questionnaire: `${questionnaire.url}|${questionnaire.version}`,
},
})
.json<Bundle<QuestionnaireResponse>>();

total += response.total || 0;
}
}

return total !== 0;
}

async function deleteQuestionnaire(questionnaire: Questionnaire) {
"use server";

Expand Down Expand Up @@ -183,6 +218,7 @@ export default async function QuestionnairesPage({ searchParams }: PageProps) {
<TableCell className="text-right pr-6">
<QuestionnairesActions
questionnaire={resource}
onCheckQuestionnaireUsageAction={checkQuestionnaireUsage}
onDeleteAction={deleteQuestionnaire}
onCreateResponseAction={createQuestionnaireResponse}
/>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ export function FormsRenderer({
ref={ref}
questionnaire={JSON.stringify(questionnaire)}
questionnaire-response={JSON.stringify(questionnaireResponse)}
config={JSON.stringify({ form: { "allow-amend": true } })}
style={{
width: "100%",
height: "100%",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,12 +36,14 @@ import { useRouter } from "next/navigation";
export function QuestionnairesActions({
questionnaire,
library,
onCheckQuestionnaireUsageAction,
onDeleteAction,
onImportAction,
onCreateResponseAction,
}: {
questionnaire: Questionnaire;
library?: boolean;
onCheckQuestionnaireUsageAction?: (questionnaire: Questionnaire) => Promise<boolean>;
onDeleteAction?: (questionnaire: Questionnaire) => Promise<void>;
onImportAction?: (questionnaire: Questionnaire) => Promise<Questionnaire>;
onCreateResponseAction?: (
Expand Down Expand Up @@ -105,12 +107,16 @@ export function QuestionnairesActions({
className="text-destructive focus:text-destructive"
onClick={async () => {
if (onDeleteAction) {
await withRunning(onDeleteAction(questionnaire));
const used = onCheckQuestionnaireUsageAction ? await onCheckQuestionnaireUsageAction(questionnaire) : false;

toast({
title: "Questionnaire deleted",
description: `Questionnaire deleted successfully`,
});
if (confirm(`Are you sure you want to delete this questionnaire? ${used ? `\n\nWarning: This questionnaire is used in responses. Deleting it will make them invalid.` : ""}`)) {
await withRunning(onDeleteAction(questionnaire));

toast({
title: "Questionnaire deleted",
description: `Questionnaire deleted successfully`,
});
}
}
}}
>
Expand Down
3 changes: 3 additions & 0 deletions aidbox-forms-smart-launch-2/src/lib/server/aidbox.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,10 +64,13 @@ export const getOrganizationalAidbox = cache(async (serverUrl: string) => {
"[aidbox]",
response.status,
response.headers.get("content-type"),
);
console.dir(
await response
.clone()
.json()
.catch(() => null),
{ depth: 1000 },
);
},
],
Expand Down
1 change: 1 addition & 0 deletions aidbox-forms-smart-launch-2/types.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ declare global {
> & {
questionnaire?: string;
"questionnaire-response"?: string;
config?: string;
};
"aidbox-form-builder": React.DetailedHTMLProps<
React.IframeHTMLAttributes<HTMLIFrameElement>,
Expand Down

0 comments on commit 637695e

Please sign in to comment.