From cfed6dce41d8090986be1e54261034a2339a26dd Mon Sep 17 00:00:00 2001 From: leeJooHaem Date: Fri, 24 Feb 2023 09:27:47 +0900 Subject: [PATCH] [ refactor ] seperate filter tag value with custom hook --- .../FilterModal/@hooks/useFilterTags.ts | 32 +++++++++++++++++++ .../FilterModal/IntimacySlider/index.tsx | 8 +++-- .../CardCollectionPage/FilterModal/index.tsx | 32 +++---------------- 3 files changed, 41 insertions(+), 31 deletions(-) create mode 100644 src/@components/CardCollectionPage/FilterModal/@hooks/useFilterTags.ts diff --git a/src/@components/CardCollectionPage/FilterModal/@hooks/useFilterTags.ts b/src/@components/CardCollectionPage/FilterModal/@hooks/useFilterTags.ts new file mode 100644 index 00000000..2dbf6ab4 --- /dev/null +++ b/src/@components/CardCollectionPage/FilterModal/@hooks/useFilterTags.ts @@ -0,0 +1,32 @@ +import { useEffect } from "react"; +import { useRecoilState } from "recoil"; + +import { filterTagsState } from "../../../../core/atom/slider"; + +export default function useFilterTags() { + const [filterTags, setFilterTags] = useRecoilState(filterTagsState); + + useEffect(() => { + if (!filterTags.isActive) setFilterTags({ tags: new Set(), intimacy: [0], isActive: false }); + // eslint-disable-next-line react-hooks/exhaustive-deps + }, [filterTags.isActive]); + + // 태그를 눌렀을 때 함수 + const toggleTag = (_tag: string) => { + if (_tag === "19금") return; + + setFilterTags((prevFilterTags) => { + const tempCheckedTags = new Set(filterTags.tags); + tempCheckedTags.has(_tag) ? tempCheckedTags.delete(_tag) : tempCheckedTags.add(_tag); + return { ...prevFilterTags, tags: tempCheckedTags }; + }); + }; + + const handleChangeIntimacyValue = (values: number[]) => { + setFilterTags((prevFilterTags) => { + return { ...prevFilterTags, intimacy: values }; + }); + }; + + return { filterTags, toggleTag, handleChangeIntimacyValue }; +} diff --git a/src/@components/CardCollectionPage/FilterModal/IntimacySlider/index.tsx b/src/@components/CardCollectionPage/FilterModal/IntimacySlider/index.tsx index e95f1fd0..719bab0b 100644 --- a/src/@components/CardCollectionPage/FilterModal/IntimacySlider/index.tsx +++ b/src/@components/CardCollectionPage/FilterModal/IntimacySlider/index.tsx @@ -1,9 +1,9 @@ import { Range } from "react-range"; +import useFilterTags from "../@hooks/useFilterTags"; import St from "./style"; interface IntimacySliderProps { price: number[]; - onChange: (values: number[]) => void; } const rangeOption = { @@ -13,7 +13,9 @@ const rangeOption = { }; export default function IntimacySlider(props: IntimacySliderProps) { - const { price, onChange } = props; + const { price } = props; + + const { handleChangeIntimacyValue } = useFilterTags(); return ( @@ -22,7 +24,7 @@ export default function IntimacySlider(props: IntimacySliderProps) { min={rangeOption.min} max={rangeOption.max} values={price} - onChange={onChange} + onChange={handleChangeIntimacyValue} renderTrack={({ props, children }) => ( {children} diff --git a/src/@components/CardCollectionPage/FilterModal/index.tsx b/src/@components/CardCollectionPage/FilterModal/index.tsx index e4324976..eb56c3b9 100644 --- a/src/@components/CardCollectionPage/FilterModal/index.tsx +++ b/src/@components/CardCollectionPage/FilterModal/index.tsx @@ -1,10 +1,9 @@ -import React, { useEffect } from "react"; -import { useRecoilState } from "recoil"; +import React from "react"; -import { filterTagsState } from "../../../core/atom/slider"; import { filterTagsInfo, intimacyTags } from "../../../core/cardCollection/filter"; import { GTM_CLASS_NAME } from "../../../util/const/gtm"; import Modal from "../../@common/Modal"; +import useFilterTags from "./@hooks/useFilterTags"; import IntimacySlider from "./IntimacySlider"; import St from "./style"; @@ -16,23 +15,7 @@ interface FilterModalProps { export default function FilterModal(props: FilterModalProps) { const { closeHandler, fetchCardListsWithFilter } = props; - const [filterTags, setFilterTags] = useRecoilState(filterTagsState); - - useEffect(() => { - if (!filterTags.isActive) setFilterTags({ tags: new Set(), intimacy: [0], isActive: false }); - // eslint-disable-next-line react-hooks/exhaustive-deps - }, [filterTags.isActive]); - - // 태그를 눌렀을 때 함수 - const toggleTag = (_tag: string) => { - if (_tag === "19금") return; - - setFilterTags((prevFilterTags) => { - const tempCheckedTags = new Set(filterTags.tags); - tempCheckedTags.has(_tag) ? tempCheckedTags.delete(_tag) : tempCheckedTags.add(_tag); - return { ...prevFilterTags, tags: tempCheckedTags }; - }); - }; + const { filterTags, toggleTag } = useFilterTags(); // 추천 시작하기를 눌렀을 때, 태그 정보들과 친밀도 정보를 담아주고 창닫기 const submitFilter = async (e: React.MouseEvent) => { @@ -64,14 +47,7 @@ export default function FilterModal(props: FilterModalProps) { 친밀도 - { - setFilterTags((prevFilterTags) => { - return { ...prevFilterTags, intimacy: values }; - }); - }} - /> + {intimacyTags.map((tag, index) => (