fix: apply feedback

This commit is contained in:
yassinedorbozgithub 2025-05-26 16:46:18 +01:00
parent bcca90b979
commit 7ddfdfc75a
2 changed files with 15 additions and 12 deletions

View File

@ -51,8 +51,8 @@ export const useQueryParam = () => {
[updateUrl], [updateUrl],
); );
const getQueryParam = useCallback( const getQueryParam = useCallback(
<T extends keyof QueryParams>(key: T): T | undefined => { <T extends keyof QueryParams>(key: T): QueryParams[T] | undefined => {
return queryParams[key] as T; return queryParams[key] as QueryParams[T];
}, },
[queryParams], [queryParams],
); );

View File

@ -12,6 +12,7 @@ import { ChangeEvent, useEffect, useRef, useState } from "react";
import { import {
TBuildInitialParamProps, TBuildInitialParamProps,
TBuildParamProps, TBuildParamProps,
TFilterStringFields,
TParamItem, TParamItem,
} from "@/types/search.types"; } from "@/types/search.types";
@ -23,13 +24,11 @@ const buildOrParams = <T>({ params, searchText }: TBuildParamProps<T>) => ({
})), })),
}); });
const buildILikeParams = <T>({ params, searchText }: TBuildParamProps<T>) => const buildILikeParams = <T>({ params, searchText }: TBuildParamProps<T>) =>
params?.reduce( params?.reduce((acc, field) => {
(acc, field) => ({ acc[field] = { contains: searchText };
...acc,
[field]: { contains: searchText }, return acc;
}), }, {} as Record<TFilterStringFields<T>, unknown>);
{},
);
const buildEqInitialParams = <T>({ const buildEqInitialParams = <T>({
initialParams, initialParams,
}: TBuildInitialParamProps<T>) => }: TBuildInitialParamProps<T>) =>
@ -75,11 +74,15 @@ export const useSearch = <T>({
}, [searchText]); }, [searchText]);
useEffect(() => { useEffect(() => {
if (queryParamKey && queryParamValue !== searchText) { if (
setSearchText(queryParamValue || ""); queryParamKey &&
queryParamValue !== searchText &&
queryParamValue !== undefined
) {
setSearchText(queryParamValue.toString() || "");
} }
// eslint-disable-next-line react-hooks/exhaustive-deps // eslint-disable-next-line react-hooks/exhaustive-deps
}, [queryParamValue]); }, [queryParamValue, queryParamKey]);
const onSearch = debounce( const onSearch = debounce(
async (e: ChangeEvent<HTMLInputElement | HTMLTextAreaElement> | string) => { async (e: ChangeEvent<HTMLInputElement | HTMLTextAreaElement> | string) => {