Skip to content

Commit

Permalink
Wap Proxy and PLP filters and sort (#441)
Browse files Browse the repository at this point in the history
* fix: proxy route

* feat: add filters and order in wap plp loader

* Update wap/loaders/productListingPage.ts

---------

Co-authored-by: guitavano <[email protected]>
  • Loading branch information
IncognitaDev and guitavano authored Mar 6, 2024
1 parent 22a7fa6 commit 3511bf9
Show file tree
Hide file tree
Showing 4 changed files with 97 additions and 9 deletions.
58 changes: 51 additions & 7 deletions wap/loaders/productListingPage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,39 @@ import {
import { WapProductsListPage } from "../utils/type.ts";
import { TypedResponse } from "../../utils/http.ts";

export interface Props {
type ORDER_OPTS = "Favoritos" | "Maior Preço" | "Menor Preço" | "Popularidade";

export interface FilterItem {
/**
* @title Filter type
*/
key: string;
/**
* @title Filter value
*/
value: string;
}

export type Props = {
busca?: string;
/**
* @max 100
* @default 12
*/
limit?: number;
}
/**
* @description page number
*/
page?: number;
/**
* @description order of products
*/
order?: ORDER_OPTS;
/**
* @title filters
*/
filterParams?: FilterItem[];
};

const endPoint = {
"product/listing/category":
Expand All @@ -43,12 +68,24 @@ const loader = async (
const rawSearch = url.searchParams.get("busca") ?? props.busca;
const busca = rawSearch && encodeURIComponent(rawSearch);

const page = Number(url.searchParams.get("pg") || 1);
const page = Number(url.searchParams.get("pg") ?? props.page ?? 1);

const limit = Number(url.searchParams.get("ipp") ?? props.limit ?? 12);

const order = url.searchParams.get("order") ?? props.order;

const offset = page <= 1 ? 0 : (page - 1) * limit;

const filterParams = props.filterParams?.reduce((acc, { key, value }) => {
if (acc[key]) {
acc[key] = [...acc[key], value];
} else {
acc[key] = value;
}

return acc;
}, {} as Record<string, string | string[]>) ?? {};

if (!busca && url.pathname.startsWith("%2F_fresh")) return null;

const { nivel } = await api
Expand All @@ -58,23 +95,29 @@ const loader = async (

if (!busca && !nivel) return null;

const params: Record<string, string | string[]> = {};
const searchParams: Record<string, string | string[]> = {};

url.searchParams.delete("busca");
url.searchParams.delete("pg");
url.searchParams.delete("order");

url.searchParams.forEach((v, k) => {
if (params[k]) {
params[k] = [...params[k], v];
if (searchParams[k]) {
searchParams[k] = [...searchParams[k], v];
}
params[k] = v;
searchParams[k] = v;
});

const params = { ...filterParams, ...searchParams };

const endpoint = nivel && endPoint[nivel as keyof typeof endPoint];

const data = endpoint
? await api[endpoint]({
url: url.pathname,
limit: String(limit),
offset: String(offset),
order,
...params,
}).then((response: TypedResponse<unknown>) =>
response.json()
Expand All @@ -84,6 +127,7 @@ const loader = async (
busca,
limit: String(limit),
offset: String(offset),
order,
...params,
}).then((response) => response.json()) as WapProductsListPage;

Expand Down
3 changes: 1 addition & 2 deletions wap/loaders/proxy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,12 @@ function loader(
const checkout = [
...DEFAULT_PROXY_PATHS,
...pagesToProxy,
].map(([pathTemplate, basePath]) => ({
].map((pathTemplate) => ({
pathTemplate,
handler: {
value: {
__resolveType: "website/handlers/proxy.ts",
url: baseUrl,
basePath,
customHeaders: [{
Host: baseUrl,
}],
Expand Down
5 changes: 5 additions & 0 deletions wap/utils/openapi/api.openapi.gen.ts
Original file line number Diff line number Diff line change
Expand Up @@ -520,6 +520,7 @@ url?: string
searchParams: {
url?: string
offset?: string
order?: string
limit?: string
}
}
Expand All @@ -542,6 +543,7 @@ limit?: string
searchParams: {
url?: string
offset?: string
order?: string
limit?: string
}
}
Expand All @@ -552,6 +554,7 @@ limit?: string
searchParams: {
url?: string
offset?: string
order?: string
limit?: string
}
}
Expand All @@ -562,6 +565,7 @@ limit?: string
searchParams: {
url?: string
offset?: string
order?: string
limit?: string
}
}
Expand All @@ -572,6 +576,7 @@ limit?: string
searchParams: {
busca?: string
offset?: string
order?: string
limit?: string
}
}
Expand Down
40 changes: 40 additions & 0 deletions wap/utils/openapi/api.openapi.json
Original file line number Diff line number Diff line change
Expand Up @@ -2997,6 +2997,14 @@
"example": "0"
}
},
{
"name": "order",
"in": "query",
"schema": {
"type": "string",
"example": "Popularidade"
}
},
{
"name": "limit",
"in": "query",
Expand Down Expand Up @@ -3099,6 +3107,14 @@
"example": "0"
}
},
{
"name": "order",
"in": "query",
"schema": {
"type": "string",
"example": "Popularidade"
}
},
{
"name": "limit",
"in": "query",
Expand Down Expand Up @@ -3149,6 +3165,14 @@
"example": "0"
}
},
{
"name": "order",
"in": "query",
"schema": {
"type": "string",
"example": "Popularidade"
}
},
{
"name": "limit",
"in": "query",
Expand Down Expand Up @@ -3199,6 +3223,14 @@
"example": "0"
}
},
{
"name": "order",
"in": "query",
"schema": {
"type": "string",
"example": "Popularidade"
}
},
{
"name": "limit",
"in": "query",
Expand Down Expand Up @@ -3249,6 +3281,14 @@
"example": "0"
}
},
{
"name": "order",
"in": "query",
"schema": {
"type": "string",
"example": "Popularidade"
}
},
{
"name": "limit",
"in": "query",
Expand Down

0 comments on commit 3511bf9

Please sign in to comment.