260623_01_ENH: 엑셀 다운로드를 제외한 기능 완성
This commit is contained in:
@@ -1,10 +1,10 @@
|
||||
"use client";
|
||||
|
||||
import {useMemo, useState} from "react";
|
||||
import {useEffect, 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 {MonitoringPerformanceDetailDialog} from "@/components/dialog/MonitoringPerformanceDetailDialog";
|
||||
import {DataTable} from "@/components/data-table/DataTable";
|
||||
import {DataTableColumn} from "@/components/data-table/DataTable.type";
|
||||
import {usePerformanceDetailQuery, usePerformanceListQuery} from "@/query/performance";
|
||||
@@ -15,8 +15,54 @@ import {useAuth} from "@/hooks/useAuth";
|
||||
import {useDialog} from "@/hooks/useDialog";
|
||||
import {useMounted} from "@/hooks/useMounted";
|
||||
import {LoadingOverlay} from "@/components/common/LoadingOverlay";
|
||||
import {toLocalDateString} from "@/lib/gwcf/date";
|
||||
import {
|
||||
getMonitoringDisplayRow,
|
||||
} from "@/lib/gwcf/performance-status";
|
||||
|
||||
const today = new Date().toISOString().slice(0, 10);
|
||||
const today = toLocalDateString();
|
||||
const todayTime = new Date(`${today}T00:00:00`).getTime();
|
||||
|
||||
const toDateTime = (date?: string) => {
|
||||
if (!date) return Number.POSITIVE_INFINITY;
|
||||
const time = new Date(`${date}T00:00:00`).getTime();
|
||||
return Number.isNaN(time) ? Number.POSITIVE_INFINITY : time;
|
||||
};
|
||||
|
||||
const compareMonitoringSchedule = (a: PerformanceFull, b: PerformanceFull) => {
|
||||
const aTime = toDateTime(a.performanceMonitoringSchedule?.monitoringDate);
|
||||
const bTime = toDateTime(b.performanceMonitoringSchedule?.monitoringDate);
|
||||
const aDistance = Math.abs(aTime - todayTime);
|
||||
const bDistance = Math.abs(bTime - todayTime);
|
||||
|
||||
if (aDistance !== bDistance) return aDistance - bDistance;
|
||||
|
||||
const aFuture = aTime >= todayTime;
|
||||
const bFuture = bTime >= todayTime;
|
||||
if (aFuture !== bFuture) return aFuture ? -1 : 1;
|
||||
|
||||
return a.performanceName.localeCompare(b.performanceName, "ko");
|
||||
};
|
||||
|
||||
const getGpsErrorMessage = (error: unknown) => {
|
||||
if (error && typeof error === "object" && "code" in error) {
|
||||
const code = Number((error as GeolocationPositionError).code);
|
||||
if (code === 1) return "GPS 권한이 거부되었습니다. 브라우저 위치 권한을 허용해 주세요.";
|
||||
if (code === 2) return "현재 위치를 확인할 수 없습니다. GPS 상태를 확인해 주세요.";
|
||||
if (code === 3) return "GPS 위치 확인 시간이 초과되었습니다. 다시 시도해 주세요.";
|
||||
}
|
||||
|
||||
return error instanceof Error ? error.message : "GPS 정보를 확인할 수 없습니다.";
|
||||
};
|
||||
|
||||
const emptyGps = (): MonitoringGpsRequest => ({
|
||||
gpsLatitude: null,
|
||||
gpsLongitude: null,
|
||||
gpsAccuracyM: null,
|
||||
gpsRaw: {
|
||||
unavailable: true,
|
||||
},
|
||||
});
|
||||
|
||||
const MonitoringPage = () => {
|
||||
const mounted = useMounted();
|
||||
@@ -35,23 +81,29 @@ const MonitoringPage = () => {
|
||||
void refetchPerformanceList();
|
||||
};
|
||||
|
||||
|
||||
const assignedItems = useMemo(() => {
|
||||
if (!userId) return [];
|
||||
return data.filter((item) => item.performanceAssignList?.some((assign) => assign.assignUser === userId));
|
||||
return data.filter((item) =>
|
||||
item.performanceAssignList?.some((assign) => assign.assignUser === userId && assign.assignConfirm)
|
||||
);
|
||||
}, [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;
|
||||
const sortedItems = useMemo(() => {
|
||||
return [...assignedItems].sort(compareMonitoringSchedule);
|
||||
}, [assignedItems]);
|
||||
|
||||
const selected = useMemo(() => {
|
||||
return todayItems.find((item) => item.id === selectedId) ?? todayItems[0] ?? null;
|
||||
}, [selectedId, todayItems]);
|
||||
return sortedItems.find((item) => item.id === selectedId) ?? sortedItems[0] ?? null;
|
||||
}, [selectedId, sortedItems]);
|
||||
|
||||
const selectedMonitoringRow = useMemo(() => {
|
||||
if (!selected || !userId) return undefined;
|
||||
return ([0, 1] as const)
|
||||
.map((index) => getMonitoringDisplayRow(selected, index))
|
||||
.find((row) => row.userId === userId);
|
||||
}, [selected, userId]);
|
||||
const hasDeparture = Boolean(selectedMonitoringRow?.slot.timeline.departure || selectedMonitoringRow?.slot.visit?.length);
|
||||
const hasArrival = Boolean(selectedMonitoringRow?.slot.timeline.arrival);
|
||||
|
||||
const columns = useMemo<DataTableColumn<PerformanceFull>[]>(() => [
|
||||
{
|
||||
@@ -112,17 +164,47 @@ const MonitoringPage = () => {
|
||||
});
|
||||
};
|
||||
|
||||
useEffect(() => {
|
||||
if (!mounted || !navigator.geolocation) return;
|
||||
navigator.geolocation.getCurrentPosition(
|
||||
() => undefined,
|
||||
() => undefined,
|
||||
{enableHighAccuracy: true, timeout: 10000, maximumAge: 0}
|
||||
);
|
||||
}, [mounted]);
|
||||
|
||||
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});
|
||||
if (visitMode === "arrival" && (!files || files.length === 0)) {
|
||||
alert("도착 저장 시 증빙사진을 선택해 주세요.");
|
||||
return;
|
||||
}
|
||||
|
||||
try {
|
||||
const gps = await getGps().catch((error) => {
|
||||
const fallback = emptyGps();
|
||||
return {
|
||||
...fallback,
|
||||
gpsRaw: {
|
||||
...fallback.gpsRaw,
|
||||
errorMessage: getGpsErrorMessage(error),
|
||||
},
|
||||
};
|
||||
});
|
||||
if (visitMode === "departure") {
|
||||
await departureMutation.mutateAsync({performanceId: selected.id, gps});
|
||||
alert("출발 정보가 저장되었습니다.");
|
||||
} else {
|
||||
await arrivalMutation.mutateAsync({performanceId: selected.id, gps, files});
|
||||
alert("도착 정보가 저장되었습니다.");
|
||||
}
|
||||
hideDialog("monitoringVisitDialog");
|
||||
refreshMonitoringList();
|
||||
} catch (error) {
|
||||
const message = error instanceof Error ? error.message : "저장 중 오류가 발생했습니다.";
|
||||
alert(message);
|
||||
}
|
||||
hideDialog("monitoringVisitDialog");
|
||||
refreshMonitoringList();
|
||||
};
|
||||
|
||||
if(!mounted) return <LoadingOverlay force={true} />
|
||||
@@ -136,7 +218,7 @@ const MonitoringPage = () => {
|
||||
setSelectedId(Number(value));
|
||||
refreshMonitoringList();
|
||||
}}
|
||||
options={todayItems.map((item) => ({
|
||||
options={sortedItems.map((item) => ({
|
||||
codeGroup: "performance",
|
||||
codeValue: item.id,
|
||||
codeGroupLabel: "사업",
|
||||
@@ -164,16 +246,21 @@ const MonitoringPage = () => {
|
||||
</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>
|
||||
{selected && (
|
||||
<div>
|
||||
{!hasDeparture ? (
|
||||
<Button type="button" className="w-full" onClick={() => openVisit("departure")}>출발</Button>
|
||||
) : (
|
||||
<Button type="button" className="w-full" disabled={hasArrival} onClick={() => openVisit("arrival")}>도착</Button>
|
||||
)}
|
||||
</div>
|
||||
)}
|
||||
|
||||
<section className="space-y-2 overflow-x-auto">
|
||||
<div className="text-sm font-semibold text-muted-foreground">오늘의 모니터링 목록</div>
|
||||
<div className="text-sm font-semibold text-muted-foreground">모니터링 목록</div>
|
||||
<DataTable
|
||||
columns={columns}
|
||||
data={todayItems}
|
||||
data={sortedItems}
|
||||
getRowId={(row) => String(row.id)}
|
||||
selectMode="click-event"
|
||||
onRowClick={(item) => {
|
||||
@@ -205,7 +292,7 @@ const MonitoringPage = () => {
|
||||
isSubmitting={departureMutation.isPending || arrivalMutation.isPending}
|
||||
/>
|
||||
|
||||
<PerformanceDetailDialog
|
||||
<MonitoringPerformanceDetailDialog
|
||||
open={detailOpen}
|
||||
onOpenChange={(open) => {
|
||||
if (open) {
|
||||
@@ -218,7 +305,7 @@ const MonitoringPage = () => {
|
||||
}}
|
||||
performance={detailData ?? selected}
|
||||
codes={codes}
|
||||
canEdit={false}
|
||||
userId={userId}
|
||||
/>
|
||||
</div>
|
||||
);
|
||||
|
||||
Reference in New Issue
Block a user