diff --git a/components/Forms/BookingWidget/FormContent/Search/SearchList/index.tsx b/components/Forms/BookingWidget/FormContent/Search/SearchList/index.tsx index c66c8e10b..cce0e571f 100644 --- a/components/Forms/BookingWidget/FormContent/Search/SearchList/index.tsx +++ b/components/Forms/BookingWidget/FormContent/Search/SearchList/index.tsx @@ -29,23 +29,17 @@ export default function SearchList({ }: SearchListProps) { const intl = useIntl() const [hasMounted, setHasMounted] = useState(false) - const [isFormSubmitted, setIsFormSubmitted] = useState(false) const { clearErrors, formState: { errors, isSubmitted }, } = useFormContext() const searchError = errors["search"] - useEffect(() => { - setIsFormSubmitted(isSubmitted) - }, [isSubmitted]) - useEffect(() => { let timeoutID: ReturnType | null = null - if (searchError && searchError.message === "Required") { + if (searchError) { timeoutID = setTimeout(() => { clearErrors("search") - setIsFormSubmitted(false) // magic number originates from animation // 5000ms delay + 120ms exectuion }, 5120) @@ -66,7 +60,7 @@ export default function SearchList({ return null } - if (searchError && isFormSubmitted) { + if (searchError && isSubmitted) { if (typeof searchError.message === "string") { if (!isOpen) { if (searchError.message === "Required") { @@ -87,6 +81,24 @@ export default function SearchList({ ) + } else if (searchError.type === "custom") { + return ( + + + + {intl.formatMessage({ id: "No results" })} + + + {intl.formatMessage({ + id: "We couldn't find a matching location for your search.", + })} + + + ) } } } diff --git a/components/Forms/BookingWidget/FormContent/Search/index.tsx b/components/Forms/BookingWidget/FormContent/Search/index.tsx index 860ecffde..1fe45ed04 100644 --- a/components/Forms/BookingWidget/FormContent/Search/index.tsx +++ b/components/Forms/BookingWidget/FormContent/Search/index.tsx @@ -27,7 +27,8 @@ import type { Location } from "@/types/trpc/routers/hotel/locations" const name = "search" export default function Search({ locations }: SearchProps) { - const { register, setValue, trigger } = useFormContext() + const { register, setValue, trigger, unregister } = + useFormContext() const intl = useIntl() const value = useWatch({ name }) const [state, dispatch] = useReducer( @@ -135,6 +136,27 @@ export default function Search({ locations }: SearchProps) { } }, [dispatch]) + const stayType = state.searchData?.type === "cities" ? "city" : "hotel" + const stayValue = + (value === state.searchData?.name && + ((state.searchData?.type === "cities" && state.searchData?.name) || + state.searchData?.id)) || + "" + + useEffect(() => { + if (stayType === "city") { + unregister("hotel") + setValue(stayType, stayValue, { + shouldValidate: true, + }) + } else { + unregister("city") + setValue(stayType, Number(stayValue), { + shouldValidate: true, + }) + } + }, [stayType, stayValue, unregister, setValue]) + return ( (
+ {value ? ( + // Adding hidden input to define hotel or city based on destination selection for basic form submit. + + ) : null}