Skip to content

Commit

Permalink
more
Browse files Browse the repository at this point in the history
  • Loading branch information
erich2s committed Oct 31, 2023
1 parent 63012bc commit 47decbf
Showing 1 changed file with 34 additions and 37 deletions.
71 changes: 34 additions & 37 deletions src/hooks/useUncompletedReports.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -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 {
Expand Down

0 comments on commit 47decbf

Please sign in to comment.