Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feature/XW-95/queryParams #129

Merged
merged 1 commit into from
Oct 23, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ 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 { IQueryLimitContext, QUERY_LIMIT_DEFAULT, useQueryLimitContext } from "../../context/queryLimit";
import { useTranslation } from "react-i18next";

interface PaginationLimitSelectProps {
Expand Down Expand Up @@ -33,25 +33,27 @@ export const PaginationLimitSelectComponent: React.FC<PaginationLimitSelectProps
if (!watchLimit) return;
if (parseInt(watchLimit.value) === value) return;

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

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

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

return (
<div className={clsx(styles.container, layoutClassName && layoutClassName)}>
<span>{`${t("Results per page")}:`}</span>
<span>{t("Results per page")}:</span>
<SelectSingle
ariaLabel={t("Select result limit")}
{...{ register, errors, control }}
defaultValue={queryLimitDefault}
defaultValue={QUERY_LIMIT_DEFAULT}
name="limit"
options={limitSelectOptions}
menuPlacement="auto"
Expand Down
6 changes: 3 additions & 3 deletions pwa/src/context/queryLimit.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
import * as React from "react";
import { GlobalContext } from "./global";

export const queryLimitDefault = 12;
export const QUERY_LIMIT_DEFAULT = 12;

export interface IQueryLimitContext {
objectsQueryLimit: number;
openWooObjectsQueryLimit: number;
}

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

export const useQueryLimitContext = () => {
Expand Down
95 changes: 29 additions & 66 deletions pwa/src/services/filtersToQueryParams.ts
Original file line number Diff line number Diff line change
@@ -1,80 +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: string = "";
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 = "";

value.forEach((value) => {
arrayParams += `&${key}[]=${value}`;
});

params += arrayParams;
}
}

return params;
return params ? `&${params}` : "";
};

export const filtersToUrlQueryParams = (filters: any): string => {
Object.keys(filters)
.filter((key) => filterKeysToRemove.includes(key))
.forEach((key) => {
delete filters[key];
});

let params: string = "";

var first_iteration = true;
for (const [key, value] of Object.entries(filters)) {
if (!value) continue;

if (first_iteration) {
if (typeof value === "string") {
params += `?${key}=${value.replace(/\s+/g, "_")}`;
}

if (Array.isArray(value)) {
let arrayParams = "";

value.forEach((value) => {
arrayParams += `?${key}[]=${value.replace(/\s+/g, "_")}`;
});

params += arrayParams;
}

first_iteration = false;
} else {
if (typeof value === "string") {
params += `&${key}=${value.replace(/\s+/g, "_")}`;
}
export const filtersToUrlQueryParams = (filters: Record<string, any>): string => {
const cleanedFilters = Object.fromEntries(
Object.entries(filters).filter(([key]) => !filterKeysToRemove.includes(key)),
);

if (Array.isArray(value)) {
let arrayParams = "";
const params = Object.entries(cleanedFilters)
.map(([key, value]) => {
if (!value) return null;

value.forEach((value) => {
arrayParams += `&${key}[]=${value.replace(/\s+/g, "_")}`;
});
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[] = [];
6 changes: 3 additions & 3 deletions pwa/src/templates/landing/LandingTemplate.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,11 @@ export const LandingTemplate: React.FC = () => {
const { queryLimit } = useQueryLimitContext();

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

React.useEffect(() => {
setCurrentPage(1);
}, [queryLimit.objectsQueryLimit]);
}, [queryLimit.openWooObjectsQueryLimit]);

return (
<>
Expand All @@ -45,7 +45,7 @@ export const LandingTemplate: React.FC = () => {
totalPages={getItems.data.pages}
{...{ currentPage, setCurrentPage }}
/>
<PaginationLimitSelectComponent queryLimitName={"objectsQueryLimit"} />
<PaginationLimitSelectComponent queryLimitName={"openWooObjectsQueryLimit"} />
</div>
</div>
)}
Expand Down
Loading