diff --git a/src/screens/search/form-for-search.tsx b/src/screens/search/form-for-search.tsx index fbdca5a..00beba4 100644 --- a/src/screens/search/form-for-search.tsx +++ b/src/screens/search/form-for-search.tsx @@ -1,24 +1,20 @@ import { zodResolver } from '@hookform/resolvers/zod'; +import { Picker } from '@react-native-picker/picker'; import React from 'react'; +import { useEffect, useMemo, useState } from 'react'; import type { SubmitHandler } from 'react-hook-form'; import { useForm } from 'react-hook-form'; import * as z from 'zod'; -import { Button, ControlledInput, ScrollView, View } from '@/ui'; +import { Button, ControlledInput, ScrollView, Text, View } from '@/ui'; + +export const SEARCH_BY_USERNAME = 1; +export const SEARCH_BY_CONTENT = 2; +export const SEARCH_BY_HASHTAG = 3; export const schema = z.object({ - username: z - .string() - .max(50, 'First Name cannot exceed 50 characters') - .optional(), - content: z - .string() - .max(50, 'Last Name cannot exceed 50 characters') - .optional(), - hashtag: z - .string() - .max(50, 'Last Name cannot exceed 50 characters') - .optional(), + search: z.string().max(50, 'Search cannot exceed 50 characters'), + type: z.number(), }); export type FormType = z.infer; @@ -30,50 +26,46 @@ export interface SearchFormProps { export const FormForSearch: React.FC = ({ onSearchSubmit = () => {}, }) => { - const { handleSubmit, control, watch } = useForm({ + const { handleSubmit, control, setValue } = useForm({ resolver: zodResolver(schema), }); - const watchedFields = watch(); - const [isAnyFieldFilled, setIsAnyFieldFilled] = - React.useState(false); + const typeOptions = useMemo(() => ['username', 'content', 'hashtag'], []); + const [type, setTypeOption] = useState(typeOptions[0]); - React.useEffect(() => { - const anyFieldFilled = Object.values(watchedFields).some( - (fieldValue) => fieldValue && fieldValue.trim() !== '' + useEffect(() => { + setValue( + 'type', + type === typeOptions[0] + ? SEARCH_BY_USERNAME + : type === typeOptions[1] + ? SEARCH_BY_CONTENT + : SEARCH_BY_HASHTAG ); - setIsAnyFieldFilled(anyFieldFilled); - }, [watchedFields]); - - const shouldDisableField = (fieldName: keyof FormType) => { - const fieldValue = watchedFields[fieldName]; - const isFieldEmpty = !fieldValue || fieldValue.trim() === ''; - return isAnyFieldFilled && isFieldEmpty; - }; + }, [type, setValue, typeOptions]); return ( - - - + Search By + + + { + setTypeOption(itemValue); + }} + > + {typeOptions.map((option, index) => ( + + ))} + + diff --git a/src/screens/search/search-bar.tsx b/src/screens/search/search-bar.tsx index ec914cc..3865084 100644 --- a/src/screens/search/search-bar.tsx +++ b/src/screens/search/search-bar.tsx @@ -8,6 +8,11 @@ import { getUserState } from '@/core'; import type { FormType } from './form-for-search'; import { FormForSearch } from './form-for-search'; +import { + SEARCH_BY_CONTENT, + SEARCH_BY_HASHTAG, + SEARCH_BY_USERNAME, +} from './form-for-search'; import type { SearchStackParamList } from './search-navigator'; const whenSearch = async ( @@ -18,30 +23,29 @@ const whenSearch = async ( console.log(currentUser); console.log(data); - if (data.username && data.username.trim() !== '') { + + if (data.type === SEARCH_BY_USERNAME && data.search.trim() !== '') { try { const { data: users } = await client.identity.get( - `/api/filter/${currentUser?.id}/${data.username}` + `/api/filter/${currentUser?.id}/${data.search}` ); navigate('Users', { users }); } catch (error) { console.error('Error searching by username:', error); } - } - if (data.content && data.content.trim() !== '') { + } else if (data.type === SEARCH_BY_CONTENT && data.search.trim() !== '') { try { const { data: snapsFromContent } = await client.content.get( - `/api/filter/content?user_id=${currentUser?.id}&content=${data.content}` + `/api/filter/content?user_id=${currentUser?.id}&content=${data.search}` ); navigate('SnapList', { snaps: snapsFromContent.snaps }); } catch (error) { console.error('Error searching by content:', error); } - } - if (data.hashtag && data.hashtag.trim() !== '') { + } else if (data.type === SEARCH_BY_HASHTAG && data.search.trim() !== '') { try { const { data: snapsFromHashtags } = await client.content.get( - `/api/filter/hashtag?user_id=${currentUser?.id}&hashtag=${data.hashtag}` + `/api/filter/hashtag?user_id=${currentUser?.id}&hashtag=${data.search}` ); navigate('SnapList', { snaps: snapsFromHashtags.snaps }); } catch (error) {