import { AppConsts } from '@/shared';
import { RouterConsts } from '@/shared/consts/RouterConsts';
import { useUpdateSearchParams } from '@/shared/hooks';
import { ChangeEvent, useCallback } from 'react';
type Params = {
updateParamAndResetPage: (paramName: string, value: unknown) => void;
refetch: () => void;
resetSmartRoomId: () => void;
clearAdditionalFilters: () => void;
resetAdditionalFilterInputs: () => void;
};
export const useBaseFilters = ({
updateParamAndResetPage,
refetch,
resetSmartRoomId,
clearAdditionalFilters,
resetAdditionalFilterInputs,
}: Params) => {
const updateSearchParams = useUpdateSearchParams();
const handleChange =
(paramName: string) =>
(event: ChangeEvent<{ name?: string | undefined; value: unknown }>) => {
if (paramName === 'type') {
clearAdditionalFilters();
}
updateParamAndResetPage(paramName, event.target.value);
};
const handleChangeUser = useCallback(
(value: string) => {
updateParamAndResetPage('userId', value);
},
[updateParamAndResetPage],
);
const handleChangeTimeFrom = useCallback(
(time: Date) => {
updateParamAndResetPage(
'tmMin',
new Date(time).getTime() / AppConsts.THOUSAND,
);
},
[updateParamAndResetPage],
);
const handleChangeTimeMax = useCallback(
(time: Date) => {
updateParamAndResetPage(
'tmMax',
new Date(time).getTime() / AppConsts.THOUSAND,
);
},
[updateParamAndResetPage],
);
const resetFilters = useCallback(() => {
resetSmartRoomId();
resetAdditionalFilterInputs();
updateSearchParams({
queryObject: {
...RouterConsts.AUDIT_DEFAULT_QUERY,
type: undefined,
source: undefined,
userId: undefined,
smartroomId: undefined,
modelIds: [],
locationIds: [],
mac: undefined,
ip: undefined,
regionId: [],
cityId: [],
placeId: [],
floorId: [],
roomId: [],
treeId: [],
},
});
}, [resetAdditionalFilterInputs, resetSmartRoomId, updateSearchParams]);
const refetchQuery = useCallback(() => {
refetch();
}, [refetch]);
return {
resetFilters,
refetchQuery,
handleChange,
handleChangeUser,
handleChangeTimeFrom,
handleChangeTimeMax,
};
};