Skip to content

Commit

Permalink
refactor: change type of parameters of getSearchPageParams
Browse files Browse the repository at this point in the history
  • Loading branch information
pooriaset committed May 18, 2024
1 parent 17dc5dc commit f2f9a8f
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 10 deletions.
2 changes: 1 addition & 1 deletion src/app/[locale]/(main)/search/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import SortWrapper from './components/SortWrapper';

const Page = async (props: { searchParams: Record<string, unknown> }) => {
const { inStock, categoryId, sort, q } = getSearchPageParams(
props.searchParams,
new Map(Object.entries(props.searchParams)),
);

const { data } = await getClient().query<GetAllProductsQuery>({
Expand Down
20 changes: 11 additions & 9 deletions src/utils/params.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,17 +14,19 @@ export interface SearchPagesParams {
categoryId: number | null;
}

export const getSearchPageParams = (params: any): SearchPagesParams => {
const _params = params.get ? params : new Map(Object.entries(params));

const sortParam = _params.get(SearchPageParamsKeys.Sort);
export const getSearchPageParams = (
params: Map<string, unknown>,
): SearchPagesParams => {
const sortParam = params.get(SearchPageParamsKeys.Sort);
const sort = sortParam ? +sortParam : sortOptions[0].key;
const q = _params.get(SearchPageParamsKeys.Q);
const inStock = _params.has(SearchPageParamsKeys.InStock)
? _params.get(SearchPageParamsKeys.InStock) === 'true'
const q = params.has(SearchPageParamsKeys.Q)
? (params.get(SearchPageParamsKeys.Q) as string)
: null;
const inStock = params.has(SearchPageParamsKeys.InStock)
? params.get(SearchPageParamsKeys.InStock) === 'true'
: true;
const categoryId = _params.has(SearchPageParamsKeys.CategoryId)
? +_params.get(SearchPageParamsKeys.CategoryId)
const categoryId = params.has(SearchPageParamsKeys.CategoryId)
? +params.get(SearchPageParamsKeys.CategoryId)!
: null;

return {
Expand Down

0 comments on commit f2f9a8f

Please sign in to comment.