refactoring

This commit is contained in:
Timur Suleymanov 2024-06-19 18:51:31 +05:00
parent 83ae628672
commit fcfe7fbcb0
12 changed files with 3571 additions and 55 deletions

View File

@ -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() {
const taxons = await getTaxons();
return (
<div>
<Specialities taxons={taxons} />
<DiseasesAndConditions taxons={taxons} />
</div>
);
return <div>Diseases page</div>;
}

View 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>
);
}

View File

@ -1,9 +1,9 @@
import commerceApi from '@commerce/api';
import marketApi from '@market/api';
import Alphabet from 'components/tests/alphabet';
import TestsTable from 'components/tests/tests-table';
export default async function TestsPage() {
const products = await commerceApi.getProducts();
const products = await marketApi.getProducts();
return (
<div>

View File

@ -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>
);
}

View File

@ -1,23 +1,23 @@
import spree from '@commerce/client';
import { IToken, ProductId } from '@commerce/types';
import { ensureIToken, setCartToken } from '@commerce/utils';
import spree from '@market/client';
import { IToken, ProductId } from '@market/types';
import { ensureIToken, setCartToken } from '@market/utils';
import { FetchError, IOrder, extractSuccess } from '@spree/storefront-api-v2-sdk';
/**
* Checks if user authenticated, creates an empty cart (for user or guest)
* and returns cart @type {Cart}
*/
const createEmptyCart = async () => {
async function createEmptyCart() {
const token: IToken | undefined = ensureIToken();
const cartResponse = await extractSuccess(spree.cart.create(token));
return cartResponse;
};
}
/**
* Checks if token is present,
* 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 token = ensureIToken();
@ -54,4 +54,4 @@ export const addProductToCart = async (productId: ProductId): Promise<IOrder | n
}
return cartResponse;
};
}

View File

@ -1,7 +1,7 @@
import commerce from '@commerce/client';
import commerce from '@market/client';
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;
const params = {
@ -17,9 +17,9 @@ export const getProduct = async (id: string): Promise<ProductAttr | null> => {
}
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 params = {
@ -36,4 +36,4 @@ export const getProducts = async ({ taxons = [] } = {}): Promise<ProductAttr[]>
const successResponse = await extractSuccess(commerce.products.list(params));
return successResponse.data;
};
}

0
lib/spree/types/index.ts Normal file
View File

View File

@ -16,7 +16,7 @@
"incremental": true,
"baseUrl": ".",
"paths": {
"@commerce/*": ["lib/spree/*"],
"@market/*": ["lib/spree/*"],
"@styles/*": ["styles/*"]
},
"noUncheckedIndexedAccess": true,

3537
yarn.lock Normal file

File diff suppressed because it is too large Load Diff