Skip to content

Commit

Permalink
Add import and delete questionnaire
Browse files Browse the repository at this point in the history
  • Loading branch information
olimsaidov committed Nov 22, 2024
1 parent 8264240 commit 934c4ab
Show file tree
Hide file tree
Showing 7 changed files with 68 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import { isDefined } from "@/lib/utils";
import { decidePageSize } from "@/lib/server/utils";
import ky from "ky";
import { QuestionnairesActions } from "@/components/questionnaires-actions";
import { getCurrentAidbox } from "@/lib/server/smart";

interface PageProps {
searchParams: Promise<{
Expand Down Expand Up @@ -53,6 +54,17 @@ export default async function PublicLibraryPage({ searchParams }: PageProps) {
const total = response.total || 0;
const totalPages = Math.ceil(total / pageSize);

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

const aidbox = await getCurrentAidbox();
return aidbox
.post("fhir/Questionnaire", {
json: { ...questionnaire, id: undefined },
})
.json<Questionnaire>();
}

return (
<>
<PageHeader
Expand Down Expand Up @@ -97,7 +109,11 @@ export default async function PublicLibraryPage({ searchParams }: PageProps) {
<TableCell>{resource.status}</TableCell>
<TableCell>{resource.date}</TableCell>
<TableCell className="text-right pr-6">
<QuestionnairesActions questionnaire={resource} library />
<QuestionnairesActions
library
questionnaire={resource}
onImportAction={importQuestionnaire}
/>
</TableCell>
</TableRow>
))}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import { Bundle, Questionnaire } from "fhir/r4";
import { isDefined } from "@/lib/utils";
import { decidePageSize } from "@/lib/server/utils";
import { QuestionnairesActions } from "@/components/questionnaires-actions";
import { revalidatePath } from "next/cache";

interface PageProps {
searchParams: Promise<{
Expand Down Expand Up @@ -51,6 +52,14 @@ export default async function QuestionnairesPage({ searchParams }: PageProps) {
const total = response.total || 0;
const totalPages = Math.ceil(total / pageSize);

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

const aidbox = await getCurrentAidbox();
await aidbox.delete(`fhir/Questionnaire/${questionnaire.id}`).json();
revalidatePath("/questionnaires");
}

return (
<>
<PageHeader
Expand Down Expand Up @@ -95,7 +104,10 @@ export default async function QuestionnairesPage({ searchParams }: PageProps) {
<TableCell>{resource.status}</TableCell>
<TableCell>{resource.date}</TableCell>
<TableCell className="text-right pr-6">
<QuestionnairesActions questionnaire={resource} />
<QuestionnairesActions
questionnaire={resource}
onDeleteAction={deleteQuestionnaire}
/>
</TableCell>
</TableRow>
))}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,14 +30,21 @@ import {
import { FormsRenderer } from "@/components/forms-renderer";
import { useToast } from "@/hooks/use-toast";
import { Spinner } from "@/components/spinner";
import { ToastAction } from "@/components/ui/toast";
import { useRouter } from "next/navigation";

export function QuestionnairesActions({
questionnaire,
library,
onDeleteAction,
onImportAction,
}: {
questionnaire: Questionnaire;
library?: boolean;
onDeleteAction?: (questionnaire: Questionnaire) => Promise<void>;
onImportAction?: (questionnaire: Questionnaire) => Promise<Questionnaire>;
}) {
const router = useRouter();
const [viewing, setViewing] = useState(false);
const { toast } = useToast();

Expand Down Expand Up @@ -80,7 +87,16 @@ export function QuestionnairesActions({
{!library && (
<DropdownMenuItem
className="text-destructive focus:text-destructive"
onClick={() => {}}
onClick={async () => {
if (onDeleteAction) {
await onDeleteAction(questionnaire);

toast({
title: "Questionnaire deleted",
description: `Questionnaire deleted successfully`,
});
}
}}
>
<Trash2 />
Delete
Expand All @@ -95,7 +111,27 @@ export function QuestionnairesActions({
)}

{library && (
<DropdownMenuItem onClick={() => {}}>
<DropdownMenuItem
onClick={async () => {
if (onImportAction) {
const { id } = await onImportAction(questionnaire);
toast({
title: "Questionnaire imported",
description: `Questionnaire imported successfully`,
action: (
<ToastAction
altText="Edit"
onClick={() => {
router.push(`/questionnaires/${id}`);
}}
>
Edit
</ToastAction>
),
});
}
}}
>
<Import />
Import
</DropdownMenuItem>
Expand Down
2 changes: 0 additions & 2 deletions aidbox-forms-smart-launch-2/src/lib/server/aidbox.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
"use server";

import ky from "ky";
import { CapabilityStatement, Organization } from "fhir/r4";
import { sha256 } from "@/lib/utils";
Expand Down
2 changes: 0 additions & 2 deletions aidbox-forms-smart-launch-2/src/lib/server/redis.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
"use server";

import Redis from "ioredis";
import { cache } from "react";

Expand Down
2 changes: 0 additions & 2 deletions aidbox-forms-smart-launch-2/src/lib/server/smart.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
"use server";

import { IronSession } from "iron-session";
import { NextApiRequest, NextApiResponse } from "next";
import BaseServerStorage from "fhirclient/lib/storage/BrowserStorage";
Expand Down
2 changes: 0 additions & 2 deletions aidbox-forms-smart-launch-2/src/lib/server/sync.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
"use server";

import Client from "fhirclient/lib/Client";
import {
Bundle,
Expand Down

0 comments on commit 934c4ab

Please sign in to comment.