generated from ConductionNL/product-website-template
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #122 from ConductionNL/development
Development to main, week 42
- Loading branch information
Showing
15 changed files
with
447 additions
and
123 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
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
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
12 changes: 12 additions & 0 deletions
12
pwa/src/components/paginationLimitSelect/PaginationLimitSelect.module.css
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,12 @@ | ||
.container { | ||
display: flex; | ||
flex-wrap: wrap; | ||
gap: 8px; | ||
align-items: center; | ||
list-style-type: none; | ||
|
||
padding: 0; | ||
margin: 0; | ||
|
||
user-select: none; | ||
} |
74 changes: 74 additions & 0 deletions
74
pwa/src/components/paginationLimitSelect/PaginationLimitSelect.tsx
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,74 @@ | ||
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, QUERY_LIMIT_DEFAULT, 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); | ||
|
||
if (selectedLimit) { | ||
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={QUERY_LIMIT_DEFAULT} | ||
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" }, | ||
]; |
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
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,24 @@ | ||
import * as React from "react"; | ||
import { GlobalContext } from "./global"; | ||
|
||
export const QUERY_LIMIT_DEFAULT = 12; | ||
|
||
export interface IQueryLimitContext { | ||
openWooObjectsQueryLimit: number; | ||
} | ||
|
||
export const defaultQueryLimitContext: IQueryLimitContext = { | ||
openWooObjectsQueryLimit: QUERY_LIMIT_DEFAULT, | ||
}; | ||
|
||
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 }; | ||
}; |
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
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 |
---|---|---|
@@ -1,31 +1,43 @@ | ||
export const filtersToQueryParams = (filters: any): string => { | ||
Object.keys(filters) | ||
.filter((key) => filterKeysToRemove.includes(key)) | ||
.forEach((key) => { | ||
delete filters[key]; | ||
}); | ||
const cleanedFilters = Object.fromEntries( | ||
Object.entries(filters).filter(([key]) => !filterKeysToRemove.includes(key)), | ||
); | ||
|
||
let params = ""; | ||
const params = Object.entries(cleanedFilters) | ||
.map(([key, value]) => { | ||
if (!value) return null; | ||
|
||
for (const [key, value] of Object.entries(filters)) { | ||
if (!value) continue; | ||
const formattedValue = Array.isArray(value) | ||
? value.map((v: string) => v.replace(/\s+/g, "_")).join(`&${key}[]=`) | ||
: (value as string).replace(/\s+/g, "_"); | ||
|
||
if (typeof value === "string") { | ||
params += `&${key}=${value}`; | ||
} | ||
return `${Array.isArray(value) ? `${key}[]` : key}=${formattedValue}`; | ||
}) | ||
.filter(Boolean) | ||
.join("&"); | ||
|
||
if (Array.isArray(value)) { | ||
let arrayParams = ""; | ||
return params ? `&${params}` : ""; | ||
}; | ||
|
||
export const filtersToUrlQueryParams = (filters: Record<string, any>): string => { | ||
const cleanedFilters = Object.fromEntries( | ||
Object.entries(filters).filter(([key]) => !filterKeysToRemove.includes(key)), | ||
); | ||
|
||
const params = Object.entries(cleanedFilters) | ||
.map(([key, value]) => { | ||
if (!value) return null; | ||
|
||
value.forEach((value) => { | ||
arrayParams += `&${key}[]=${value}`; | ||
}); | ||
const formattedValue = Array.isArray(value) | ||
? value.map((v: string) => v.replace(/\s+/g, "_")).join(`&${key}[]=`) | ||
: (value as string).replace(/\s+/g, "_"); | ||
|
||
params += arrayParams; | ||
} | ||
} | ||
return `${Array.isArray(value) ? `${key}[]` : key}=${formattedValue}`; | ||
}) | ||
.filter(Boolean) | ||
.join("&"); | ||
|
||
return params; | ||
return params ? `?${params}` : ""; | ||
}; | ||
|
||
const filterKeysToRemove: string[] = []; |
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
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
Oops, something went wrong.