Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix: Asset tab facility and location badges are malfunctioning #6474

Merged
merged 9 commits into from
Oct 26, 2023
32 changes: 3 additions & 29 deletions src/Components/Assets/AssetFilter.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { navigate, useQueryParams } from "raviger";
import { FacilitySelect } from "../Common/FacilitySelect";
import { FacilityModel } from "../Facility/models";
import { LocationSelect } from "../Common/LocationSelect";
import { AssetClass, AssetLocationObject } from "./AssetTypes";
import { AssetClass } from "./AssetTypes";
import { FieldLabel } from "../Form/FormFields/FormField";
import { SelectFormField } from "../Form/FormFields/SelectFormField";
import FiltersSlideover from "../../CAREUI/interactive/FiltersSlideover";
Expand All @@ -15,24 +15,12 @@ import { FieldChangeEvent } from "../Form/FormFields/Utils";
import { DateRange } from "../Common/DateRangeInputV2";
import { dateQueryString } from "../../Utils/utils";

const initialLocation = {
id: "",
name: "",
description: "",
facility: {
id: "",
name: "",
},
};

const getDate = (value: any) =>
value && dayjs(value).isValid() && dayjs(value).toDate();

function AssetFilter(props: any) {
const { filter, onChange, closeFilter } = props;
const [facility, setFacility] = useState<FacilityModel>({ name: "" });
const [location, setLocation] =
useState<AssetLocationObject>(initialLocation);
const [asset_type, setAssetType] = useState<string>(
filter.asset_type ? filter.asset_type : ""
);
Expand All @@ -58,28 +46,14 @@ function AssetFilter(props: any) {
prefetch: !!facilityId,
});

useQuery(routes.getFacilityAssetLocation, {
pathParams: {
facilityId: String(facilityId),
locationId: String(locationId),
},
onResponse: ({ res, data }) => {
if (res?.status === 200 && data) {
setLocation(data);
}
},
prefetch: !!(facilityId && locationId),
});

useEffect(() => {
setFacilityId(facility?.id ? `${facility?.id}` : "");
setLocationId(
facility?.id === qParams.facility ? qParams.location ?? "" : ""
);
}, [facility, location]);
}, [facility.id, qParams.facility, qParams.location]);

const clearFilter = useCallback(() => {
setLocation(initialLocation);
setFacility({ name: "" });
setAssetType("");
setAssetStatus("");
Expand All @@ -98,7 +72,7 @@ function AssetFilter(props: any) {
asset_type: asset_type ?? "",
asset_class: asset_class ?? "",
status: asset_status ?? "",
location: locationId,
location: locationId ?? "",
warranty_amc_end_of_validity_before: dateQueryString(
warrantyExpiry.before
),
Expand Down
18 changes: 12 additions & 6 deletions src/Components/Assets/AssetsList.tsx
Pranshu1902 marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,6 @@ const AssetsList = () => {
const [facility, setFacility] = useState<FacilityModel>();
const [asset_type, setAssetType] = useState<string>();
const [status, setStatus] = useState<string>();
const [facilityName, setFacilityName] = useState<string>();
const [asset_class, setAssetClass] = useState<string>();
const [importAssetModalOpen, setImportAssetModalOpen] = useState(false);
const assetsExist = assets.length > 0 && Object.keys(assets[0]).length > 0;
Expand Down Expand Up @@ -76,13 +75,12 @@ const AssetsList = () => {
},
});

useQuery(routes.getAnyFacility, {
const { data: facilityObject } = useQuery(routes.getAnyFacility, {
pathParams: { id: qParams.facility },
onResponse: ({ res, data }) => {
if (res?.status === 200 && data) {
setFacility(data);
setSelectedFacility(data);
setFacilityName(data.name);
}
},
prefetch: !!qParams.facility,
Expand All @@ -100,7 +98,7 @@ const AssetsList = () => {
setAssetClass(qParams.asset_class);
}, [qParams.asset_class]);

const { data: location } = useQuery(routes.getFacilityAssetLocation, {
const { data: locationObject } = useQuery(routes.getFacilityAssetLocation, {
pathParams: {
facility_external_id: String(qParams.facility),
external_id: String(qParams.location),
Expand Down Expand Up @@ -365,12 +363,20 @@ const AssetsList = () => {
<>
<FilterBadges
badges={({ badge, value }) => [
value("Facility", "facility", facilityName ?? ""),
value(
"Facility",
"facility",
qParams.facility && facilityObject?.name
),
badge("Name/Serial No./QR ID", "search"),
value("Asset Type", "asset_type", asset_type ?? ""),
value("Asset Class", "asset_class", asset_class ?? ""),
value("Status", "status", status?.replace(/_/g, " ") ?? ""),
value("Location", "location", location?.name ?? ""),
value(
"Location",
"location",
qParams.facility && locationObject?.name
rithviknishad marked this conversation as resolved.
Show resolved Hide resolved
),
value(
"Warranty AMC End Of Validity Before",
"warranty_amc_end_of_validity_before",
Expand Down
Loading