fix: 카테고리 API를 React Query로 변경 (중복 호출 방지)
- useEffect + useState → useQuery로 변경 - staleTime 10분으로 캐시하여 중복 요청 제거 - 카테고리 색상 Redis 캐시도 삭제 (DB 변경 반영) Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This commit is contained in:
parent
48ed3bb9e0
commit
c14bd90e89
1 changed files with 18 additions and 21 deletions
|
|
@ -1,5 +1,6 @@
|
|||
import { useState, useEffect } from "react";
|
||||
import { useNavigate, Link } from "react-router-dom";
|
||||
import { useQuery } from "@tanstack/react-query";
|
||||
import { motion, AnimatePresence } from "framer-motion";
|
||||
import { Home, ChevronRight } from "lucide-react";
|
||||
import AdminLayout from "@/components/pc/admin/layout/Layout";
|
||||
|
|
@ -38,32 +39,28 @@ function ScheduleFormPage() {
|
|||
const navigate = useNavigate();
|
||||
const { user, isAuthenticated } = useAdminAuth();
|
||||
|
||||
const [categories, setCategories] = useState([]);
|
||||
const [selectedCategory, setSelectedCategory] = useState(null);
|
||||
const [loading, setLoading] = useState(true);
|
||||
const [isInitialLoad, setIsInitialLoad] = useState(true);
|
||||
|
||||
// 카테고리 로드
|
||||
useEffect(() => {
|
||||
if (!isAuthenticated) return;
|
||||
|
||||
const fetchCategories = async () => {
|
||||
try {
|
||||
const data = await categoriesApi.getCategories();
|
||||
setCategories(data);
|
||||
// 첫 번째 카테고리를 기본값으로
|
||||
if (data.length > 0) {
|
||||
setSelectedCategory(data[0].id);
|
||||
}
|
||||
} catch (error) {
|
||||
console.error("카테고리 로드 오류:", error);
|
||||
} finally {
|
||||
setLoading(false);
|
||||
// 카테고리 로드 (React Query)
|
||||
const { data: categories = [], isLoading: loading } = useQuery({
|
||||
queryKey: ["scheduleCategories"],
|
||||
queryFn: categoriesApi.getCategories,
|
||||
enabled: isAuthenticated,
|
||||
staleTime: 10 * 60 * 1000,
|
||||
onSuccess: (data) => {
|
||||
if (data.length > 0 && !selectedCategory) {
|
||||
setSelectedCategory(data[0].id);
|
||||
}
|
||||
};
|
||||
},
|
||||
});
|
||||
|
||||
fetchCategories();
|
||||
}, [isAuthenticated]);
|
||||
// 카테고리 로드 시 기본값 설정
|
||||
useEffect(() => {
|
||||
if (categories.length > 0 && !selectedCategory) {
|
||||
setSelectedCategory(categories[0].id);
|
||||
}
|
||||
}, [categories, selectedCategory]);
|
||||
|
||||
// 카테고리에 따른 폼 렌더링
|
||||
const renderForm = () => {
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue