-
Notifications
You must be signed in to change notification settings - Fork 84
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add productCategory and productListingPage loader
- Loading branch information
1 parent
2bf920b
commit 3724358
Showing
5 changed files
with
151 additions
and
7 deletions.
There are no files selected for viewing
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,38 @@ | ||
import { RequestURLParam } from "../../../website/functions/requestToParam.ts"; | ||
import { AppContext } from "../../mod.ts"; | ||
import { Category } from "../../utils/types.ts"; | ||
|
||
export interface Props { | ||
slug?: RequestURLParam; | ||
} | ||
|
||
/** | ||
* @title WooCommerce Integration | ||
* @description Product Category loader | ||
*/ | ||
async function loader( | ||
props: Props, | ||
req: Request, | ||
ctx: AppContext, | ||
): Promise<Category | null> { | ||
const { slug } = props; | ||
const { api } = ctx; | ||
|
||
const urlPathname = new URL(req.url).pathname; | ||
|
||
const pathname = (slug || urlPathname).split("/").filter(Boolean).pop(); | ||
|
||
if (!pathname) return null; | ||
|
||
const categories = await api["GET /wc/v3/products/categories"]({ | ||
slug, | ||
}).then((res) => res.json()); | ||
|
||
const category = categories.find((item) => item.slug === pathname); | ||
|
||
if (!category) return null; | ||
|
||
return category; | ||
} | ||
|
||
export default loader; |
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,92 @@ | ||
import type { ProductListingPage } from "../../../commerce/types.ts"; | ||
import { AppContext } from "../../mod.ts"; | ||
import { toProduct } from "../../utils/transform.ts"; | ||
import { Order, OrderBy, Status, StockStatus } from "../../utils/types.ts"; | ||
import { WOOCOMMERCE_SORT_OPTIONS } from "../../utils/utils.ts"; | ||
|
||
export interface Props { | ||
/** | ||
* @description overrides the query term at url | ||
*/ | ||
query?: string; | ||
/** | ||
* @default 1 | ||
*/ | ||
page: number; | ||
/** | ||
* @title Per Page | ||
* @default 12 | ||
* @description Maximum number of items to be returned in result set. Default is 12. | ||
*/ | ||
per_page: number; | ||
order?: Order; | ||
order_by?: OrderBy; | ||
status?: Status; | ||
stock_status?: StockStatus; | ||
} | ||
|
||
/** | ||
* @title WooCommerce Integration | ||
* @description Product Listing Page loader | ||
*/ | ||
async function loader( | ||
props: Props, | ||
req: Request, | ||
ctx: AppContext, | ||
): Promise<ProductListingPage | null> { | ||
const url = new URL(req.url); | ||
const pathname = url.pathname.split("/").filter(Boolean).pop(); | ||
|
||
const { page = 1, per_page = 12, query } = props; | ||
const { api } = ctx; | ||
|
||
const category = await ctx.invoke.woocommerce.loaders.product.productCategory( | ||
{ | ||
slug: pathname, | ||
}, | ||
); | ||
|
||
const products = await api["GET /wc/v3/products"]({ | ||
...props, | ||
page, | ||
per_page, | ||
category: !query ? category?.id?.toString() : undefined, | ||
search: query, | ||
}).then((res) => res.json()); | ||
|
||
if (!products) return null; | ||
|
||
const totalPages = Math.ceil((category?.count ?? 0) / props.per_page); | ||
const notHasNextPage = totalPages == page; | ||
|
||
return { | ||
"@type": "ProductListingPage", | ||
products: products.map((product) => toProduct(product)), | ||
sortOptions: WOOCOMMERCE_SORT_OPTIONS, | ||
filters: [], | ||
pageInfo: { | ||
previousPage: page == 1 ? undefined : Number(page + 1).toString(), | ||
currentPage: page, | ||
nextPage: notHasNextPage ? undefined : Number(page + 1).toString(), | ||
}, | ||
breadcrumb: { | ||
"@type": "BreadcrumbList", | ||
itemListElement: [ | ||
{ | ||
"@type": "ListItem" as const, | ||
name: category?.name, | ||
position: 1, | ||
item: new URL(category?.slug ?? "").href, | ||
}, | ||
], | ||
numberOfItems: category?.count ?? 0, | ||
}, | ||
seo: { | ||
title: query || category?.name || pathname?.replaceAll("-", " ") || "", | ||
description: category?.description ?? "", | ||
canonical: pathname || req.url, | ||
}, | ||
}; | ||
} | ||
|
||
export default loader; |
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,8 @@ | ||
import { SortOption } from "../../commerce/types.ts"; | ||
|
||
export const WOOCOMMERCE_SORT_OPTIONS: SortOption[] = [ | ||
{ value: "date", label: "Data" }, | ||
{ value: "price", label: "Preço" }, | ||
{ value: "popularity", label: "Popularidade" }, | ||
{ value: "rating", label: "Média de Classificação" }, | ||
]; |