Skip to content

Commit

Permalink
refactor/ag/create-hook-for-pagination-and-filter (#98)
Browse files Browse the repository at this point in the history
  • Loading branch information
ANGdesarrollo authored Oct 20, 2024
1 parent 4084bbc commit a83f2c7
Show file tree
Hide file tree
Showing 4 changed files with 105 additions and 121 deletions.
73 changes: 12 additions & 61 deletions src/features/items/organisms/List.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
'use client';
import React, { useCallback, useEffect, useMemo, useState } from 'react';
import { usePathname, useRouter, useSearchParams } from 'next/navigation';
import React from 'react';
import { useTranslations } from 'next-intl';

import { deleteItem } from '@/features/items/actions/ItemAction';
Expand All @@ -14,15 +13,12 @@ import { PaginationComponent } from '@/features/shared/atoms/pagination/Paginati
import { SelectColorType } from '@/features/shared/atoms/select/SelectForm';
import { SizeType, SwitchComponent } from '@/features/shared/atoms/swich/switch';
import { Title } from '@/features/shared/atoms/title/Title';
import { useFilter } from '@/features/shared/hooks/useFilter';
import { usePagination } from '@/features/shared/hooks/usePagination';
import { useFilterAndPagination } from '@/features/shared/hooks/useFilterAndPagination';
import { PaginationAPI } from '@/features/shared/interfaces/PaginationAPI';
import { FiltersApplied } from '@/features/shared/molecules/filtersApplied/FiltersApplied';
import { SortComponent } from '@/features/shared/molecules/sort/Sort';
import { FilterAndSearch } from '@/features/shared/organisms/filterAndSearch/FilterAndSearch';

import { OptionKey } from '@/features/users/interfaces/OptionKey';

import styleCard from './card.module.css';
import style from './list.module.css';

Expand All @@ -31,65 +27,20 @@ type Props = {
pagination: PaginationAPI;
};
export const List = ({ items, pagination }: Props) => {
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 pathname = usePathname();
const { replace } = useRouter();
const { handlePage, currentPage } = usePagination(pagination, params);
const t = useTranslations('Items');
const {
handleSetFilterValues,
filterValues,
filtersApplied,
handlePage,
currentPage,
handleRemoveFilter,
handleSetFiltersApplied,
handleRemoveFilterAll,
} = useFilter(params);
const t = useTranslations('Items');

const [openDropdownId, setOpenDropdownId] = useState<string | null>(null);

const handleDropdown = (id: string) => {
setOpenDropdownId(openDropdownId === id ? null : id);
};

const handleReplaceURL = () => {
replace(`${pathname}?${params.toString()}`);
};

const handleSearchType = useCallback(() => {
const data = selectOptionsData.find(({ value }) => value === filterValues.key);
if (data) {
setKeySelected(data);
}
}, [filterValues.key]);

useEffect(() => {
handleSetFilterValues({
key: selectOptionsData[0].value,
});
}, []);

useEffect(() => {
handlePage(1);
}, [filterValues.term]);

useEffect(() => {
handleSearchType();
}, [handleSearchType]);

useEffect(() => {
handleReplaceURL();
}, [currentPage]);
filtersApplied,
handleDropdown,
openDropdownId,
keySelected,
handleSetFilterValues,
handleReplaceURL,
} = useFilterAndPagination(selectOptionsData, pagination);

return (
<section className={style.container}>
Expand Down
Empty file.
82 changes: 82 additions & 0 deletions src/features/shared/hooks/useFilterAndPagination.tsx
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,
};
};
71 changes: 11 additions & 60 deletions src/features/users/organisms/UsersList.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
'use client';
import React, { useCallback, useEffect, useMemo, useState } from 'react';

import { usePathname, useRouter, useSearchParams } from 'next/navigation';
import React from 'react';

import { useTranslations } from 'next-intl';

Expand All @@ -15,14 +13,12 @@ import { SelectColorType } from '@/features/shared/atoms/select/SelectForm';
import { SizeType } from '@/features/shared/atoms/swich/switch';
import { Title } from '@/features/shared/atoms/title/Title';

import { useFilter } from '@/features/shared/hooks/useFilter';
import { useFilterAndPagination } from '@/features/shared/hooks/useFilterAndPagination';
import { UserHasRole } from '@/features/shared/interfaces/UserHasRole';
import { FiltersApplied } from '@/features/shared/molecules/filtersApplied/FiltersApplied';
import { FilterAndSearch } from '@/features/shared/organisms/filterAndSearch/FilterAndSearch';
import { usePagination } from '@/features/users/atoms/usePagination/usePagination';

import { selectOptionsData } from '@/features/users/constants/selectOptionsData';
import { OptionKey } from '@/features/users/interfaces/OptionKey';

import styles from './users-list.module.css';

Expand All @@ -33,64 +29,19 @@ interface Props {

export const UserList = (props: Props) => {
const t = useTranslations('UserList');
const [keySelected, setKeySelected] = useState<OptionKey>({ ...selectOptionsData[0] });
const pathname = usePathname();
const { replace } = useRouter();
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 { handlePage, currentPage } = usePagination(props.pagination, params);
const {
handleSetFilterValues,
filterValues,
filtersApplied,
handlePage,
currentPage,
handleRemoveFilter,
handleSetFiltersApplied,
handleRemoveFilterAll,
} = useFilter(params);

const [openDropdownId, setOpenDropdownId] = useState<string | null>(null);

const handleReplaceURL = () => {
replace(`${pathname}?${params.toString()}`);
};

const handleSearchType = useCallback(() => {
const data = selectOptionsData.find(({ value }) => value === filterValues.key);
if (data) {
setKeySelected(data);
}
}, [filterValues.key]);

const handleDropdown = (id: string) => {
setOpenDropdownId(openDropdownId === id ? null : id);
};

useEffect(() => {
handleSetFilterValues({
key: selectOptionsData[0].value,
});
}, []);

useEffect(() => {
handlePage(1);
}, [filterValues.term]);

useEffect(() => {
handleSearchType();
}, [handleSearchType]);

useEffect(() => {
handleReplaceURL();
}, [currentPage]);
filtersApplied,
handleDropdown,
openDropdownId,
keySelected,
handleSetFilterValues,
handleReplaceURL,
} = useFilterAndPagination(selectOptionsData, props.pagination);

return (
<section className={styles.container}>
Expand Down

0 comments on commit a83f2c7

Please sign in to comment.