Skip to content

Commit

Permalink
Merge branch 'develop' into issues/8610/fix-clearing-facilities-white…
Browse files Browse the repository at this point in the history
…-screen
  • Loading branch information
nihal467 authored Sep 27, 2024
2 parents 8c79b03 + 53c5a7a commit c0a2b61
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 14 deletions.
12 changes: 12 additions & 0 deletions src/Common/hooks/useFilters.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import PaginationComponent from "../../Components/Common/Pagination";
import { classNames, humanizeStrings } from "../../Utils/utils";
import FiltersCache from "../../Utils/FiltersCache";
import careConfig from "@careConfig";
import { triggerGoal } from "../../Integrations/Plausible";

export type FilterState = Record<string, unknown>;

Expand Down Expand Up @@ -42,6 +43,17 @@ export default function useFilters({
) => {
query = FiltersCache.utils.clean(query);
_setQueryParams(query, options);

// For each of the newly applied filters (additional filters compared to
// previously applied ones), trigger a plausible goal "Advanced filter
// applied" with the applied filter's query key and current location as tags.
Object.keys(query).forEach((filter) =>
triggerGoal("Advanced filter applied", {
filter,
location: location.pathname,
}),
);

updateCache(query);
};

Expand Down
11 changes: 6 additions & 5 deletions src/Components/Files/FileUpload.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,7 @@ export const FileUpload = (props: FileUploadProps) => {
"ods",
"pdf",
],
allowNameFallback: false,
onUpload: refetchAll,
});

Expand Down Expand Up @@ -259,7 +260,7 @@ export const FileUpload = (props: FileUploadProps) => {
</span>
<button
onClick={fileUpload.clearFiles}
disabled={!!fileUpload.progress}
disabled={fileUpload.uploading}
className="text-lg"
>
<CareIcon icon="l-times" />
Expand All @@ -271,8 +272,8 @@ export const FileUpload = (props: FileUploadProps) => {
label={t("enter_file_name")}
id="upload-file-name"
required
value={fileUpload.fileNames[0]}
disabled={!!fileUpload.progress}
value={fileUpload.fileNames[0] || ""}
disabled={fileUpload.uploading}
onChange={(e) => fileUpload.setFileName(e.value)}
error={fileUpload.error || undefined}
/>
Expand All @@ -281,7 +282,7 @@ export const FileUpload = (props: FileUploadProps) => {
onClick={() =>
fileUpload.handleFileUpload(associatedId)
}
loading={!!fileUpload.progress}
loading={fileUpload.uploading}
className="w-full"
id="upload_file_button"
>
Expand All @@ -291,7 +292,7 @@ export const FileUpload = (props: FileUploadProps) => {
<ButtonV2
variant="danger"
onClick={fileUpload.clearFiles}
disabled={!!fileUpload.progress}
disabled={fileUpload.uploading}
>
<CareIcon icon="l-trash-alt" className="" />
{t("discard")}
Expand Down
7 changes: 4 additions & 3 deletions src/Components/Patient/PatientConsentRecords.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ export default function PatientConsentRecords(props: {
const fileUpload = useFileUpload({
type: "CONSENT_RECORD",
allowedExtensions: ["pdf", "jpg", "jpeg", "png"],
allowNameFallback: false,
});

const fileManager = useFileManager({
Expand Down Expand Up @@ -181,7 +182,7 @@ export default function PatientConsentRecords(props: {
<TextFormField
name="filename"
label="File Name"
value={fileUpload.fileNames[0]}
value={fileUpload.fileNames[0] || ""}
onChange={(e) => fileUpload.setFileName(e.value)}
/>
<div className="flex gap-2">
Expand All @@ -203,7 +204,7 @@ export default function PatientConsentRecords(props: {
handleUpload();
}
}}
loading={!!fileUpload.progress}
loading={fileUpload.uploading}
disabled={
newConsent.type === 2 &&
newConsent.patient_code_status === 0
Expand All @@ -216,7 +217,7 @@ export default function PatientConsentRecords(props: {
<ButtonV2
variant="danger"
onClick={fileUpload.clearFiles}
disabled={!!fileUpload.progress}
disabled={fileUpload.uploading}
>
<CareIcon icon="l-trash-alt" className="" />
</ButtonV2>
Expand Down
1 change: 1 addition & 0 deletions src/Locale/en/FileUpload.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
"file_list_headings__supporting_info": "Supporting Info",
"file_error__choose_file": "Please choose a file to upload",
"file_error__file_name": "Please give a name for all files!",
"file_error__single_file_name": "Please give a name for the file",
"change_file": "Change File",
"file_error__file_size": "Maximum size of files is 100 MB",
"file_error__file_type": "Invalid file type \".{{extension}}\" Allowed types: {{allowedExtensions}}",
Expand Down
33 changes: 27 additions & 6 deletions src/Utils/useFileUpload.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import {
ChangeEvent,
DetailedHTMLProps,
InputHTMLAttributes,
useEffect,
useState,
} from "react";
import {
Expand All @@ -24,6 +25,8 @@ export type FileUploadOptions = {
type: string;
category?: FileCategory;
onUpload?: (file: FileUploadModel) => void;
// if allowed, will fallback to the name of the file if a seperate filename is not defined.
allowNameFallback?: boolean;
} & (
| {
allowedExtensions?: string[];
Expand Down Expand Up @@ -54,6 +57,7 @@ export type FileUploadReturn = {
setFileNames: (names: string[]) => void;
removeFile: (index: number) => void;
clearFiles: () => void;
uploading: boolean;
};

// Array of image extensions
Expand All @@ -71,13 +75,20 @@ const ExtImage: string[] = [
export default function useFileUpload(
options: FileUploadOptions,
): FileUploadReturn {
const { type, onUpload, category = "UNSPECIFIED", multiple } = options;
const {
type,
onUpload,
category = "UNSPECIFIED",
multiple,
allowNameFallback = true,
} = options;

const [uploadFileNames, setUploadFileNames] = useState<string[]>([]);
const [error, setError] = useState<string | null>(null);
const [progress, setProgress] = useState<null | number>(null);
const [cameraModalOpen, setCameraModalOpen] = useState(false);
const [audioModalOpen, setAudioModalOpen] = useState(false);
const [uploading, setUploading] = useState(false);

const [files, setFiles] = useState<File[]>([]);

Expand All @@ -104,6 +115,11 @@ export default function useFileUpload(
});
};

useEffect(() => {
const blanks = Array(files.length).fill("");
setUploadFileNames((names) => [...names, ...blanks].slice(0, files.length));
}, [files]);

const validateFileUpload = () => {
if (files.length === 0) {
setError(t("file_error__choose_file"));
Expand Down Expand Up @@ -199,9 +215,14 @@ export default function useFileUpload(

for (const [index, file] of files.entries()) {
const filename =
uploadFileNames[index] === "" && file
allowNameFallback && uploadFileNames[index] === "" && file
? file.name
: uploadFileNames[index];
if (!filename) {
setError(t("file_error__single_file_name"));
return;
}
setUploading(true);

const { data } = await request(routes.createUpload, {
body: {
Expand All @@ -220,6 +241,7 @@ export default function useFileUpload(
}
}

setUploading(false);
setFiles([]);
setUploadFileNames([]);
};
Expand All @@ -229,17 +251,15 @@ export default function useFileUpload(
<CameraCaptureDialog
show={cameraModalOpen}
onHide={() => setCameraModalOpen(false)}
onCapture={(file, fileName) => {
onCapture={(file) => {
setFiles((prev) => [...prev, file]);
setUploadFileNames((prev) => [...prev, fileName]);
}}
/>
<AudioCaptureDialog
show={audioModalOpen}
onHide={() => setAudioModalOpen(false)}
onCapture={(file, fileName) => {
onCapture={(file) => {
setFiles((prev) => [...prev, file]);
setUploadFileNames((prev) => [...prev, fileName]);
}}
autoRecord
/>
Expand Down Expand Up @@ -290,5 +310,6 @@ export default function useFileUpload(
setFiles([]);
setUploadFileNames([]);
},
uploading,
};
}

0 comments on commit c0a2b61

Please sign in to comment.