diff --git a/view/next-project/src/components/budget_managements/BudgetManagement.tsx b/view/next-project/src/components/budget_managements/BudgetManagement.tsx index 129283e9f..229b70bbf 100644 --- a/view/next-project/src/components/budget_managements/BudgetManagement.tsx +++ b/view/next-project/src/components/budget_managements/BudgetManagement.tsx @@ -1,4 +1,4 @@ -import { useRouter } from 'next/router'; +import { useQueryStates, parseAsInteger } from 'nuqs'; import { useState, useEffect } from 'react'; import { Department, @@ -12,81 +12,48 @@ import { Card, EditButton, AddButton, Title } from '@/components/common'; import PrimaryButton from '@/components/common/OutlinePrimaryButton/OutlinePrimaryButton'; export default function BudgetManagement() { - const router = useRouter(); - const { departmentId, divisionId } = router.query; - const [departments, setDepartments] = useState([]); const [divisions, setDivisions] = useState([]); const [items, setItems] = useState([]); - const [selectedDepartmentId, setSelectedDepartmentId] = useState(departmentId || ''); - const [selectedDivisionId, setSelectedDivisionId] = useState(divisionId || ''); + const [{ departmentId, divisionId }, setQueryState] = useQueryStates({ + departmentId: parseAsInteger.withOptions({ history: 'push', shallow: true }), + divisionId: parseAsInteger.withOptions({ history: 'push', shallow: true }), + }); useEffect(() => { fetchDepartments().then(setDepartments); }, []); + // FIXME: APIが実装されたら、修正する。 useEffect(() => { - setSelectedDepartmentId(departmentId || ''); - }, [departmentId]); - - useEffect(() => { - setSelectedDivisionId(divisionId || ''); - }, [divisionId]); - - useEffect(() => { - if (selectedDepartmentId) { - fetchDivisions(Number(selectedDepartmentId)).then(setDivisions); + if (departmentId !== null) { + fetchDivisions(departmentId).then(setDivisions); setItems([]); } else { setDivisions([]); - setSelectedDivisionId(''); + setQueryState({ divisionId: null }); setItems([]); } - - router.push( - { - pathname: router.pathname, - query: { - ...router.query, - departmentId: selectedDepartmentId || undefined, - divisionId: undefined, - }, - }, - undefined, - { shallow: true }, - ); - }, [selectedDepartmentId]); + }, [departmentId]); useEffect(() => { - if (selectedDivisionId) { - fetchItems(Number(selectedDivisionId)).then(setItems); + if (divisionId !== null) { + fetchItems(divisionId).then(setItems); } else { setItems([]); } + }, [divisionId]); - router.push( - { - pathname: router.pathname, - query: { - ...router.query, - departmentId: selectedDepartmentId || undefined, - divisionId: selectedDivisionId || undefined, - }, - }, - undefined, - { shallow: true }, - ); - }, [selectedDivisionId]); - + // FIXME: any型はAPIのレスポンスに合わせて変更する。 let displayItems: any[] = []; let title = '購入報告'; const showBudgetColumns = true; - if (selectedDivisionId) { + if (divisionId !== null) { displayItems = items; title = '申請物品'; - } else if (selectedDepartmentId) { + } else if (departmentId !== null) { displayItems = divisions; title = '申請部門'; } else { @@ -99,22 +66,21 @@ export default function BudgetManagement() { const totalRemaining = displayItems.reduce((sum, item) => sum + (item.remaining || 0), 0); const handleDepartmentChange = (e: React.ChangeEvent) => { - const deptId = e.target.value; - setSelectedDepartmentId(deptId); - setSelectedDivisionId(''); + const deptId = e.target.value ? parseInt(e.target.value, 10) : null; + setQueryState({ departmentId: deptId, divisionId: null }); }; const handleDivisionChange = (e: React.ChangeEvent) => { - const divId = e.target.value; - setSelectedDivisionId(divId); + const divId = e.target.value ? parseInt(e.target.value, 10) : null; + setQueryState({ divisionId: divId }); }; + // FIXME: any型はAPIのレスポンスに合わせて変更する。 const handleRowClick = (item: any) => { - if (!selectedDepartmentId) { - setSelectedDepartmentId(item.id); - setSelectedDivisionId(''); - } else if (!selectedDivisionId) { - setSelectedDivisionId(item.id); + if (departmentId === null) { + setQueryState({ departmentId: item.id, divisionId: null }); + } else if (divisionId === null) { + setQueryState({ divisionId: item.id }); } }; @@ -125,11 +91,11 @@ export default function BudgetManagement() { 予算管理ページ
-
+
申請する局
-
+
申請する部門