From 957768d87bc78536e3faabb00c97aa3353a82221 Mon Sep 17 00:00:00 2001 From: Logos as a philosopher Date: Fri, 18 Oct 2024 19:28:39 +0800 Subject: [PATCH] fix: discount selection card should still present even no available coupons --- .../MultiPeriodCouponSelectionModal.tsx | 88 +++++++++---------- .../MultiPeriodDiscountSelectionCard.tsx | 85 +++++++++--------- 2 files changed, 82 insertions(+), 91 deletions(-) diff --git a/src/components/appointment/MultiPeriodCouponSelectionModal.tsx b/src/components/appointment/MultiPeriodCouponSelectionModal.tsx index 62935e1e1..663e6299f 100644 --- a/src/components/appointment/MultiPeriodCouponSelectionModal.tsx +++ b/src/components/appointment/MultiPeriodCouponSelectionModal.tsx @@ -10,7 +10,7 @@ import { handleError } from 'lodestar-app-element/src/helpers' import { checkoutMessages, commonMessages } from 'lodestar-app-element/src/helpers/translation' import { useToastMessage } from 'lodestar-app-element/src/hooks/util' import { CouponProps, OrderDiscountProps, OrderProductProps } from 'lodestar-app-element/src/types/checkout' -import { sum } from 'ramda' +import { always, ifElse, isEmpty, map, sum } from 'ramda' import React, { useState } from 'react' import { useIntl } from 'react-intl' import styled from 'styled-components' @@ -27,7 +27,6 @@ const MultiPeriodCouponSelectionModal: React.VFC<{ memberId: string orderProducts: OrderProductProps[] orderDiscounts: OrderDiscountProps[] - currentlyUsedDiscountIds: Array coupons: CouponProps[] loadingCoupons: boolean refetchCoupons: ( @@ -35,16 +34,7 @@ const MultiPeriodCouponSelectionModal: React.VFC<{ ) => Promise> renderTrigger: (params: { onOpen: () => void; selectedCoupon?: CouponProps }) => React.ReactElement onSelect?: (coupon: CouponProps) => void -}> = ({ - orderProducts, - orderDiscounts, - currentlyUsedDiscountIds, - coupons, - loadingCoupons, - refetchCoupons, - onSelect, - renderTrigger, -}) => { +}> = ({ orderProducts, orderDiscounts, coupons, loadingCoupons, refetchCoupons, onSelect, renderTrigger }) => { const { formatMessage } = useIntl() const { authToken } = useAuth() @@ -91,44 +81,50 @@ const MultiPeriodCouponSelectionModal: React.VFC<{ {loadingCoupons ? ( ) : ( - coupons.map(coupon => { - const couponPlanScope = coupon.couponCode.couponPlan.scope - const couponPlanProductIds = coupon.couponCode.couponPlan.productIds || [] - const isInCouponScope = (productId: string) => { - const [productType] = productId.split('_') - return ( - couponPlanScope === null || - couponPlanScope.includes(productType) || - couponPlanProductIds.includes(productId) - ) - } + ifElse( + isEmpty, + always(

目前無可用折價券,請於下列輸入框中輸入代碼,新增折價券:

), + map((coupon: CouponProps) => { + const couponPlanScope = coupon.couponCode.couponPlan.scope + const couponPlanProductIds = coupon.couponCode.couponPlan.productIds || [] + const isInCouponScope = (productId: string) => { + const [productType] = productId.split('_') + return ( + couponPlanScope === null || + couponPlanScope.includes(productType) || + couponPlanProductIds.includes(productId) + ) + } - const filteredOrderProducts = orderProducts.filter(orderProduct => isInCouponScope(orderProduct.productId)) - const filteredOrderDiscounts = orderDiscounts.filter(orderDiscount => orderDiscount.type === 'DownPrice') - const price = - sum(filteredOrderProducts.map(orderProduct => orderProduct.price)) - - sum(filteredOrderDiscounts.map(orderDiscount => orderDiscount.price)) + const filteredOrderProducts = orderProducts.filter(orderProduct => + isInCouponScope(orderProduct.productId), + ) + const filteredOrderDiscounts = orderDiscounts.filter(orderDiscount => orderDiscount.type === 'DownPrice') + const price = + sum(filteredOrderProducts.map(orderProduct => orderProduct.price)) - + sum(filteredOrderDiscounts.map(orderDiscount => orderDiscount.price)) - const isDisabled = filteredOrderProducts.length === 0 || coupon.couponCode.couponPlan.constraint > price - return ( - { - if (isDisabled) { - return - } - onSelect && onSelect(coupon) - setSelectedCoupon(coupon) - setVisible(false) - }} - isDisabled={isDisabled} - /> - ) - }) + const isDisabled = filteredOrderProducts.length === 0 || coupon.couponCode.couponPlan.constraint > price + return ( + { + if (isDisabled) { + return + } + onSelect && onSelect(coupon) + setSelectedCoupon(coupon) + setVisible(false) + }} + isDisabled={isDisabled} + /> + ) + }), + )(coupons) )} - {formatMessage(commonMessages.label.or)} + {coupons.length > 0 ? {formatMessage(commonMessages.label.or)} : <>}
diff --git a/src/components/appointment/MultiPeriodDiscountSelectionCard.tsx b/src/components/appointment/MultiPeriodDiscountSelectionCard.tsx index 056da430d..12371f6ec 100644 --- a/src/components/appointment/MultiPeriodDiscountSelectionCard.tsx +++ b/src/components/appointment/MultiPeriodDiscountSelectionCard.tsx @@ -46,51 +46,46 @@ const DiscountSelectionCard: React.VFC<{ {formatMessage(checkoutMessages.form.radio.noDiscount)} - {discountId || leftCoupons.length > 0 ? ( -
- - {formatMessage(checkoutMessages.form.radio.useCoupon)} - - {discountType === 'Coupon' && ( - - {currentMemberId ? ( - { - onChange?.(`Coupon_${coupon.id}`) - }} - renderTrigger={({ onOpen, selectedCoupon }) => ( - <> - - {selectedCoupon && ( - - {selectedCoupon.couponCode.couponPlan.title} {selectedCoupon.couponCode.code} - - )} - - )} - /> - ) : ( - - )} - - )} -
- ) : ( - <> - )} +
+ + {formatMessage(checkoutMessages.form.radio.useCoupon)} + + {discountType === 'Coupon' && ( + + {currentMemberId ? ( + { + onChange?.(`Coupon_${coupon.id}`) + }} + renderTrigger={({ onOpen, selectedCoupon }) => ( + <> + + {selectedCoupon && ( + + {selectedCoupon.couponCode.couponPlan.title} {selectedCoupon.couponCode.code} + + )} + + )} + /> + ) : ( + + )} + + )} +
{enrolledMembershipCardIds.length > 0 && ( {formatMessage(checkoutMessages.content.useMemberCard)}