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 { useState, useEffect } from "react";
|
||||||
import { useNavigate, Link } from "react-router-dom";
|
import { useNavigate, Link } from "react-router-dom";
|
||||||
|
import { useQuery } from "@tanstack/react-query";
|
||||||
import { motion, AnimatePresence } from "framer-motion";
|
import { motion, AnimatePresence } from "framer-motion";
|
||||||
import { Home, ChevronRight } from "lucide-react";
|
import { Home, ChevronRight } from "lucide-react";
|
||||||
import AdminLayout from "@/components/pc/admin/layout/Layout";
|
import AdminLayout from "@/components/pc/admin/layout/Layout";
|
||||||
|
|
@ -38,32 +39,28 @@ function ScheduleFormPage() {
|
||||||
const navigate = useNavigate();
|
const navigate = useNavigate();
|
||||||
const { user, isAuthenticated } = useAdminAuth();
|
const { user, isAuthenticated } = useAdminAuth();
|
||||||
|
|
||||||
const [categories, setCategories] = useState([]);
|
|
||||||
const [selectedCategory, setSelectedCategory] = useState(null);
|
const [selectedCategory, setSelectedCategory] = useState(null);
|
||||||
const [loading, setLoading] = useState(true);
|
|
||||||
const [isInitialLoad, setIsInitialLoad] = useState(true);
|
const [isInitialLoad, setIsInitialLoad] = useState(true);
|
||||||
|
|
||||||
// 카테고리 로드
|
// 카테고리 로드 (React Query)
|
||||||
useEffect(() => {
|
const { data: categories = [], isLoading: loading } = useQuery({
|
||||||
if (!isAuthenticated) return;
|
queryKey: ["scheduleCategories"],
|
||||||
|
queryFn: categoriesApi.getCategories,
|
||||||
const fetchCategories = async () => {
|
enabled: isAuthenticated,
|
||||||
try {
|
staleTime: 10 * 60 * 1000,
|
||||||
const data = await categoriesApi.getCategories();
|
onSuccess: (data) => {
|
||||||
setCategories(data);
|
if (data.length > 0 && !selectedCategory) {
|
||||||
// 첫 번째 카테고리를 기본값으로
|
setSelectedCategory(data[0].id);
|
||||||
if (data.length > 0) {
|
|
||||||
setSelectedCategory(data[0].id);
|
|
||||||
}
|
|
||||||
} catch (error) {
|
|
||||||
console.error("카테고리 로드 오류:", error);
|
|
||||||
} finally {
|
|
||||||
setLoading(false);
|
|
||||||
}
|
}
|
||||||
};
|
},
|
||||||
|
});
|
||||||
|
|
||||||
fetchCategories();
|
// 카테고리 로드 시 기본값 설정
|
||||||
}, [isAuthenticated]);
|
useEffect(() => {
|
||||||
|
if (categories.length > 0 && !selectedCategory) {
|
||||||
|
setSelectedCategory(categories[0].id);
|
||||||
|
}
|
||||||
|
}, [categories, selectedCategory]);
|
||||||
|
|
||||||
// 카테고리에 따른 폼 렌더링
|
// 카테고리에 따른 폼 렌더링
|
||||||
const renderForm = () => {
|
const renderForm = () => {
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue