Skip to content

Commit aa5327a

Browse files
committed
Completed the File upload option and created new component for View the page
1 parent a1b6f2c commit aa5327a

File tree

6 files changed

+156
-32
lines changed

6 files changed

+156
-32
lines changed

src/Routers/routes/ConsultationRoutes.tsx

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
import { Suspense, lazy } from "react";
2+
3+
import Loading from "@/components/Common/Loading";
14
import QuestionnaireResponseView from "@/components/Facility/ConsultationDetails/QuestionnaireResponseView";
25
import EncounterQuestionnaire from "@/components/Patient/EncounterQuestionnaire";
36
import TreatmentSummary from "@/components/Patient/TreatmentSummary";
@@ -6,6 +9,9 @@ import { AppRoutes } from "@/Routers/AppRouter";
69
import { EncounterShow } from "@/pages/Encounters/EncounterShow";
710
import { PrintPrescription } from "@/pages/Encounters/PrintPrescription";
811

12+
const ExcalidrawPage = lazy(() => import("@/components/Files/ExcalidrawPage"));
13+
const ExcalidrawView = lazy(() => import("@/components/Files/ExcalidrawView"));
14+
915
const consultationRoutes: AppRoutes = {
1016
"/facility/:facilityId/patient/:patientId/encounter/:encounterId/prescriptions/print":
1117
({ facilityId, encounterId, patientId }) => (
@@ -27,6 +33,20 @@ const consultationRoutes: AppRoutes = {
2733
patientId={patientId}
2834
/>
2935
),
36+
"/facility/:facilityId/patient/:patientId/encounter/:encounterId/drawings": ({
37+
encounterId,
38+
}) => (
39+
<Suspense fallback={<Loading />}>
40+
<ExcalidrawPage associatingId={encounterId} fileType="encounter" />
41+
</Suspense>
42+
),
43+
"/facility/:facilityId/patient/:patientId/encounter/:encounterId/drawings/:drawingId":
44+
({ drawingId }) => (
45+
<Suspense fallback={<Loading />}>
46+
<ExcalidrawView drawingId={drawingId} />
47+
</Suspense>
48+
),
49+
3050
"/facility/:facilityId/patient/:patientId/encounter/:encounterId/questionnaire/:slug":
3151
({ facilityId, encounterId, slug, patientId }) => (
3252
<EncounterQuestionnaire

src/Routers/routes/PatientRoutes.tsx

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,8 @@ import { AppRoutes } from "@/Routers/AppRouter";
1313
import { EncounterList } from "@/pages/Encounters/EncounterList";
1414
import VerifyPatient from "@/pages/Patients/VerifyPatient";
1515

16-
const ExcalidrawPage = lazy(
17-
() => import("@/components/Files/ExcalidrawDialog"),
18-
);
16+
const ExcalidrawPage = lazy(() => import("@/components/Files/ExcalidrawPage"));
17+
const ExcalidrawView = lazy(() => import("@/components/Files/ExcalidrawView"));
1918

2019
const PatientRoutes: AppRoutes = {
2120
"/facility/:facilityId/patients": ({ facilityId }) => (
@@ -54,18 +53,17 @@ const PatientRoutes: AppRoutes = {
5453
"/facility/:facilityId/patient/:id/drawings": ({ id }) => {
5554
return (
5655
<Suspense fallback={<Loading />}>
57-
<ExcalidrawPage patientId={id} />
56+
<ExcalidrawPage associatingId={id} fileType="patient" />
5857
</Suspense>
5958
);
6059
},
6160

6261
"/facility/:facilityId/patient/:patientId/drawings/:drawingId": ({
6362
drawingId,
64-
patientId,
6563
}) => {
6664
return (
6765
<Suspense fallback={<Loading />}>
68-
<ExcalidrawPage patientId={patientId} drawingId={drawingId} />
66+
<ExcalidrawView drawingId={drawingId} />
6967
</Suspense>
7068
);
7169
},

src/components/Files/ExcalidrawDialog.tsx renamed to src/components/Files/ExcalidrawPage.tsx

Lines changed: 27 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import { Excalidraw } from "@excalidraw/excalidraw";
22
import { type ExcalidrawElement } from "@excalidraw/excalidraw/types/element/types";
33
import { useMutation, useQuery } from "@tanstack/react-query";
44
import { t } from "i18next";
5+
import { navigate } from "raviger";
56
import { useEffect, useState } from "react";
67
import { toast } from "sonner";
78

@@ -10,6 +11,7 @@ import { debounce } from "@/lib/utils";
1011
import CareIcon from "@/CAREUI/icons/CareIcon";
1112

1213
import { Button } from "@/components/ui/button";
14+
import { Input } from "@/components/ui/input";
1315

1416
import Loading from "@/components/Common/Loading";
1517
import { CreateFileResponse } from "@/components/Patient/models";
@@ -19,16 +21,22 @@ import mutate from "@/Utils/request/mutate";
1921
import query from "@/Utils/request/query";
2022

2123
type Props = {
22-
patientId: string;
24+
associatingId: string;
25+
fileType: string;
2326
drawingId?: string;
2427
};
2528

26-
export default function ExcalidrawPage({ patientId, drawingId }: Props) {
29+
export default function ExcalidrawPage({
30+
associatingId,
31+
drawingId,
32+
fileType,
33+
}: Props) {
2734
const [elements, setElements] = useState<readonly ExcalidrawElement[] | null>(
2835
drawingId ? null : [],
2936
);
3037
const [name, setName] = useState("");
3138
const [id, setId] = useState(drawingId);
39+
const [isDirty, setIsDirty] = useState(false);
3240

3341
const { data, isLoading } = useQuery({
3442
queryKey: ["file", id],
@@ -37,7 +45,11 @@ export default function ExcalidrawPage({ patientId, drawingId }: Props) {
3745
}),
3846
enabled: !!id,
3947
});
40-
console.log(data);
48+
49+
useEffect(() => {
50+
setIsDirty(!!elements?.length);
51+
}, [elements?.length]);
52+
4153
useEffect(() => {
4254
if (!data) {
4355
return;
@@ -87,11 +99,11 @@ export default function ExcalidrawPage({ patientId, drawingId }: Props) {
8799
let response: CreateFileResponse | null = null;
88100
if (!id) {
89101
response = await createUpload({
90-
original_name: name,
102+
original_name: `${name}.excalidraw`,
91103
name: name,
92-
file_type: "patient",
104+
file_type: fileType,
93105
file_category: "unspecified",
94-
associating_id: patientId,
106+
associating_id: associatingId,
95107
mime_type: "text/plain",
96108
});
97109
signedUrl = response.signed_url;
@@ -117,6 +129,7 @@ export default function ExcalidrawPage({ patientId, drawingId }: Props) {
117129
return;
118130
} else {
119131
toast.success(t("file_success__upload_complete"));
132+
navigate(`drawings/${response!.id}`);
120133
}
121134
} catch (error) {
122135
console.error("Error in Step 1 (createUpload):", error);
@@ -128,27 +141,29 @@ export default function ExcalidrawPage({ patientId, drawingId }: Props) {
128141
}
129142

130143
return (
131-
<div className="flex flex-col h-[calc(100vh-5rem)]">
144+
<div className="flex flex-col h-[calc(100vh-4rem)]">
132145
<div className="flex flex-row justify-between items-center p-2">
133146
<div className="flex flex-row items-center">
134147
<div className="rounded-full bg-primary-100 px-5 py-4">
135148
<CareIcon icon="l-pen" className="text-lg text-primary-500" />
136149
</div>
137150
<div className="m-4">
138-
<input
151+
<Input
139152
type="text"
140153
value={name}
141154
placeholder="Enter the File Name"
142155
onChange={(e) => setName(e.target.value)}
143156
/>
144157
</div>
145158
</div>
146-
<Button className="ml-auto" onClick={handleSave}>
147-
{t("save")}
148-
</Button>
159+
{isDirty && (
160+
<Button className="ml-auto" onClick={handleSave}>
161+
{t("save")}
162+
</Button>
163+
)}
149164
</div>
150165

151-
<div className="flex-grow p-2">
166+
<div className="flex-grow h-[calc(100vh-10rem)] -m-2">
152167
<Excalidraw
153168
UIOptions={{
154169
canvasActions: {
@@ -158,7 +173,6 @@ export default function ExcalidrawPage({ patientId, drawingId }: Props) {
158173
},
159174
}}
160175
initialData={{
161-
elements: elements,
162176
appState: { theme: "light" },
163177
}}
164178
onChange={debounce(async (elements) => {
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
import { Excalidraw } from "@excalidraw/excalidraw";
2+
import { type ExcalidrawElement } from "@excalidraw/excalidraw/types/element/types";
3+
import { useQuery } from "@tanstack/react-query";
4+
import { useEffect, useState } from "react";
5+
6+
import CareIcon from "@/CAREUI/icons/CareIcon";
7+
8+
import { Input } from "@/components/ui/input";
9+
10+
import Loading from "@/components/Common/Loading";
11+
12+
import routes from "@/Utils/request/api";
13+
import query from "@/Utils/request/query";
14+
15+
type Props = {
16+
drawingId?: string;
17+
};
18+
19+
export default function ExcalidrawView({ drawingId }: Props) {
20+
const [elements, setElements] = useState<readonly ExcalidrawElement[] | null>(
21+
drawingId ? null : [],
22+
);
23+
const [name, setName] = useState("");
24+
const [id] = useState(drawingId);
25+
26+
const { data, isLoading } = useQuery({
27+
queryKey: ["file", id],
28+
queryFn: query(routes.retrieveUpload, {
29+
pathParams: { id: id || "" },
30+
}),
31+
enabled: !!id,
32+
});
33+
34+
useEffect(() => {
35+
if (!data) {
36+
return;
37+
}
38+
setName(data.name!);
39+
const fetchData = async () => {
40+
const response = await fetch(data.read_signed_url!);
41+
const json = await response.json();
42+
setElements(json.elements);
43+
};
44+
fetchData();
45+
}, [data]);
46+
47+
if (isLoading || elements === null) {
48+
return <Loading />;
49+
}
50+
51+
return (
52+
<div className="flex flex-col h-[calc(100vh-4rem)]">
53+
<div className="flex flex-row justify-between items-center p-2">
54+
<div className="flex flex-row items-center">
55+
<div className="rounded-full bg-primary-100 px-5 py-4">
56+
<CareIcon icon="l-pen" className="text-lg text-primary-500" />
57+
</div>
58+
<div className="m-4">
59+
<Input type="text" value={name} />
60+
</div>
61+
</div>
62+
</div>
63+
64+
<div className="flex-grow h-[calc(100vh-10rem)] -m-2">
65+
<Excalidraw
66+
viewModeEnabled={true}
67+
UIOptions={{
68+
canvasActions: {
69+
saveAsImage: true,
70+
export: false,
71+
loadScene: false,
72+
},
73+
}}
74+
initialData={{
75+
appState: { theme: "light" },
76+
elements: elements,
77+
}}
78+
/>
79+
</div>
80+
</div>
81+
);
82+
}

src/components/Files/FilesTab.tsx

Lines changed: 22 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -123,8 +123,6 @@ export const FilesTab = (props: FilesTabProps) => {
123123
}),
124124
});
125125

126-
console.log("facilityId", props.facilityId);
127-
console.log("PatientId", props.patientId);
128126
const fileManager = useFileManager({
129127
type: type,
130128
onArchive: refetch,
@@ -279,11 +277,17 @@ export const FilesTab = (props: FilesTabProps) => {
279277
{file.extension === ".excalidraw" && (
280278
<Button
281279
variant="secondary"
282-
onClick={() =>
283-
navigate(
284-
`/facility/${props.facilityId}/patient/${props.patientId}/drawings/${file.id}`,
285-
)
286-
}
280+
onClick={() => {
281+
if (type === "encounter") {
282+
navigate(
283+
`/facility/${props.facilityId}/patient/${props.patientId}/encounter/${props.encounter?.id}/drawings/${file.id}`,
284+
);
285+
} else {
286+
navigate(
287+
`/facility/${props.facilityId}/patient/${props.patientId}/drawings/${file.id}`,
288+
);
289+
}
290+
}}
287291
>
288292
<span className="flex flex-row items-center gap-1">
289293
<CareIcon icon="l-eye" />
@@ -458,11 +462,17 @@ export const FilesTab = (props: FilesTabProps) => {
458462
<Button
459463
size="sm"
460464
variant="ghost"
461-
onClick={() =>
462-
navigate(
463-
`/facility/${props.facilityId}/patient/${props.patientId}/drawings`,
464-
)
465-
}
465+
onClick={() => {
466+
if (type === "encounter") {
467+
navigate(
468+
`/facility/${props.facilityId}/patient/${props.patientId}/encounter/${props.encounter?.id}/drawings`,
469+
);
470+
} else {
471+
navigate(
472+
`/facility/${props.facilityId}/patient/${props.patientId}/drawings`,
473+
);
474+
}
475+
}}
466476
className="flex flex-row justify-stretch items-center w-full text-primary-900"
467477
>
468478
<CareIcon icon="l-pen" />
@@ -561,7 +571,6 @@ export const FilesTab = (props: FilesTabProps) => {
561571
files.results.map((file) => {
562572
const filetype = getFileType(file);
563573
const fileName = file.name ? file.name + file.extension : "";
564-
console.log("fileName", file.extension);
565574
return (
566575
<TableRow
567576
key={file.id}

src/pages/Encounters/tabs/EncounterFilesTab.tsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ export const EncounterFilesTab = (props: EncounterTabProps) => {
77
<FilesTab
88
type="encounter"
99
facilityId={props.facilityId}
10+
patientId={props.patient.id}
1011
encounter={props.encounter}
1112
subPage={props.subPage}
1213
/>

0 commit comments

Comments
 (0)