Skip to content

Commit

Permalink
added pagination limit selector
Browse files Browse the repository at this point in the history
  • Loading branch information
remko48 committed Oct 17, 2023
1 parent 04c016b commit f946269
Show file tree
Hide file tree
Showing 10 changed files with 133 additions and 8 deletions.
6 changes: 3 additions & 3 deletions pwa/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@
"prepare": "cd .. && husky install"
},
"dependencies": {
"@conduction/components": "2.2.17",
"@conduction/theme": "1.0.49",
"@conduction/components": "2.2.18",
"@conduction/theme": "1.0.50",
"@fortawesome/fontawesome-svg-core": "^6.1.1",
"@fortawesome/free-solid-svg-icons": "^6.4.0",
"@fortawesome/react-fontawesome": "^0.1.18",
Expand Down Expand Up @@ -57,7 +57,7 @@
"@tabler/icons-react": "2.21.0",
"@types/react": "^18.0.28",
"@types/react-dom": "^18.0.11",
"@utrecht/component-library-react": "^1.0.0-alpha.355",
"@utrecht/component-library-react": "^1.0.0-alpha.394",
"@utrecht/design-tokens": "^1.0.0-alpha.524",
"axios": "^0.25.0",
"clsx": "^1.1.1",
Expand Down
6 changes: 4 additions & 2 deletions pwa/src/apiService/resources/openWoo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,10 @@ export default class OpenWoo {
this._send = send;
}

public getAll = async (filters: IFiltersContext, currentPage: number): Promise<any> => {
let endpoint = `/openWOO?extend[]=all${filtersToQueryParams(filters)}&_order[Publicatiedatum]=desc&_limit=${OPEN_WOO_LIMIT}&_page=${currentPage}`;
public getAll = async (filters: IFiltersContext, currentPage: number, limit: number): Promise<any> => {
let endpoint = `/openWOO?extend[]=all${filtersToQueryParams(
filters,
)}&_order[Publicatiedatum]=desc&_limit=${limit}&_page=${currentPage}`;

if (process.env.GATSBY_OIDN_NUMBER) {
endpoint += `&oidn=${process.env.GATSBY_OIDN_NUMBER}`;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
.container {
display: flex;
flex-wrap: wrap;
gap: 8px;
align-items: center;
list-style-type: none;

padding: 0;
margin: 0;

user-select: none;
}
72 changes: 72 additions & 0 deletions pwa/src/components/paginationLimitSelect/PaginationLimitSelect.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
import * as React from "react";
import * as styles from "./PaginationLimitSelect.module.css";
import clsx from "clsx";
import { useForm } from "react-hook-form";
import { SelectSingle } from "@conduction/components";
import { IQueryLimitContext, queryLimitDefault, useQueryLimitContext } from "../../context/queryLimit";
import { useTranslation } from "react-i18next";

interface PaginationLimitSelectProps {
queryLimitName: string;
layoutClassName?: string;
}

export const PaginationLimitSelectComponent: React.FC<PaginationLimitSelectProps> = ({
queryLimitName,
layoutClassName,
}) => {
const {
watch,
register,
control,
setValue,
formState: { errors },
} = useForm();
const { queryLimit, setQueryLimit } = useQueryLimitContext();
const { t } = useTranslation();

const watchLimit = watch("limit");

const value = queryLimit[queryLimitName as keyof IQueryLimitContext];

React.useEffect(() => {
if (!watchLimit) return;
if (parseInt(watchLimit.value) === value) return;

const selectedLimit = limitSelectOptions.find((LimitOption) => LimitOption.value === watchLimit.value);

selectedLimit !== undefined && setQueryLimit({ ...queryLimit, [queryLimitName]: parseInt(selectedLimit.value) });
}, [watchLimit]);

React.useEffect(() => {
setValue(
"limit",
limitSelectOptions.find((LimitOption) => LimitOption.value === (value !== undefined && value.toString())),
);
}, []);

return (
<div className={clsx(styles.container, layoutClassName && layoutClassName)}>
<span>{`${t("Results per page")}:`}</span>
<SelectSingle
ariaLabel={t("Select result limit")}
{...{ register, errors, control }}
defaultValue={queryLimitDefault}
name="limit"
options={limitSelectOptions}
menuPlacement="auto"
placeholder={t("Limit")}
/>
</div>
);
};

const limitSelectOptions = [
{ label: "6", value: "6" },
{ label: "9", value: "9" },
{ label: "12", value: "12" },
{ label: "21", value: "21" },
{ label: "30", value: "30" },
{ label: "60", value: "60" },
{ label: "120", value: "120" },
];
3 changes: 3 additions & 0 deletions pwa/src/context/global.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,15 @@ import { defaultGatsbyContext, IGatsbyContext } from "./gatsby";
import { defaultFiltersContext, IFiltersContext } from "./filters";
import { defaultDisplayContext, IDisplayContext } from "./displays";
import { defaultPaginationContext, IPaginationContext } from "./pagination";
import { defaultQueryLimitContext, IQueryLimitContext } from "./queryLimit";

export interface IGlobalContext {
initiated: boolean;
gatsby: IGatsbyContext;
filters: IFiltersContext;
displays: IDisplayContext;
pagination: IPaginationContext;
queryLimit: IQueryLimitContext;
}

export const defaultGlobalContext: IGlobalContext = {
Expand All @@ -18,6 +20,7 @@ export const defaultGlobalContext: IGlobalContext = {
filters: defaultFiltersContext,
displays: defaultDisplayContext,
pagination: defaultPaginationContext,
queryLimit: defaultQueryLimitContext,
};

export const GlobalContext = React.createContext<
Expand Down
24 changes: 24 additions & 0 deletions pwa/src/context/queryLimit.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import * as React from "react";
import { GlobalContext } from "./global";

export const queryLimitDefault = 12;

export interface IQueryLimitContext {
objectsQueryLimit: number;
}

export const defaultQueryLimitContext: IQueryLimitContext = {
objectsQueryLimit: queryLimitDefault,
};

export const useQueryLimitContext = () => {
const [globalContext, setGlobalContext] = React.useContext(GlobalContext);

const queryLimit: IQueryLimitContext = globalContext.queryLimit;

const setQueryLimit = (query: IQueryLimitContext) => {
setGlobalContext((context) => ({ ...context, queryLimit: { ...globalContext.queryLimit, ...query } }));
};

return { setQueryLimit, queryLimit };
};
4 changes: 2 additions & 2 deletions pwa/src/hooks/openWoo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ import { IFiltersContext } from "../context/filters";
export const useOpenWoo = (queryClient: QueryClient) => {
const API: APIService | null = React.useContext(APIContext);

const getAll = (filters: IFiltersContext, currentPage: number) =>
useQuery<any, Error>(["OpenWoo", filters, currentPage], () => API?.OpenWoo.getAll(filters, currentPage), {
const getAll = (filters: IFiltersContext, currentPage: number, limit: number) =>
useQuery<any, Error>(["OpenWoo", filters, currentPage, limit], () => API?.OpenWoo.getAll(filters, currentPage, limit), {
onError: (error) => {
console.warn(error.message);
},
Expand Down
8 changes: 7 additions & 1 deletion pwa/src/templates/landing/LandingTemplate.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,18 +11,22 @@ import { QueryClient } from "react-query";
import { Pagination } from "@conduction/components";
import { usePaginationContext } from "../../context/pagination";
import { useTranslation } from "react-i18next";
import { useQueryLimitContext } from "../../context/queryLimit";
import { PaginationLimitSelectComponent } from "../../components/paginationLimitSelect/PaginationLimitSelect";

export const LandingTemplate: React.FC = () => {
const { currentPage, setCurrentPage } = usePaginationContext();
const { filters } = useFiltersContext();
const { t } = useTranslation();
const { queryLimit } = useQueryLimitContext();

const queryClient = new QueryClient();
const getItems = useOpenWoo(queryClient).getAll(filters, currentPage);
const getItems = useOpenWoo(queryClient).getAll(filters, currentPage, queryLimit.objectsQueryLimit);

return (
<>
<h1 className={styles.header1}></h1>

<JumbotronTemplate />

<Page>
Expand All @@ -39,6 +43,8 @@ export const LandingTemplate: React.FC = () => {
totalPages={getItems.data.pages}
{...{ currentPage, setCurrentPage }}
/>

<PaginationLimitSelectComponent queryLimitName={"objectsQueryLimit"} />
</div>
)}
{getItems.isLoading && <Skeleton height={"200px"} />}
Expand Down
3 changes: 3 additions & 0 deletions pwa/src/translations/en.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ export const en = {
Address: "Address",
Page: "Page",
Jumbotron: "Jumbotron",
Limit: "Limit",
"Jumbotron card": "Jumbotron card",
"N/A": "N/A",
"Details page": "Details page",
Expand Down Expand Up @@ -51,4 +52,6 @@ export const en = {
"Can open a new window": "Can open a new window",
"No results found": "No results found",
"Enter search query": "Enter search query",
"Results per page": "Results per page",
"Select result limit": "Select result limit",
};
3 changes: 3 additions & 0 deletions pwa/src/translations/nl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ export const nl = {
Address: "Adres",
Page: "Pagina",
Jumbotron: "Jumbotron",
Limit: "Limiet",
"Jumbotron card": "Jumbotron tegel",
"N/A": "N.v.t",
"Details page": "Detailpagina",
Expand Down Expand Up @@ -51,4 +52,6 @@ export const nl = {
"Can open a new window": "Kan een nieuw venster openen",
"No results found": "Geen resultaten gevonden",
"Enter search query": "Voer zoekopdracht in",
"Results per page": "Resultaten per pagina",
"Select result limit": "Selecteer resultaten limiet",
};

0 comments on commit f946269

Please sign in to comment.