From ae17ff52c5f69b0a6ef37b10d988af491acabbdc Mon Sep 17 00:00:00 2001 From: "hyojun.choi" Date: Wed, 14 Aug 2024 15:59:38 +0900 Subject: [PATCH] =?UTF-8?q?=EC=A7=80=EB=B6=95=EC=9E=AC=20=EC=84=A0?= =?UTF-8?q?=ED=83=9D=20=EB=B3=B4=EC=99=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/app/[locale]/roof2/RoofSelect.jsx | 69 +++++++++++++++++++++++---- src/components/Roof2.jsx | 1 - 2 files changed, 60 insertions(+), 10 deletions(-) diff --git a/src/app/[locale]/roof2/RoofSelect.jsx b/src/app/[locale]/roof2/RoofSelect.jsx index 3d14d535..0dc5645d 100644 --- a/src/app/[locale]/roof2/RoofSelect.jsx +++ b/src/app/[locale]/roof2/RoofSelect.jsx @@ -6,23 +6,68 @@ import { useAxios } from '@/hooks/useAxios' export default function RoofSelect() { const [roofMaterials, setRoofMaterials] = useState([]) + const [manufacturers, setManufacturers] = useState([]) const [trestles, setTrestles] = useState([]) + const [originTrestles, setOriginTrestles] = useState([]) + + const [roofMaterialId, setRoofMaterialId] = useState(null) + const [manufacturerId, setManufacturerId] = useState(null) const { get } = useAxios() - const handleRoofMaterialOnChange = (e) => { - const id = e.target.value - setTrestles([]) - get({ url: `/api/trestle/v1.0/trestles/${id}` }).then((res) => { - setTrestles(res) - }) - } - useEffect(() => { get({ url: '/api/roof-material/v1.0/roof-materials' }).then((res) => { + if (res.length === 0) { + return + } setRoofMaterials(res) }) }, []) + + useEffect(() => { + if (!roofMaterialId) { + return + } + + get({ url: `/api/trestle/v1.0/trestles/${roofMaterialId}` }).then((res) => { + if (res.length === 0) { + return + } + setOriginTrestles(res) + const manufactural = res.map((trestle) => { + return { id: trestle.manufacturerId, name: trestle.manufacturerName } + }) + // Remove duplicates + const uniqueManufactural = Array.from(new Set(manufactural.map((a) => a.id))).map((id) => { + return manufactural.find((a) => a.id === id) + }) + + setManufacturers(uniqueManufactural) + }) + }, [roofMaterialId]) + + useEffect(() => { + if (!manufacturerId) { + return + } + + const trestles = originTrestles.filter((trestle) => trestle.manufacturerId === manufacturerId) + setTrestles(trestles) + }, [manufacturerId]) + + const handleRoofMaterialOnChange = (e) => { + const id = e.target.value + setRoofMaterialId(id) + setManufacturers([]) + setManufacturerId(null) + setTrestles([]) + } + + const handleManufacturersOnChange = (e) => { + const id = Number(e.target.value) + setTrestles([]) + setManufacturerId(id) + } return ( <>
@@ -33,7 +78,13 @@ export default function RoofSelect() { ))} )} - + {manufacturers.length > 0 && ( + + )} {trestles.length > 0 && (