From b9be8a711a24f3c3ed7509bd0d94b531342837ad Mon Sep 17 00:00:00 2001 From: knom Date: Wed, 1 Jan 2025 18:41:45 +0000 Subject: [PATCH 1/2] Remove assets from list, that have been located --- src/pages/assets/missing-locations.tsx | 19 +++++++++++-------- 1 file changed, 11 insertions(+), 8 deletions(-) diff --git a/src/pages/assets/missing-locations.tsx b/src/pages/assets/missing-locations.tsx index 7a3b30e..e475fb7 100644 --- a/src/pages/assets/missing-locations.tsx +++ b/src/pages/assets/missing-locations.tsx @@ -29,17 +29,20 @@ export default function MissingLocations() { const selectedAssets = useMemo(() => config.assets.filter((a) => config.selectedIds.includes(a.id)), [config.assets, config.selectedIds]) ; - const handleSubmit = (place: IPlace) => { - return updateAssets({ + const handleSubmit = async (place: IPlace) => { + await updateAssets({ ids: config.selectedIds, latitude: place.latitude, longitude: place.longitude, - }).then(() => { - setConfig({ - ...config, - selectedIds: [], - }); - }) + }); + + const newAssets = config.assets.filter(asset => !config.selectedIds.includes(asset.id)); + + setConfig({ + ...config, + selectedIds: [], + assets: newAssets + }); }; return ( From 80d2cce1ab261ddf65517d9aef25b0151e3f26f2 Mon Sep 17 00:00:00 2001 From: knom Date: Wed, 1 Jan 2025 20:14:49 +0000 Subject: [PATCH 2/2] update date records, when locations are assiigned to assets --- .../missing-location/MissingLocationDates.tsx | 11 ++++------- src/contexts/MissingLocationContext.tsx | 3 +++ src/pages/assets/missing-locations.tsx | 18 ++++++++++++++++++ 3 files changed, 25 insertions(+), 7 deletions(-) diff --git a/src/components/assets/missing-location/MissingLocationDates.tsx b/src/components/assets/missing-location/MissingLocationDates.tsx index d7c6fad..28fa58e 100644 --- a/src/components/assets/missing-location/MissingLocationDates.tsx +++ b/src/components/assets/missing-location/MissingLocationDates.tsx @@ -9,22 +9,19 @@ import { Button } from "@/components/ui/button"; import { SortAsc, SortDesc } from "lucide-react"; export default function MissingLocationDates() { - const { updateContext } = useMissingLocationContext(); + const { dates, updateContext } = useMissingLocationContext(); const router = useRouter(); - const [dateRecords, setDateRecords] = React.useState< - IMissingLocationDatesResponse[] - >([]); const [filters, setFilters] = useState<{ sortBy: string, sortOrder: string }>({ sortBy: "date", sortOrder: "desc" }); const [loading, setLoading] = useState(false); const [errorMessage, setErrorMessage] = useState(null); - const fetchData = async () => { + const fetchData = () => { return listMissingLocationDates({ sortBy: filters.sortBy, sortOrder: filters.sortOrder, }) - .then(setDateRecords) + .then((r) => updateContext({dates: r})) .catch(setErrorMessage) .finally(() => setLoading(false)); }; @@ -68,7 +65,7 @@ export default function MissingLocationDates() { - {dateRecords.map((record) => ( + {dates.map((record) => ( ({ startDate: undefined, + dates: [], selectedIds: [], assets: [], updateContext: () => { }, diff --git a/src/pages/assets/missing-locations.tsx b/src/pages/assets/missing-locations.tsx index e475fb7..b992b3c 100644 --- a/src/pages/assets/missing-locations.tsx +++ b/src/pages/assets/missing-locations.tsx @@ -13,6 +13,7 @@ import MissingLocationContext, { import { updateAssets } from "@/handlers/api/asset.handler"; import { IPlace } from "@/types/common"; +import { isSameDay } from "date-fns"; import { useRouter } from "next/router"; import React, { useMemo } from "react"; @@ -25,6 +26,7 @@ export default function MissingLocations() { startDate: startDate || undefined, selectedIds: [], assets: [], + dates: [] }); const selectedAssets = useMemo(() => config.assets.filter((a) => config.selectedIds.includes(a.id)), [config.assets, config.selectedIds]) ; @@ -38,6 +40,22 @@ export default function MissingLocations() { const newAssets = config.assets.filter(asset => !config.selectedIds.includes(asset.id)); + if (config.startDate) { + const dayRecord = config.dates.filter(f => isSameDay(new Date(f.date), new Date(config.startDate!))); + + if (dayRecord.length === 1) { + if (newAssets.length > 0) + dayRecord[0].asset_count = newAssets.length; + else { + const indexToRemove = config.dates.findIndex(v=>isSameDay(v.date, dayRecord[0].date)); + + if (indexToRemove !== -1) { + config.dates.splice(indexToRemove, 1); + } + } + } + } + setConfig({ ...config, selectedIds: [],