'use client'; import * as React from 'react'; import PropTypes from 'prop-types'; import { useRouter } from 'next-nprogress-bar'; // mui import { alpha, styled } from '@mui/material/styles'; import Box from '@mui/material/Box'; import ListItemText from '@mui/material/ListItemText'; import Typography from '@mui/material/Typography'; import SearchIcon from '@mui/icons-material/Search'; import TextField from '@mui/material/TextField'; import Skeleton from '@mui/material/Skeleton'; import { InputAdornment, Stack, Button } from '@mui/material'; import MenuList from '@mui/material/MenuList'; import MenuItem from '@mui/material/MenuItem'; import ListItemIcon from '@mui/material/ListItemIcon'; import CircularProgress from '@mui/material/CircularProgress'; import Divider from '@mui/material/Divider'; // components import NoDataFound from 'src/illustrations/dataNotFound'; import { useMutation, useQuery } from 'react-query'; import BlurImageAvatar from '../../avatar'; import FormControl from '@mui/material/FormControl'; import Select from '@mui/material/Select'; import { useCurrencyConvert } from 'src/hooks/convertCurrency'; import { useCurrencyFormatter } from 'src/hooks/formatCurrency'; // api import * as api from 'src/services'; Search.propTypes = { onClose: PropTypes.func.isRequired, mobile: PropTypes.bool.isRequired }; const LabelStyle = styled(Typography)(({ theme }) => ({ ...theme.typography.body2, color: theme.palette.text.secondary, marginBottom: theme.spacing(1), fontSize: 12, fontWeight: 600, lineHeight: 1 })); export default function Search({ ...props }) { const { onClose, mobile, multiSelect, selectedProducts, handleSave } = props; const cCurrency = useCurrencyConvert(); const fCurrency = useCurrencyFormatter(); const [state, setstate] = React.useState({ products: [], selected: selectedProducts || [], initialized: false, category: '', subCategory: '', shop: '' }); const router = useRouter(); const [search, setSearch] = React.useState(''); const { data: filters, isLoading: filtersLoading } = useQuery(['get-search-filters'], () => api.getSearchFilters()); const { mutate, isLoading } = useMutation('search', api.search, { onSuccess: (data) => { setstate({ ...state, ...data }); } }); const [focus, setFocus] = React.useState(true); const handleListItemClick = (prop) => { if (multiSelect) { const matched = state.selected.filter((v) => prop._id === v._id); const notMatched = state.selected.filter((v) => prop._id !== v._id); if (Boolean(matched.length)) { setstate({ ...state, selected: notMatched }); } else { setstate({ ...state, selected: [...state.selected, prop] }); } } else { !mobile && onClose(prop); router.push(`/product/${prop}`); } }; const onKeyDown = (e) => { if (e.keyCode == '38' || e.keyCode == '40') { setFocus(false); } }; React.useEffect(() => { const delayDebounceFn = setTimeout(() => { mutate({ query: search, category: state.category, subCategory: state.subCategory, shop: state.shop }); }, 1000); return () => clearTimeout(delayDebounceFn); // eslint-disable-next-line react-hooks/exhaustive-deps }, [search]); React.useEffect(() => { mutate({ query: search, category: state.category, subCategory: state.subCategory, shop: state.shop }); // eslint-disable-next-line react-hooks/exhaustive-deps }, [state.category, state.subCategory, state.shop]); const fileInputRef = React.useRef(null); // 이미지를 찾는 함수 const handleImageSearch = () => { if (fileInputRef.current) { fileInputRef.current.click(); // 숨겨진 파일 입력을 트리거 } }; // 파일 선택 시 호출되는 함수 const handleFileChange = (event) => { const file = event.target.files?.[0]; if (file) { console.log('Selected File:', file); // 예: 파일을 서버에 업로드하거나 처리 const reader = new FileReader(); reader.onload = () => { console.log('Image Preview URL:', reader.result); }; reader.readAsDataURL(file); } }; return ( <> setFocus(true)} onKeyDown={onKeyDown} onChange={(e) => { setSearch(e.target.value); setstate({ ...state, initialized: true }); }} fullWidth InputProps={{ startAdornment: ( {isLoading ? ( ) : ( )} ) }} sx={{ ...(mobile && { position: 'sticky', top: 0, zIndex: 1, bgcolor: 'background.paper' }), '& .MuiInput-root': { height: { lg: 72, md: 72, sm: 72, xs: 56 } }, '& .MuiInputAdornment-root': { width: 100, mr: 0, svg: { mx: 'auto', color: 'primary.main' } } }} /> Shop {filtersLoading ? ( ) : ( )} Category {filtersLoading ? ( ) : ( )} SubCategory {filtersLoading ? ( ) : ( )} {state.initialized && !isLoading && !Boolean(state.products.length) && ( <> )} {!isLoading && !Boolean(state.products.length) ? ( '' ) : ( <> `1px solid ${theme.palette.primary.main}`, bgcolor: (theme) => alpha(theme.palette.primary.main, 0.16), h6: { color: 'primary.main' } }, '&.active': { border: (theme) => `1px solid ${theme.palette.primary.main}`, bgcolor: (theme) => alpha(theme.palette.primary.main, 0.16), h6: { color: 'primary.main' } } } }} autoFocusItem={!focus} > {(isLoading ? Array.from(new Array(mobile ? 6 : 8)) : state.products).map((product) => ( v._id === product?._id)?.length) ? 'active' : ''} onClick={() => handleListItemClick(multiSelect ? product : product?.slug)} > {isLoading ? ( ) : ( )}
{isLoading ? : product.name} {isLoading ? : product.category}
{isLoading ? ( ) : ( fCurrency(cCurrency(product.priceSale)) )}
))}
)}
{' '} {multiSelect && ( )}
); }