-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: 식품 필터 선택 후 선택한 필터 보여주는 기능 추가 (#461)
* feat/#460: 반려견 등록 화살표 삭제 * feat/#460: 선택한 필터 보여줄 때 난독화돼서 나오지 않도록 url복호화 * refactor/#460: 파일 이동 * feat/#460: 선택한 필터 목록 보여주는 기능 추가 * refactor/#460: 컴포넌트명 수정 * refactor/#460: 컴포넌트명 수정 * fix/#460: 잘못된 지표 설명 수정 * refactor/#460: iPhone13 mini에서 도움말 줄바꿈 없이 보이도록 수정 * refactor: else if 문 개선 * refactor: 클릭이벤트핸들러 SelectedFilterItem으로 이동 * refactor: string배열 KEYWORD_EN으로 대체 * refactor: 타입명과 일치하도록 category > keyword로 변수명 수정 * feat: Object.entries 타입 추론을 위한 오버로딩 타입 추가 * refactor: 쿼리스트링에 따라 필터 상태 업데이트 하도록 수정
- Loading branch information
1 parent
1d8206f
commit a59c14a
Showing
13 changed files
with
228 additions
and
105 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
73 changes: 73 additions & 0 deletions
73
frontend/src/components/Food/FilterSelectionDisplay/FilterSelectionDisplay.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
import { styled } from 'styled-components'; | ||
|
||
import { useFilterSelectionDisplay } from '@/hooks/food/useFilterSelectionDisplay'; | ||
import { invariantOf } from '@/utils/invariantOf'; | ||
|
||
const FilterSelectionDisplay = () => { | ||
const { filterListQueryString, removeFilter } = useFilterSelectionDisplay(); | ||
|
||
return ( | ||
<SelectedFilterList> | ||
{Object.entries(invariantOf(filterListQueryString)).map(([keyword, values]) => | ||
values?.split(',').map(value => ( | ||
<SelectedFilterItem key={value} onClick={() => removeFilter(keyword, value)}> | ||
{value} | ||
<FilterToggleButton type="button" aria-label={`${value}필터 선택 해제`}> | ||
x | ||
</FilterToggleButton> | ||
</SelectedFilterItem> | ||
)), | ||
)} | ||
</SelectedFilterList> | ||
); | ||
}; | ||
|
||
export default FilterSelectionDisplay; | ||
|
||
const SelectedFilterList = styled.ul` | ||
scrollbar-width: none; | ||
overflow-x: scroll; | ||
display: flex; | ||
gap: 0.4rem; | ||
align-items: center; | ||
width: calc(100% - 8.8rem - 1rem); | ||
margin-left: 1rem; | ||
&::-webkit-scrollbar { | ||
width: 0; | ||
height: 0; | ||
} | ||
`; | ||
|
||
const SelectedFilterItem = styled.li` | ||
cursor: pointer; | ||
overflow: hidden; | ||
flex-shrink: 0; | ||
height: 3.2rem; | ||
padding: 0.4rem; | ||
font-size: 1.2rem; | ||
font-weight: 500; | ||
line-height: 2.4rem; | ||
color: ${({ theme }) => theme.color.grey400}; | ||
text-align: center; | ||
text-overflow: ellipsis; | ||
white-space: nowrap; | ||
`; | ||
|
||
const FilterToggleButton = styled.button` | ||
cursor: pointer; | ||
display: inline-block; | ||
margin-left: 0.4rem; | ||
color: ${({ theme }) => theme.color.grey300}; | ||
background: none; | ||
border: none; | ||
`; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
import { KEYWORD_EN } from '@/constants/food'; | ||
import { generateQueryString } from '@/router/routes'; | ||
import { KeywordEn } from '@/types/food/client'; | ||
|
||
import useEasyNavigate from '../@common/useEasyNavigate'; | ||
import useValidQueryString from '../common/useValidQueryString'; | ||
|
||
export const useFilterSelectionDisplay = () => { | ||
const { replaceQueryString } = useEasyNavigate(); | ||
const filterListQueryString = useValidQueryString(KEYWORD_EN); | ||
|
||
const removeFilter = (keyword: KeywordEn, value: string) => { | ||
const filterList = filterListQueryString[keyword]?.split(','); | ||
|
||
if (!filterList) return; | ||
|
||
if (filterList.includes(value)) { | ||
filterList.splice(filterList.indexOf(value), 1); | ||
|
||
const updatedQueryString = filterList.join(','); | ||
const newQueryString = generateQueryString({ | ||
...filterListQueryString, | ||
[keyword]: updatedQueryString, | ||
}); | ||
|
||
replaceQueryString(newQueryString, { exclude: [] }); | ||
} | ||
}; | ||
|
||
return { filterListQueryString, removeFilter }; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
import { useEffect, useState } from 'react'; | ||
|
||
import { initialSelectedFilterList } from '@/context/food'; | ||
import type { KeywordEn } from '@/types/food/client'; | ||
import { invariantOf } from '@/utils/invariantOf'; | ||
import { parseCheckList } from '@/utils/parseCheckList'; | ||
|
||
import { useFilterSelectionDisplay } from './useFilterSelectionDisplay'; | ||
|
||
export const useFoodListFilter = () => { | ||
const { filterListQueryString } = useFilterSelectionDisplay(); | ||
const [selectedFilterList, setSelectedFilterList] = useState(initialSelectedFilterList); | ||
const parsedSelectedFilterList = parseCheckList(selectedFilterList); | ||
|
||
const toggleFilter = (keyword: KeywordEn, filter: string) => { | ||
const targetFilterList = structuredClone(selectedFilterList)[keyword]; | ||
const selected = targetFilterList.has(filter); | ||
|
||
selected ? targetFilterList.delete(filter) : targetFilterList.add(filter); | ||
|
||
setSelectedFilterList(prev => ({ ...prev, [keyword]: new Set(targetFilterList) })); | ||
}; | ||
|
||
const resetSelectedFilterList = () => { | ||
setSelectedFilterList(initialSelectedFilterList); | ||
}; | ||
|
||
useEffect(() => { | ||
const newFilterList = Object.entries(invariantOf(filterListQueryString)).reduce( | ||
(newFilterList, [keyword, queryString]) => ({ | ||
...newFilterList, | ||
[keyword]: new Set(queryString?.split(',')), | ||
}), | ||
initialSelectedFilterList, | ||
); | ||
|
||
setSelectedFilterList(prev => newFilterList); | ||
// eslint-disable-next-line react-hooks/exhaustive-deps | ||
}, [Object.values(filterListQueryString).join()]); | ||
|
||
return { selectedFilterList, parsedSelectedFilterList, toggleFilter, resetSelectedFilterList }; | ||
}; |
Oops, something went wrong.