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

Feature: Update UI after location assigned (Missing-locations) #103

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 4 additions & 7 deletions src/components/assets/missing-location/MissingLocationDates.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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<string | null>(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));
};
Expand Down Expand Up @@ -68,7 +65,7 @@ export default function MissingLocationDates() {
</div>
</div>

{dateRecords.map((record) => (
{dates.map((record) => (
<MissingLocationDateItem
key={record.date}
record={record}
Expand Down
3 changes: 3 additions & 0 deletions src/contexts/MissingLocationContext.tsx
Original file line number Diff line number Diff line change
@@ -1,17 +1,20 @@
import { IMissingLocationDatesResponse } from "@/handlers/api/asset.handler";
import { IAsset } from "@/types/asset";
import { createContext, useContext } from "react";

export interface IMissingLocationConfig {
startDate?: string;
selectedIds: string[];
assets: IAsset[];
dates: IMissingLocationDatesResponse[];
}

export interface MissingLocationContext extends IMissingLocationConfig {
updateContext: (newConfig: Partial<MissingLocationContext>) => void;
}
const MissingLocationContext = createContext<MissingLocationContext>({
startDate: undefined,
dates: [],
selectedIds: [],
assets: [],
updateContext: () => { },
Expand Down
37 changes: 29 additions & 8 deletions src/pages/assets/missing-locations.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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";

Expand All @@ -25,21 +26,41 @@ 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]) ;

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));

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: [],
assets: newAssets
});
};

return (
Expand Down
Loading