105 lines
2.6 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import { EnumSaleMode } from '@/common/Enumerate'
import React, { useRef } from 'react'
import { useContext } from 'react'
/**
* 456: {
* purchaserId: 456,
* colorKind: {
* 4562: {
* id: 4562,
* checked: false
* }
* }
* },
* 457: {
* purchaserId: 457,
* colorKind: {
* 4562: {
* id: 4562,
* checked: false
* }
* }
* }
*/
export type ColorStore = {
[purchaserId: GoodsMeta['purchaserId']]: GoodsMeta
}
export type Goods = {
id: number
estimate_amount: number // 预估金额
checked: boolean
}
// 分组
export interface GoodsMeta {
purchaserId: number
goodsKind?: {
[id: number]: Goods
}
checked: boolean
}
export interface ShoppingContextValue {
isManageStatus: boolean
setManageStatus: (isManageStatus: boolean) => void
currentCheckedPurchaserId: number // 当前高亮的客户ID
setCurrentCheckedPurchaserId: (purchaserId: number) => void
currentCheckedSaleMode: number
setCurrentCheckedSaleMode: (saleMode: number) => void
colorStore: ColorStore
setColorStore: (colorStore: React.SetStateAction<ColorStore>) => void
setChangedCheckbox: (changedGoods: GoodsMeta) => void
selectedAmount: number
setSelectedAmount: (amount: React.SetStateAction<number>) => void
}
export const ShoppingContext = React.createContext<ShoppingContextValue | null>(null)
export function useShoppingContext() {
const ctx = useContext(ShoppingContext)
if (!ctx) {
throw new Error('没有获取到shopping context')
}
return ctx
}
export function useWatch(purchaserId: GoodsMeta['purchaserId'], id: number) {
const { colorStore } = useShoppingContext()
return id ? colorStore[purchaserId]['goodsKind']![id] : colorStore[purchaserId]['goodsKind']
}
export interface ShoppingCartAction {
setManageStatus: (manageStatus: boolean) => void
getManageStatus: () => boolean
}
export interface InternalShoppingCartAction extends ShoppingCartAction {
__INTERNAL__: React.MutableRefObject<ShoppingCartAction | null>
}
function throwError(): never {
throw new Error('有没有用 ref 这个props')
}
export function useShoppingCart(): ShoppingCartAction {
const __INTERNAL__ = useRef<ShoppingCartAction | null>(null)
return {
__INTERNAL__,
setManageStatus(manageStatus: boolean) {
const action = __INTERNAL__.current
if (!action) {
throwError()
}
action.setManageStatus(manageStatus)
},
getManageStatus() {
const action = __INTERNAL__.current
if (!action) {
throwError()
}
return action.getManageStatus()
},
} as InternalShoppingCartAction
}