-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor/ag/create-hook-for-pagination-and-filter (#98)
- Loading branch information
1 parent
4084bbc
commit a83f2c7
Showing
4 changed files
with
105 additions
and
121 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
import { useCallback, useEffect, useMemo, useState } from 'react'; | ||
import { usePathname, useRouter, useSearchParams } from 'next/navigation'; | ||
|
||
import { useFilter } from '@/features/shared/hooks/useFilter'; | ||
import { usePagination } from '@/features/shared/hooks/usePagination'; | ||
import { OptionKey } from '@/features/users/interfaces/OptionKey'; | ||
|
||
export const useFilterAndPagination = (selectOptionsData: OptionKey[], pagination: any) => { | ||
const [keySelected, setKeySelected] = useState<OptionKey>({ ...selectOptionsData[0] }); | ||
const searchParams = useSearchParams(); | ||
const params = useMemo(() => { | ||
const newParams = new URLSearchParams(); | ||
const entriesArray = Array.from(searchParams.entries()); | ||
for (const [key, value] of entriesArray) { | ||
newParams.append(key, value); | ||
} | ||
return newParams; | ||
}, [searchParams]); | ||
|
||
const handleReplaceURL = () => { | ||
replace(`${pathname}?${params.toString()}`); | ||
}; | ||
|
||
const pathname = usePathname(); | ||
const { replace } = useRouter(); | ||
const { handlePage, currentPage } = usePagination(pagination, params); | ||
const { | ||
filterValues, | ||
filtersApplied, | ||
handleRemoveFilter, | ||
handleSetFiltersApplied, | ||
handleRemoveFilterAll, | ||
handleSetFilterValues, | ||
} = useFilter(params); | ||
|
||
const [openDropdownId, setOpenDropdownId] = useState<string | null>(null); | ||
|
||
const handleDropdown = (id: string) => { | ||
setOpenDropdownId(openDropdownId === id ? null : id); | ||
}; | ||
|
||
const handleSearchType = useCallback(() => { | ||
const data = selectOptionsData.find(({ value }) => value === filterValues.key); | ||
if (data) { | ||
setKeySelected(data); | ||
} | ||
}, [filterValues.key, selectOptionsData]); | ||
|
||
useEffect(() => { | ||
handleSetFilterValues({ | ||
key: selectOptionsData[0].value, | ||
}); | ||
}, [selectOptionsData, handleSetFilterValues]); | ||
|
||
useEffect(() => { | ||
handlePage(1); | ||
}, [filterValues.term, handlePage]); | ||
|
||
useEffect(() => { | ||
handleSearchType(); | ||
}, [handleSearchType]); | ||
|
||
useEffect(() => { | ||
handleReplaceURL(); | ||
}, [currentPage, handleReplaceURL]); | ||
|
||
return { | ||
keySelected, | ||
setKeySelected, | ||
filterValues, | ||
filtersApplied, | ||
handleRemoveFilter, | ||
handleSetFiltersApplied, | ||
handleRemoveFilterAll, | ||
openDropdownId, | ||
handleDropdown, | ||
currentPage, | ||
handlePage, | ||
handleSetFilterValues, | ||
handleReplaceURL, | ||
}; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters