From 87faa53ade7857f8ac9e35713385249f40c6d7ef Mon Sep 17 00:00:00 2001 From: Ashesh <3626859+Ashesh3@users.noreply.github.com> Date: Mon, 12 Dec 2022 21:25:03 +0530 Subject: [PATCH] Add location selection to Asset import (#4284) --- src/Components/Assets/AssetImportModal.tsx | 274 +++++++++++++-------- 1 file changed, 168 insertions(+), 106 deletions(-) diff --git a/src/Components/Assets/AssetImportModal.tsx b/src/Components/Assets/AssetImportModal.tsx index d6d79c32352..fc04e31e7f0 100644 --- a/src/Components/Assets/AssetImportModal.tsx +++ b/src/Components/Assets/AssetImportModal.tsx @@ -6,6 +6,10 @@ import { FacilityModel } from "../Facility/models"; import { AssetData } from "./AssetTypes"; import * as Notification from "../../Utils/Notifications.js"; import ButtonV2 from "../Common/components/ButtonV2"; +import { listFacilityAssetLocation } from "../../Redux/actions"; +import { useDispatch } from "react-redux"; +import { Link } from "raviger"; +import SelectMenuV2 from "../Form/SelectMenuV2"; interface Props { open: boolean; @@ -17,6 +21,9 @@ const AssetImportModal = ({ open, onClose, facility }: Props) => { const [isImporting, setIsUploading] = useState(false); const [selectedFile, setSelectedFile] = useState(); const [preview, setPreview] = useState(); + const [location, setLocation] = useState(""); + const [locations, setLocations] = useState([]); + const dispatchAction: any = useDispatch(); const closeModal = () => { setPreview(undefined); @@ -24,6 +31,16 @@ const AssetImportModal = ({ open, onClose, facility }: Props) => { onClose && onClose(); }; + useEffect(() => { + dispatchAction( + listFacilityAssetLocation({}, { facility_external_id: facility.id }) + ).then(({ data }: any) => { + if (data.count > 0) { + setLocations(data.results); + } + }); + }, []); + useEffect(() => { const readFile = async () => { try { @@ -65,18 +82,20 @@ const AssetImportModal = ({ open, onClose, facility }: Props) => { is_working: asset.is_working, not_working_reason: asset.not_working_reason, serial_number: asset.serial_number, - location: asset.location_object.id, + location: location, vendor_name: asset.vendor_name, support_name: asset.support_name, support_email: asset.support_email, support_phone: asset.support_phone, qr_code_id: asset.qr_code_id, manufacturer: asset.manufacturer, + meta: { ...asset.meta }, warranty_amc_end_of_validity: asset.warranty_amc_end_of_validity, last_serviced_on: asset.last_serviced_on, notes: asset.notes, cancelToken: { promise: {} }, }); + const response = await fetch("/api/v1/asset/", { method: "POST", headers: { @@ -118,121 +137,164 @@ const AssetImportModal = ({ open, onClose, facility }: Props) => { return (
-
+
Import Assets {facility.name}
- - {preview && preview?.length > 0 ? ( -
+ {locations.length === 0 ? ( +

- {preview.length} assets will be imported + You need at least one location to import an assest.

-
-
-
#
-
Name
-
Description
-
- {preview.map((data: AssetData, index: number) => { - return ( -
-
{index + 1}
-
{data.name}
-
- {data.description} -
-
- ); - })} -
+ + + Add Asset Location + +
) : ( -
- -

- {dragProps.fileDropError !== "" - ? dragProps.fileDropError - : "Drag & drop JSON file to upload"} -

-

- Upload the JSON file exported from Care. -

-
- )} - -
-
- -
-
- { - e.stopPropagation(); - closeModal(); - dragProps.setFileDropError(""); - }} - disabled={isImporting} - > - Cancel - - - {isImporting ? ( - + <> + {preview && preview?.length > 0 ? ( +
+

+ {preview.length} assets will be imported +

+
+ +
+ ({ + title: location.name, + description: location.facility.name, + value: location.id, + })), + ]} + optionLabel={(o) => o.title} + optionValue={(o) => o.value} + value={location} + onChange={(e) => setLocation(e)} + /> +
+
+
+
+
#
+
Name
+
+ Description +
+
+ {preview.map((data: AssetData, index: number) => { + return ( +
+
{index + 1}
+
{data.name}
+
+ {data.description} +
+
+ ); + })} +
+
) : ( - +
+ +

+ {dragProps.fileDropError !== "" + ? dragProps.fileDropError + : "Drag & drop JSON file to upload"} +

+

+ Upload the JSON file exported from Care. +

+
)} - {isImporting ? "Importing..." : "Import"} -
-
- + +
+
+ +
+
+ { + e.stopPropagation(); + closeModal(); + dragProps.setFileDropError(""); + }} + disabled={isImporting} + > + Cancel + + + {isImporting ? ( + + ) : ( + + )} + {isImporting ? "Importing..." : "Import"} + +
+ + )} +
);