|
| 1 | +import { XMarkIcon } from '@heroicons/react/24/outline'; |
| 2 | +import { MODAL_MESSAGE_CUSTOMER_SERVICE } from '../../../constants/user/customerService'; |
| 3 | +import * as S from './SearchBar.styled'; |
| 4 | +import { useState } from 'react'; |
| 5 | +import { useLocation, useSearchParams } from 'react-router-dom'; |
| 6 | +import { useModal } from '../../../hooks/useModal'; |
| 7 | +import Modal from '../../common/modal/Modal'; |
| 8 | +import { ADMIN_ROUTE } from '../../../constants/routes'; |
| 9 | + |
| 10 | +interface SearchBarProps { |
| 11 | + onGetKeyword: (keyword: string) => void; |
| 12 | + value: string; |
| 13 | +} |
| 14 | + |
| 15 | +export default function SearchBar({ onGetKeyword, value }: SearchBarProps) { |
| 16 | + const [inputValue, setInputValue] = useState<string>(''); |
| 17 | + const { isOpen, message, handleModalOpen, handleModalClose } = useModal(); |
| 18 | + const [searchParams, setSearchParams] = useSearchParams(); |
| 19 | + const location = useLocation(); |
| 20 | + const keyword = inputValue ? inputValue : value; |
| 21 | + |
| 22 | + const handleKeyword = (inputValue: string) => { |
| 23 | + const newSearchParams = new URLSearchParams(searchParams); |
| 24 | + if (inputValue === '') { |
| 25 | + newSearchParams.delete('keyword'); |
| 26 | + } else { |
| 27 | + newSearchParams.set('keyword', inputValue); |
| 28 | + } |
| 29 | + setSearchParams(newSearchParams); |
| 30 | + }; |
| 31 | + |
| 32 | + const handleSubmit = (e: React.FormEvent<HTMLFormElement>) => { |
| 33 | + e.preventDefault(); |
| 34 | + |
| 35 | + if (inputValue.trim() === '') { |
| 36 | + return handleModalOpen(MODAL_MESSAGE_CUSTOMER_SERVICE.noKeyword); |
| 37 | + } else { |
| 38 | + onGetKeyword(inputValue); |
| 39 | + handleKeyword(inputValue); |
| 40 | + return; |
| 41 | + } |
| 42 | + }; |
| 43 | + |
| 44 | + const handleChangeKeyword = (e: React.ChangeEvent<HTMLInputElement>) => { |
| 45 | + const value = e.target.value; |
| 46 | + setInputValue(value); |
| 47 | + }; |
| 48 | + |
| 49 | + const handleClickSearchDefault = () => { |
| 50 | + setInputValue(''); |
| 51 | + onGetKeyword(''); |
| 52 | + handleKeyword(''); |
| 53 | + }; |
| 54 | + |
| 55 | + return ( |
| 56 | + <S.AdminSearchBarContainer onSubmit={handleSubmit}> |
| 57 | + <S.AdminSearchBarWrapper> |
| 58 | + <S.AdminSearchBarInputWrapper> |
| 59 | + <S.AdminSearchBarInput |
| 60 | + placeholder={MODAL_MESSAGE_CUSTOMER_SERVICE.noKeyword} |
| 61 | + value={keyword} |
| 62 | + onChange={handleChangeKeyword} |
| 63 | + /> |
| 64 | + {keyword && ( |
| 65 | + <S.AdminSearchBarBackIcon |
| 66 | + type='button' |
| 67 | + aria-label='show all result' |
| 68 | + onClick={handleClickSearchDefault} |
| 69 | + > |
| 70 | + <XMarkIcon /> |
| 71 | + </S.AdminSearchBarBackIcon> |
| 72 | + )} |
| 73 | + </S.AdminSearchBarInputWrapper> |
| 74 | + <S.AdminSearchBarButton>검색</S.AdminSearchBarButton> |
| 75 | + </S.AdminSearchBarWrapper> |
| 76 | + <S.WriteLink to={ADMIN_ROUTE.write} state={{ form: location.pathname }}> |
| 77 | + 작성하기 |
| 78 | + </S.WriteLink> |
| 79 | + <Modal isOpen={isOpen} onClose={handleModalClose}> |
| 80 | + {message} |
| 81 | + </Modal> |
| 82 | + </S.AdminSearchBarContainer> |
| 83 | + ); |
| 84 | +} |
0 commit comments