调整购买页面价格显示
This commit is contained in:
@@ -11,7 +11,7 @@ import {FieldPayment} from './field-payment'
|
||||
import {buildPurchaseResource, PurchaseKind, PurchaseSelection} from './resource'
|
||||
import {getPrice, getPriceHome} from '@/actions/resource'
|
||||
import {ExtraResp} from '@/lib/api'
|
||||
import {formatPurchaseLiveLabel} from './sku'
|
||||
import {formatPurchaseLiveLabel, getPurchaseSkuCountMin, PurchaseSkuData} from './sku'
|
||||
import {User} from '@/lib/models'
|
||||
import {PurchaseFormValues} from './form-values'
|
||||
import {IdCard} from 'lucide-react'
|
||||
@@ -25,6 +25,7 @@ const emptyPrice: ExtraResp<typeof getPrice> = {
|
||||
|
||||
export type PurchaseSidePanelProps = {
|
||||
kind: PurchaseKind
|
||||
skuData: PurchaseSkuData
|
||||
}
|
||||
|
||||
export function PurchaseSidePanel(props: PurchaseSidePanelProps) {
|
||||
@@ -44,7 +45,7 @@ export function PurchaseSidePanel(props: PurchaseSidePanelProps) {
|
||||
expire,
|
||||
dailyLimit,
|
||||
}
|
||||
const {priceData, isLoading, isError} = usePurchasePrice(profile, selection)
|
||||
const {priceData, isLoading, isError} = usePurchasePrice(profile, selection, props.skuData)
|
||||
const {price, actual: discountedPrice = '0.00'} = priceData
|
||||
const totalDiscount = getTotalDiscount(price, discountedPrice)
|
||||
const hasDiscount = Number(totalDiscount) > 0
|
||||
@@ -154,7 +155,7 @@ export function PurchaseSidePanel(props: PurchaseSidePanelProps) {
|
||||
)
|
||||
}
|
||||
|
||||
function usePurchasePrice(profile: User | null, selection: PurchaseSelection) {
|
||||
function usePurchasePrice(profile: User | null, selection: PurchaseSelection, skuData: PurchaseSkuData) {
|
||||
const [priceData, setPriceData] = useState<ExtraResp<typeof getPrice>>(emptyPrice)
|
||||
const [isLoading, setIsLoading] = useState(true)
|
||||
const [isError, setIsError] = useState(false)
|
||||
@@ -164,6 +165,15 @@ function usePurchasePrice(profile: User | null, selection: PurchaseSelection) {
|
||||
useEffect(() => {
|
||||
const requestId = ++requestIdRef.current
|
||||
|
||||
const expireValue = mode === '1' ? expire : '0'
|
||||
const countMin = getPurchaseSkuCountMin(skuData, {mode, live, expire: expireValue})
|
||||
const quantity = mode === '1' ? dailyLimit : quota
|
||||
if (countMin > 0 && quantity < countMin) {
|
||||
setIsLoading(false)
|
||||
setIsError(false)
|
||||
return
|
||||
}
|
||||
|
||||
const loadPrice = async () => {
|
||||
setIsLoading(true)
|
||||
setIsError(false)
|
||||
@@ -177,6 +187,7 @@ function usePurchasePrice(profile: User | null, selection: PurchaseSelection) {
|
||||
expire,
|
||||
dailyLimit,
|
||||
})
|
||||
|
||||
const response = profile
|
||||
? await getPrice(resource)
|
||||
: await getPriceHome(resource)
|
||||
@@ -184,7 +195,9 @@ function usePurchasePrice(profile: User | null, selection: PurchaseSelection) {
|
||||
if (requestId !== requestIdRef.current) {
|
||||
return
|
||||
}
|
||||
|
||||
if (!response.success) {
|
||||
throw new Error(response.message)
|
||||
}
|
||||
if (response.success) {
|
||||
setPriceData({
|
||||
price: response.data.price,
|
||||
@@ -211,7 +224,7 @@ function usePurchasePrice(profile: User | null, selection: PurchaseSelection) {
|
||||
}
|
||||
|
||||
loadPrice()
|
||||
}, [dailyLimit, expire, kind, live, mode, profile, quota])
|
||||
}, [dailyLimit, expire, kind, live, mode, profile, quota, skuData])
|
||||
|
||||
return {priceData, isLoading, isError}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user