diff --git a/src/hooks/useUncompletedReports.ts b/src/hooks/useUncompletedReports.ts index d74086a..a3958ca 100644 --- a/src/hooks/useUncompletedReports.ts +++ b/src/hooks/useUncompletedReports.ts @@ -2,6 +2,15 @@ import { useEffect, useMemo, useState } from "react"; import useSWR from "swr"; import { ReportItemType } from "../../types/reportItemType"; +type LocationType = { + id: number; + name: string; + lat: number; + lon: number; + pendingCount: number; + acceptedCount: number; +}; + export default function useUncompletedReports() { const { data, isLoading, mutate } = useSWR( "/api/reports/getUnCompleted", @@ -19,44 +28,32 @@ export default function useUncompletedReports() { }, [data]); // 数据聚合 const locations = useMemo(() => { - const result = reports.reduce( - ( - acc: { - id: number; - name: string; - lat: number; - lon: number; - pendingCount: number; - acceptedCount: number; - }[], - report, - ) => { - const { location, status } = report; - const index = acc.findIndex((l) => l.id === location.id); - // 对于已经存在acc里的地点, - // 如果是PENDING,pendingCount+1, - // 如果是ACCEPTED,acceptedCount+1 - if (index !== -1) { - if (status === "PENDING") { - acc[index].pendingCount++; - } else if (status === "ACCEPTED") { - acc[index].acceptedCount++; - } - } else { - // 对于不存在的地点,新建一个地点对象 - acc.push({ - id: location.id, - name: location.name, - lat: location.latitude, - lon: location.longitude, - pendingCount: status === "PENDING" ? 1 : 0, - acceptedCount: status === "ACCEPTED" ? 1 : 0, - }); + const result = reports.reduce((acc: LocationType[], report) => { + const { location, status } = report; + // 查找是否已经存在acc里 + const index = acc.findIndex((l) => l.id === location.id); + // 对于已经存在acc里的地点, + // 如果是PENDING,pendingCount+1, + // 如果是ACCEPTED,acceptedCount+1 + if (index !== -1) { + if (status === "PENDING") { + acc[index].pendingCount++; + } else if (status === "ACCEPTED") { + acc[index].acceptedCount++; } - return acc; - }, - [], - ); + } else { + // 对于不存在的地点,新建一个地点对象 + acc.push({ + id: location.id, + name: location.name, + lat: location.latitude, + lon: location.longitude, + pendingCount: status === "PENDING" ? 1 : 0, + acceptedCount: status === "ACCEPTED" ? 1 : 0, + }); + } + return acc; + }, []); return result; }, [reports]); return {