feat: 리포트 타입 에러 수정
This commit is contained in:
@@ -1,24 +1,41 @@
|
||||
import { useState, useEffect } from "react";
|
||||
import { useState, useEffect, useCallback, useRef } from "react";
|
||||
import { ReportMaster, GetReportsParams } from "@/types/report";
|
||||
import { reportApi } from "@/lib/api/reportApi";
|
||||
import { useToast } from "@/hooks/use-toast";
|
||||
|
||||
type SearchField = "report_name" | "created_by" | "report_type" | "updated_at" | "created_at";
|
||||
|
||||
export function useReportList() {
|
||||
const [reports, setReports] = useState<ReportMaster[]>([]);
|
||||
const [total, setTotal] = useState(0);
|
||||
const [typeSummary, setTypeSummary] = useState<Array<{ type: string; count: number }>>([]);
|
||||
const [allTypes, setAllTypes] = useState<string[]>([]);
|
||||
const [recentActivity, setRecentActivity] = useState<Array<{ date: string; count: number }>>([]);
|
||||
const [recentTotal, setRecentTotal] = useState(0);
|
||||
const [page, setPage] = useState(1);
|
||||
const [limit] = useState(20);
|
||||
const [limit, setLimit] = useState(8);
|
||||
const [isLoading, setIsLoading] = useState(false);
|
||||
const [searchText, setSearchText] = useState("");
|
||||
const [searchField, setSearchField] = useState<SearchField>("report_name");
|
||||
const [startDate, setStartDate] = useState("");
|
||||
const [endDate, setEndDate] = useState("");
|
||||
const [reportType, setReportType] = useState("");
|
||||
const { toast } = useToast();
|
||||
const toastRef = useRef(toast);
|
||||
toastRef.current = toast;
|
||||
|
||||
const fetchReports = async () => {
|
||||
const fetchReports = useCallback(async () => {
|
||||
setIsLoading(true);
|
||||
try {
|
||||
const isDateSearch = searchField === "created_at" || searchField === "updated_at";
|
||||
const params: GetReportsParams = {
|
||||
page,
|
||||
limit,
|
||||
searchText,
|
||||
searchText: isDateSearch ? undefined : searchText || undefined,
|
||||
searchField: (isDateSearch && startDate && endDate) || searchText ? searchField : undefined,
|
||||
startDate: isDateSearch && startDate ? startDate : undefined,
|
||||
endDate: isDateSearch && endDate ? endDate : undefined,
|
||||
reportType: reportType || undefined,
|
||||
useYn: "Y",
|
||||
sortBy: "created_at",
|
||||
sortOrder: "DESC",
|
||||
@@ -29,37 +46,67 @@ export function useReportList() {
|
||||
if (response.success && response.data) {
|
||||
setReports(response.data.items);
|
||||
setTotal(response.data.total);
|
||||
setTypeSummary(response.data.typeSummary ?? []);
|
||||
setAllTypes(response.data.allTypes ?? []);
|
||||
setRecentActivity(response.data.recentActivity ?? []);
|
||||
setRecentTotal(response.data.recentTotal ?? 0);
|
||||
}
|
||||
} catch (error: any) {
|
||||
console.error("리포트 목록 조회 에러:", error);
|
||||
toast({
|
||||
title: "오류",
|
||||
description: error.message || "리포트 목록을 불러오는데 실패했습니다.",
|
||||
variant: "destructive",
|
||||
});
|
||||
} catch (error: unknown) {
|
||||
const msg = error instanceof Error ? error.message : "리포트 목록을 불러오는데 실패했습니다.";
|
||||
toastRef.current({ title: "오류", description: msg, variant: "destructive" });
|
||||
} finally {
|
||||
setIsLoading(false);
|
||||
}
|
||||
};
|
||||
}, [page, limit, searchText, searchField, startDate, endDate, reportType]);
|
||||
|
||||
useEffect(() => {
|
||||
fetchReports();
|
||||
// eslint-disable-next-line react-hooks/exhaustive-deps
|
||||
}, [page, searchText]);
|
||||
}, [fetchReports]);
|
||||
|
||||
const handleSearch = (text: string) => {
|
||||
const handleSearch = useCallback((text: string) => {
|
||||
setSearchText(text);
|
||||
setPage(1);
|
||||
};
|
||||
}, []);
|
||||
|
||||
const handleSearchFieldChange = useCallback((field: SearchField) => {
|
||||
setSearchField(field);
|
||||
if (field !== "created_at" && field !== "updated_at") {
|
||||
setStartDate("");
|
||||
setEndDate("");
|
||||
}
|
||||
setPage(1);
|
||||
}, []);
|
||||
|
||||
const handleDateRangeChange = useCallback((start: string, end: string) => {
|
||||
setStartDate(start);
|
||||
setEndDate(end);
|
||||
setPage(1);
|
||||
}, []);
|
||||
|
||||
const handleTypeFilter = useCallback((type: string) => {
|
||||
setReportType(type);
|
||||
setPage(1);
|
||||
}, []);
|
||||
|
||||
return {
|
||||
reports,
|
||||
total,
|
||||
typeSummary,
|
||||
allTypes,
|
||||
recentActivity,
|
||||
recentTotal,
|
||||
page,
|
||||
limit,
|
||||
isLoading,
|
||||
searchField,
|
||||
startDate,
|
||||
endDate,
|
||||
refetch: fetchReports,
|
||||
setPage,
|
||||
setLimit,
|
||||
handleSearch,
|
||||
handleSearchFieldChange,
|
||||
handleDateRangeChange,
|
||||
handleTypeFilter,
|
||||
};
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user