Skip to content

Commit

Permalink
fix build error with more readable code
Browse files Browse the repository at this point in the history
not related with the change but resulting in build error
  • Loading branch information
kouloumos committed Nov 14, 2024
1 parent f4b3274 commit cd08ea6
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 5 deletions.
12 changes: 11 additions & 1 deletion src/context/SearchQueryContext.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,17 @@ export const SearchQueryProvider = ({
}, [rawSearchQuery]);

const page = useMemo(() => {
return pageQuery ? parseInt(pageQuery) - 1 ?? 0 : 0;
// Handle empty or invalid input
if (!pageQuery) {
return 0;
}

// Convert to number and validate
const parsedPage = Number(pageQuery);
const isValidPage = !isNaN(parsedPage) && parsedPage > 0;

// Convert from 1-based to 0-based index, or default to 0
return isValidPage ? parsedPage - 1 : 0;
}, [pageQuery]);

const resultsPerPage = sizeQuery
Expand Down
8 changes: 4 additions & 4 deletions src/pages/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -55,10 +55,10 @@ export const getServerSideProps: GetServerSideProps = async (
};
}

const page = pageQuery ? parseInt(pageQuery) - 1 ?? 0 : 0;
const size = sizeQuery
? parseInt(sizeQuery) ?? defaultParam[URLSearchParamsKeyword.SIZE]
: defaultParam[URLSearchParamsKeyword.SIZE];
const defaultPage = 0;
const page = pageQuery ? parseInt(pageQuery) - 1 : defaultPage;
const defaultSize = defaultParam[URLSearchParamsKeyword.SIZE];
const size = sizeQuery ? parseInt(sizeQuery) || defaultSize : defaultSize;

const options = {
queryString,
Expand Down

0 comments on commit cd08ea6

Please sign in to comment.