@caseparts-org/casecore 0.0.9 → 0.0.11
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/cart/ClassPricing.d.ts +7 -0
- package/dist/cart/ClassPricing.js +10 -0
- package/dist/cart/CreateCart.d.ts +31 -0
- package/dist/cart/CreateCart.js +65 -0
- package/dist/cart/DomainCartProvider.d.ts +5 -0
- package/dist/cart/DomainCartProvider.js +11 -0
- package/dist/index.d.ts +5 -0
- package/dist/index.js +3 -0
- package/package.json +1 -1
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
export type CustomerClass = 'List' | 'Dealer' | 'Wholesale';
|
|
2
|
+
export interface Pricing {
|
|
3
|
+
ListPrice: number;
|
|
4
|
+
DealerPrice: number;
|
|
5
|
+
WholesalePrice: number;
|
|
6
|
+
}
|
|
7
|
+
export declare function getClassPricing(custClass: CustomerClass | undefined, pricing?: Pricing): number | undefined;
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
export function getClassPricing(custClass, pricing) {
|
|
2
|
+
if (!pricing)
|
|
3
|
+
return undefined;
|
|
4
|
+
switch (custClass) {
|
|
5
|
+
case 'Dealer': return pricing.DealerPrice;
|
|
6
|
+
case 'Wholesale': return pricing.WholesalePrice;
|
|
7
|
+
case 'List':
|
|
8
|
+
default: return pricing.ListPrice;
|
|
9
|
+
}
|
|
10
|
+
}
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
import React from 'react';
|
|
2
|
+
import { Pricing } from './ClassPricing';
|
|
3
|
+
export type PricingResolver<T extends BaseCartItem> = (_item: T) => number;
|
|
4
|
+
export interface BaseCartItem {
|
|
5
|
+
ItemKey: number | string;
|
|
6
|
+
Quantity: number;
|
|
7
|
+
Pricing?: Pricing | undefined;
|
|
8
|
+
[k: string]: any;
|
|
9
|
+
}
|
|
10
|
+
export interface Cart<T extends BaseCartItem = BaseCartItem> {
|
|
11
|
+
items: T[];
|
|
12
|
+
}
|
|
13
|
+
export interface CartConfig<T extends BaseCartItem> {
|
|
14
|
+
storageKey?: string;
|
|
15
|
+
pricingResolver?: PricingResolver<T>;
|
|
16
|
+
}
|
|
17
|
+
export interface CartContextValue<T extends BaseCartItem> {
|
|
18
|
+
cart: Cart<T>;
|
|
19
|
+
updateCart: (_cart: Cart<T>) => void;
|
|
20
|
+
cartSubtotal: () => number;
|
|
21
|
+
initializedCart: boolean;
|
|
22
|
+
isMobile: boolean;
|
|
23
|
+
}
|
|
24
|
+
export type CartProviderProps<T extends BaseCartItem> = React.PropsWithChildren<{
|
|
25
|
+
pricingResolver?: PricingResolver<T>;
|
|
26
|
+
}>;
|
|
27
|
+
export interface CreateCartReturn<T extends BaseCartItem> {
|
|
28
|
+
CartProvider: React.FC<CartProviderProps<T>>;
|
|
29
|
+
useCartContext: () => CartContextValue<T>;
|
|
30
|
+
}
|
|
31
|
+
export declare function createCart<T extends BaseCartItem = BaseCartItem>(config?: CartConfig<T>): CreateCartReturn<T>;
|
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
import { jsx as _jsx } from "react/jsx-runtime";
|
|
2
|
+
import { createContext, useContext, useState, useCallback, useMemo, useEffect } from 'react';
|
|
3
|
+
export function createCart(config) {
|
|
4
|
+
const STORAGE_KEY = config?.storageKey ?? 'CPC.cart';
|
|
5
|
+
const CartContext = createContext(undefined);
|
|
6
|
+
const CartProvider = ({ children, pricingResolver }) => {
|
|
7
|
+
const [cart, setCart] = useState({ items: [] });
|
|
8
|
+
const [initialized, setInitialized] = useState(false);
|
|
9
|
+
const [isMobile, setIsMobile] = useState(false);
|
|
10
|
+
const resolver = pricingResolver ?? config?.pricingResolver;
|
|
11
|
+
// Initialize cart from local storage
|
|
12
|
+
useEffect(() => {
|
|
13
|
+
try {
|
|
14
|
+
const rawCart = typeof window !== 'undefined' ? window.localStorage.getItem(STORAGE_KEY) : null;
|
|
15
|
+
if (rawCart) {
|
|
16
|
+
const parsed = JSON.parse(rawCart);
|
|
17
|
+
if (parsed?.items && Array.isArray(parsed.items)) {
|
|
18
|
+
setCart({ items: parsed.items });
|
|
19
|
+
}
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
catch {
|
|
23
|
+
/* ignore */
|
|
24
|
+
}
|
|
25
|
+
if (typeof navigator !== 'undefined') {
|
|
26
|
+
setIsMobile(/Android|BlackBerry|iPhone|iPad|iPod|Opera Mini|IEMobile|WPDesktop/i.test(navigator.userAgent));
|
|
27
|
+
}
|
|
28
|
+
setInitialized(true);
|
|
29
|
+
}, []);
|
|
30
|
+
const save = useCallback((c) => {
|
|
31
|
+
try {
|
|
32
|
+
window.localStorage.setItem(STORAGE_KEY, JSON.stringify(c));
|
|
33
|
+
}
|
|
34
|
+
catch { /* suppress */ }
|
|
35
|
+
}, []);
|
|
36
|
+
const updateCart = useCallback((c) => {
|
|
37
|
+
setCart({ items: [...c.items] });
|
|
38
|
+
save(c);
|
|
39
|
+
}, [save]);
|
|
40
|
+
const cartSubtotal = useCallback(() => {
|
|
41
|
+
if (!cart.items.length)
|
|
42
|
+
return 0;
|
|
43
|
+
return cart.items.reduce((sum, item) => {
|
|
44
|
+
const price = resolver ? resolver(item) : 0;
|
|
45
|
+
const qty = item.Quantity ?? 1;
|
|
46
|
+
return sum + price * qty;
|
|
47
|
+
}, 0);
|
|
48
|
+
}, [cart.items, resolver]);
|
|
49
|
+
const value = useMemo(() => ({
|
|
50
|
+
cart,
|
|
51
|
+
updateCart,
|
|
52
|
+
cartSubtotal,
|
|
53
|
+
initializedCart: initialized,
|
|
54
|
+
isMobile
|
|
55
|
+
}), [cart, updateCart, cartSubtotal, initialized, isMobile]);
|
|
56
|
+
return _jsx(CartContext.Provider, { value: value, children: children });
|
|
57
|
+
};
|
|
58
|
+
const useCartContext = () => {
|
|
59
|
+
const cartContext = useContext(CartContext);
|
|
60
|
+
if (!cartContext)
|
|
61
|
+
throw new Error('useCartContext must be used inside CartProvider');
|
|
62
|
+
return cartContext;
|
|
63
|
+
};
|
|
64
|
+
return { CartProvider, useCartContext };
|
|
65
|
+
}
|
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
import { BaseCartItem } from './CreateCart';
|
|
2
|
+
export declare const useCartContext: () => import("./CreateCart").CartContextValue<BaseCartItem>;
|
|
3
|
+
export declare function DomainCartProvider({ children }: {
|
|
4
|
+
children: React.ReactNode;
|
|
5
|
+
}): import("react/jsx-runtime").JSX.Element;
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import { jsx as _jsx } from "react/jsx-runtime";
|
|
2
|
+
import { createCart } from './CreateCart';
|
|
3
|
+
import { useAuthContext } from '../authentication/AuthContext';
|
|
4
|
+
import { getClassPricing } from './ClassPricing';
|
|
5
|
+
const CartApi = createCart({ storageKey: 'CPC.cart' });
|
|
6
|
+
export const { useCartContext } = CartApi;
|
|
7
|
+
export function DomainCartProvider({ children }) {
|
|
8
|
+
const auth = useAuthContext();
|
|
9
|
+
const resolver = (item) => getClassPricing(auth.discountLevel, item.Pricing) ?? 0;
|
|
10
|
+
return (_jsx(CartApi.CartProvider, { pricingResolver: resolver, children: children }));
|
|
11
|
+
}
|
package/dist/index.d.ts
CHANGED
|
@@ -6,3 +6,8 @@ export { getSessionId } from './utils/SessionUtils';
|
|
|
6
6
|
export { buildClaimsFromPayload } from './utils/ClaimsUtils';
|
|
7
7
|
export type { MicroFlexFieldSelectors, MicroFlexOptions, MicroFlexReturn } from './hooks/useMicroFlex';
|
|
8
8
|
export { default as useMicroFlex } from './hooks/useMicroFlex';
|
|
9
|
+
export type { Pricing, CustomerClass } from './cart/ClassPricing';
|
|
10
|
+
export { getClassPricing } from './cart/ClassPricing';
|
|
11
|
+
export type { BaseCartItem, Cart, CartConfig, CartContextValue, CreateCartReturn, PricingResolver, CartProviderProps } from './cart/CreateCart';
|
|
12
|
+
export { createCart } from './cart/CreateCart';
|
|
13
|
+
export { useCartContext, DomainCartProvider } from './cart/DomainCartProvider';
|
package/dist/index.js
CHANGED
|
@@ -5,3 +5,6 @@ export { default as useLocalStorage } from './hooks/useLocalStorage';
|
|
|
5
5
|
export { getSessionId } from './utils/SessionUtils';
|
|
6
6
|
export { buildClaimsFromPayload } from './utils/ClaimsUtils';
|
|
7
7
|
export { default as useMicroFlex } from './hooks/useMicroFlex';
|
|
8
|
+
export { getClassPricing } from './cart/ClassPricing';
|
|
9
|
+
export { createCart } from './cart/CreateCart';
|
|
10
|
+
export { useCartContext, DomainCartProvider } from './cart/DomainCartProvider';
|