260617_01_ENH: 배정 테스트를 위해 개발 중도 커밋
This commit is contained in:
@@ -1,9 +1,227 @@
|
||||
'use client'
|
||||
"use client";
|
||||
|
||||
import {useMemo, useState} from "react";
|
||||
import {Button} from "@/components/ui/button";
|
||||
import {FilterSelect} from "@/components/gwcf/FilterSelect";
|
||||
import {MonitoringVisitDialog} from "@/components/dialog/MonitoringVisitDialog";
|
||||
import {PerformanceDetailDialog} from "@/components/dialog/PerformanceDetailDialog";
|
||||
import {DataTable} from "@/components/data-table/DataTable";
|
||||
import {DataTableColumn} from "@/components/data-table/DataTable.type";
|
||||
import {usePerformanceDetailQuery, usePerformanceListQuery} from "@/query/performance";
|
||||
import {useCodesQuery} from "@/query/code";
|
||||
import {useSaveArrivalMutation, useSaveDepartureMutation} from "@/query/monitoring";
|
||||
import {MonitoringGpsRequest, PerformanceFull} from "@/types/gwcf";
|
||||
import {useAuth} from "@/hooks/useAuth";
|
||||
import {useDialog} from "@/hooks/useDialog";
|
||||
import {useMounted} from "@/hooks/useMounted";
|
||||
import {LoadingOverlay} from "@/components/common/LoadingOverlay";
|
||||
|
||||
const today = new Date().toISOString().slice(0, 10);
|
||||
|
||||
const MonitoringPage = () => {
|
||||
return (
|
||||
<div>MonitoringPage</div>
|
||||
);
|
||||
}
|
||||
const mounted = useMounted();
|
||||
const {auth} = useAuth();
|
||||
const {isDialogOpen, showDialog, hideDialog} = useDialog();
|
||||
const departureMutation = useSaveDepartureMutation();
|
||||
const arrivalMutation = useSaveArrivalMutation();
|
||||
const {data = [], refetch: refetchPerformanceList} = usePerformanceListQuery();
|
||||
const {data: codes} = useCodesQuery();
|
||||
const userId = auth.user?.userId;
|
||||
|
||||
export default MonitoringPage;
|
||||
const [selectedId, setSelectedId] = useState<number | null>(null);
|
||||
const [visitMode, setVisitMode] = useState<"departure" | "arrival">("departure");
|
||||
|
||||
const refreshMonitoringList = () => {
|
||||
void refetchPerformanceList();
|
||||
};
|
||||
|
||||
|
||||
const assignedItems = useMemo(() => {
|
||||
if (!userId) return [];
|
||||
return data.filter((item) => item.performanceAssignList?.some((assign) => assign.assignUser === userId));
|
||||
}, [data, userId]);
|
||||
|
||||
const todayItems = useMemo(() => {
|
||||
const matched = assignedItems
|
||||
.filter((item) => item.performanceMonitoringSchedule?.monitoringDate === today)
|
||||
.sort((a, b) => a.performanceName.localeCompare(b.performanceName, "ko"));
|
||||
|
||||
return matched.length > 0 ? matched : assignedItems;
|
||||
}, [assignedItems]);
|
||||
|
||||
const selected = useMemo(() => {
|
||||
return todayItems.find((item) => item.id === selectedId) ?? todayItems[0] ?? null;
|
||||
}, [selectedId, todayItems]);
|
||||
|
||||
const columns = useMemo<DataTableColumn<PerformanceFull>[]>(() => [
|
||||
{
|
||||
accessorKey: "performanceName",
|
||||
header: "사업명",
|
||||
cell: ({row}) => (
|
||||
<div className="max-w-[180px] whitespace-normal py-2 text-sm font-semibold leading-5">
|
||||
{row.original.performanceName}
|
||||
</div>
|
||||
),
|
||||
},
|
||||
{
|
||||
id: "monitoringDate",
|
||||
header: "일정",
|
||||
cell: ({row}) => row.original.performanceMonitoringSchedule?.monitoringDate?.slice(5) || "-",
|
||||
},
|
||||
{
|
||||
id: "monitoringTime",
|
||||
header: "시간",
|
||||
cell: ({row}) => row.original.performanceMonitoringSchedule?.monitoringTimeInfo || "-",
|
||||
},
|
||||
], []);
|
||||
|
||||
const visitOpen = isDialogOpen("monitoringVisitDialog");
|
||||
const detailOpen = isDialogOpen("monitoringDetailDialog");
|
||||
const {data: detailData} = usePerformanceDetailQuery(selected?.id ?? 0, Boolean(selected?.id && detailOpen));
|
||||
|
||||
const openVisit = (mode: "departure" | "arrival") => {
|
||||
setVisitMode(mode);
|
||||
showDialog("monitoringVisitDialog");
|
||||
};
|
||||
|
||||
const getGps = (): Promise<MonitoringGpsRequest> => {
|
||||
return new Promise((resolve, reject) => {
|
||||
if (!navigator.geolocation) {
|
||||
reject(new Error("GPS를 사용할 수 없습니다."));
|
||||
return;
|
||||
}
|
||||
|
||||
navigator.geolocation.getCurrentPosition(
|
||||
(position) => {
|
||||
const {latitude, longitude, accuracy} = position.coords;
|
||||
resolve({
|
||||
gpsLatitude: latitude,
|
||||
gpsLongitude: longitude,
|
||||
gpsAccuracyM: accuracy,
|
||||
gpsRaw: {
|
||||
latitude,
|
||||
longitude,
|
||||
accuracy,
|
||||
timestamp: position.timestamp,
|
||||
},
|
||||
});
|
||||
},
|
||||
(error) => reject(error),
|
||||
{enableHighAccuracy: true, timeout: 10000, maximumAge: 0}
|
||||
);
|
||||
});
|
||||
};
|
||||
|
||||
const saveVisit = async (files?: File[]) => {
|
||||
if (!selected) return;
|
||||
|
||||
const gps = await getGps();
|
||||
if (visitMode === "departure") {
|
||||
await departureMutation.mutateAsync({performanceId: selected.id, gps});
|
||||
} else {
|
||||
await arrivalMutation.mutateAsync({performanceId: selected.id, gps, files});
|
||||
}
|
||||
hideDialog("monitoringVisitDialog");
|
||||
refreshMonitoringList();
|
||||
};
|
||||
|
||||
if(!mounted) return <LoadingOverlay force={true} />
|
||||
|
||||
return (
|
||||
<div className="mx-auto min-h-[calc(100vh-160px)] max-w-[430px] bg-white shadow-sm sm:border">
|
||||
<main className="space-y-3 p-3">
|
||||
<FilterSelect
|
||||
value={selected ? String(selected.id) : ""}
|
||||
onChange={(value) => {
|
||||
setSelectedId(Number(value));
|
||||
refreshMonitoringList();
|
||||
}}
|
||||
options={todayItems.map((item) => ({
|
||||
codeGroup: "performance",
|
||||
codeValue: item.id,
|
||||
codeGroupLabel: "사업",
|
||||
codeLabel: item.performanceName,
|
||||
memo: "",
|
||||
}))}
|
||||
placeholder="모니터링 사업 선택"
|
||||
className="w-full"
|
||||
useClear={false}
|
||||
/>
|
||||
|
||||
{selected && (
|
||||
<section className="rounded-md border">
|
||||
<div className="border-b bg-muted px-3 py-2 text-center font-semibold">
|
||||
{selected.performanceMonitoringSchedule?.monitoringDate} {selected.performanceMonitoringSchedule?.monitoringTimeInfo}
|
||||
</div>
|
||||
<div className="grid grid-cols-[80px_1fr] border-b">
|
||||
<div className="bg-muted px-3 py-2 text-sm font-semibold">사업명</div>
|
||||
<div className="px-3 py-2 text-sm">{selected.performanceName}</div>
|
||||
</div>
|
||||
<div className="grid grid-cols-[80px_1fr]">
|
||||
<div className="bg-muted px-3 py-2 text-sm font-semibold">장소</div>
|
||||
<div className="px-3 py-2 text-sm">{selected.performanceMonitoringSchedule?.monitoringAddress}</div>
|
||||
</div>
|
||||
</section>
|
||||
)}
|
||||
|
||||
<div className="grid grid-cols-2 gap-2">
|
||||
<Button type="button" disabled={!selected} onClick={() => openVisit("departure")}>출발</Button>
|
||||
<Button type="button" disabled={!selected} variant="outline" onClick={() => openVisit("arrival")}>도착</Button>
|
||||
</div>
|
||||
|
||||
<section className="space-y-2 overflow-x-auto">
|
||||
<div className="text-sm font-semibold text-muted-foreground">오늘의 모니터링 목록</div>
|
||||
<DataTable
|
||||
columns={columns}
|
||||
data={todayItems}
|
||||
getRowId={(row) => String(row.id)}
|
||||
selectMode="click-event"
|
||||
onRowClick={(item) => {
|
||||
setSelectedId(item.id);
|
||||
showDialog("monitoringDetailDialog");
|
||||
}}
|
||||
highlightRowIds={selected ? [String(selected.id)] : []}
|
||||
defaultPageSize={10}
|
||||
pageSizeOptions={[10, 20, 30]}
|
||||
paginationItemsToDisplay={3}
|
||||
/>
|
||||
</section>
|
||||
</main>
|
||||
|
||||
<MonitoringVisitDialog
|
||||
open={visitOpen}
|
||||
onOpenChange={(open) => {
|
||||
if (open) {
|
||||
showDialog("monitoringVisitDialog");
|
||||
return;
|
||||
}
|
||||
|
||||
hideDialog("monitoringVisitDialog");
|
||||
refreshMonitoringList();
|
||||
}}
|
||||
performance={selected}
|
||||
mode={visitMode}
|
||||
onSubmit={saveVisit}
|
||||
isSubmitting={departureMutation.isPending || arrivalMutation.isPending}
|
||||
/>
|
||||
|
||||
<PerformanceDetailDialog
|
||||
open={detailOpen}
|
||||
onOpenChange={(open) => {
|
||||
if (open) {
|
||||
showDialog("monitoringDetailDialog");
|
||||
return;
|
||||
}
|
||||
|
||||
hideDialog("monitoringDetailDialog");
|
||||
refreshMonitoringList();
|
||||
}}
|
||||
performance={detailData ?? selected}
|
||||
codes={codes}
|
||||
canEdit={false}
|
||||
/>
|
||||
</div>
|
||||
);
|
||||
};
|
||||
|
||||
export default MonitoringPage;
|
||||
|
||||
Reference in New Issue
Block a user