mirror of
https://github.com/vercel/commerce.git
synced 2025-06-16 04:11:22 +00:00
refactoring
This commit is contained in:
parent
83ae628672
commit
fcfe7fbcb0
@ -1,22 +1,3 @@
|
|||||||
import spree from '@commerce/client';
|
|
||||||
import DiseasesAndConditions from 'components/diseases/diseases-and-conditions';
|
|
||||||
import Specialities from 'components/diseases/specialities';
|
|
||||||
import { cache } from 'react';
|
|
||||||
|
|
||||||
const getTaxons = cache(async (id: string) => {
|
|
||||||
console.log('getTaxons');
|
|
||||||
const res = await spree.taxons.list({});
|
|
||||||
if (res.isFail()) throw new Error('Failed to fetch data');
|
|
||||||
return res.success().data;
|
|
||||||
});
|
|
||||||
|
|
||||||
export default async function DiseasesPage() {
|
export default async function DiseasesPage() {
|
||||||
const taxons = await getTaxons();
|
return <div>Diseases page</div>;
|
||||||
|
|
||||||
return (
|
|
||||||
<div>
|
|
||||||
<Specialities taxons={taxons} />
|
|
||||||
<DiseasesAndConditions taxons={taxons} />
|
|
||||||
</div>
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
|
18
app/test-menu/[id]/page.tsx
Normal file
18
app/test-menu/[id]/page.tsx
Normal file
@ -0,0 +1,18 @@
|
|||||||
|
import marketApi from '@market/api';
|
||||||
|
import CartButton from 'components/tests/cart-button';
|
||||||
|
import { notFound } from 'next/navigation';
|
||||||
|
|
||||||
|
export default async function TestPage({ params }: { params: any }) {
|
||||||
|
const { id } = params;
|
||||||
|
const product = await marketApi.getProduct(id);
|
||||||
|
|
||||||
|
if (!product) return notFound();
|
||||||
|
|
||||||
|
return (
|
||||||
|
<div>
|
||||||
|
<h1>Test Detail for ID: {product.id}</h1>
|
||||||
|
<p>{product.attributes.name}</p>
|
||||||
|
<CartButton id={product.id} />
|
||||||
|
</div>
|
||||||
|
);
|
||||||
|
}
|
@ -1,9 +1,9 @@
|
|||||||
import commerceApi from '@commerce/api';
|
import marketApi from '@market/api';
|
||||||
import Alphabet from 'components/tests/alphabet';
|
import Alphabet from 'components/tests/alphabet';
|
||||||
import TestsTable from 'components/tests/tests-table';
|
import TestsTable from 'components/tests/tests-table';
|
||||||
|
|
||||||
export default async function TestsPage() {
|
export default async function TestsPage() {
|
||||||
const products = await commerceApi.getProducts();
|
const products = await marketApi.getProducts();
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<div>
|
<div>
|
@ -1,20 +0,0 @@
|
|||||||
import commerceApi from '@commerce/api';
|
|
||||||
import CartButton from 'components/tests/cart-button';
|
|
||||||
import { notFound } from 'next/navigation';
|
|
||||||
|
|
||||||
export default async function TestPage({ params }: { params: any }) {
|
|
||||||
const { id } = params;
|
|
||||||
const product = await commerceApi.getProduct(id);
|
|
||||||
|
|
||||||
if (!product) return notFound();
|
|
||||||
|
|
||||||
return (
|
|
||||||
<div>
|
|
||||||
<h1 className="mb-4 text-4xl font-extrabold leading-none tracking-tight text-gray-900 md:text-5xl lg:text-6xl dark:text-white">
|
|
||||||
Test Detail for ID: {product.id}
|
|
||||||
</h1>
|
|
||||||
<p className="mb-4 text-lg text-gray-600 dark:text-gray-400">{product.attributes.name}</p>
|
|
||||||
<CartButton id={product.id} />
|
|
||||||
</div>
|
|
||||||
);
|
|
||||||
}
|
|
@ -1,23 +1,23 @@
|
|||||||
import spree from '@commerce/client';
|
import spree from '@market/client';
|
||||||
import { IToken, ProductId } from '@commerce/types';
|
import { IToken, ProductId } from '@market/types';
|
||||||
import { ensureIToken, setCartToken } from '@commerce/utils';
|
import { ensureIToken, setCartToken } from '@market/utils';
|
||||||
import { FetchError, IOrder, extractSuccess } from '@spree/storefront-api-v2-sdk';
|
import { FetchError, IOrder, extractSuccess } from '@spree/storefront-api-v2-sdk';
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Checks if user authenticated, creates an empty cart (for user or guest)
|
* Checks if user authenticated, creates an empty cart (for user or guest)
|
||||||
* and returns cart @type {Cart}
|
* and returns cart @type {Cart}
|
||||||
*/
|
*/
|
||||||
const createEmptyCart = async () => {
|
async function createEmptyCart() {
|
||||||
const token: IToken | undefined = ensureIToken();
|
const token: IToken | undefined = ensureIToken();
|
||||||
const cartResponse = await extractSuccess(spree.cart.create(token));
|
const cartResponse = await extractSuccess(spree.cart.create(token));
|
||||||
return cartResponse;
|
return cartResponse;
|
||||||
};
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Checks if token is present,
|
* Checks if token is present,
|
||||||
* if not creates an empty cart and returns cart @type {Cart}
|
* if not creates an empty cart and returns cart @type {Cart}
|
||||||
*/
|
*/
|
||||||
export const addProductToCart = async (productId: ProductId): Promise<IOrder | null> => {
|
export async function addProductToCart(productId: ProductId): Promise<IOrder | null> {
|
||||||
let cartResponse: IOrder | null;
|
let cartResponse: IOrder | null;
|
||||||
|
|
||||||
let token = ensureIToken();
|
let token = ensureIToken();
|
||||||
@ -54,4 +54,4 @@ export const addProductToCart = async (productId: ProductId): Promise<IOrder | n
|
|||||||
}
|
}
|
||||||
|
|
||||||
return cartResponse;
|
return cartResponse;
|
||||||
};
|
}
|
||||||
|
@ -1,7 +1,7 @@
|
|||||||
import commerce from '@commerce/client';
|
import commerce from '@market/client';
|
||||||
import { ProductAttr, extractSuccess } from '@spree/storefront-api-v2-sdk';
|
import { ProductAttr, extractSuccess } from '@spree/storefront-api-v2-sdk';
|
||||||
|
|
||||||
export const getProduct = async (id: string): Promise<ProductAttr | null> => {
|
export async function getProduct(id: string): Promise<ProductAttr | null> {
|
||||||
let product = null;
|
let product = null;
|
||||||
|
|
||||||
const params = {
|
const params = {
|
||||||
@ -17,9 +17,9 @@ export const getProduct = async (id: string): Promise<ProductAttr | null> => {
|
|||||||
}
|
}
|
||||||
|
|
||||||
return product;
|
return product;
|
||||||
};
|
}
|
||||||
|
|
||||||
export const getProducts = async ({ taxons = [] } = {}): Promise<ProductAttr[]> => {
|
export async function getProducts({ taxons = [] } = {}): Promise<ProductAttr[]> {
|
||||||
const filter = { filter: { taxons: taxons.join(',') }, sort: '-updated_at' };
|
const filter = { filter: { taxons: taxons.join(',') }, sort: '-updated_at' };
|
||||||
|
|
||||||
const params = {
|
const params = {
|
||||||
@ -36,4 +36,4 @@ export const getProducts = async ({ taxons = [] } = {}): Promise<ProductAttr[]>
|
|||||||
const successResponse = await extractSuccess(commerce.products.list(params));
|
const successResponse = await extractSuccess(commerce.products.list(params));
|
||||||
|
|
||||||
return successResponse.data;
|
return successResponse.data;
|
||||||
};
|
}
|
||||||
|
0
lib/spree/types/index.ts
Normal file
0
lib/spree/types/index.ts
Normal file
@ -16,7 +16,7 @@
|
|||||||
"incremental": true,
|
"incremental": true,
|
||||||
"baseUrl": ".",
|
"baseUrl": ".",
|
||||||
"paths": {
|
"paths": {
|
||||||
"@commerce/*": ["lib/spree/*"],
|
"@market/*": ["lib/spree/*"],
|
||||||
"@styles/*": ["styles/*"]
|
"@styles/*": ["styles/*"]
|
||||||
},
|
},
|
||||||
"noUncheckedIndexedAccess": true,
|
"noUncheckedIndexedAccess": true,
|
||||||
|
Loading…
x
Reference in New Issue
Block a user