From 51d2215942bea10d6d5f8f95afa8688fa698aa2f Mon Sep 17 00:00:00 2001 From: MorganeLecurieux Date: Thu, 2 Jun 2022 16:56:27 +0200 Subject: [PATCH] =?UTF-8?q?fix:=20=F0=9F=91=80=20QA=20Add-ons?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../customers/AddAddOnToCustomerDialog.tsx | 2 +- src/components/customers/CustomerAddOns.tsx | 25 +-- src/components/customers/CustomerCoupons.tsx | 173 +++++++--------- .../customers/CustomerSubscriptionsList.tsx | 196 +++++++----------- src/pages/CustomerDetails.tsx | 71 ++++--- 5 files changed, 207 insertions(+), 260 deletions(-) diff --git a/src/components/customers/AddAddOnToCustomerDialog.tsx b/src/components/customers/AddAddOnToCustomerDialog.tsx index 617c5002e..8189ba1eb 100644 --- a/src/components/customers/AddAddOnToCustomerDialog.tsx +++ b/src/components/customers/AddAddOnToCustomerDialog.tsx @@ -120,7 +120,7 @@ export const AddAddOnToCustomerDialog = forwardRef< ( {translate('text_629781ec7c6c1500d94fbc16', { - amount: formatAmountToCurrency(amountCents || 0, { + amountWithCurrency: formatAmountToCurrency(amountCents || 0, { currencyDisplay: 'code', currency: amountCurrency, }), diff --git a/src/components/customers/CustomerAddOns.tsx b/src/components/customers/CustomerAddOns.tsx index 324100e4b..be645c722 100644 --- a/src/components/customers/CustomerAddOns.tsx +++ b/src/components/customers/CustomerAddOns.tsx @@ -1,5 +1,5 @@ /* eslint-disable react/prop-types */ -import { useRef, useImperativeHandle, forwardRef, memo } from 'react' +import { forwardRef, memo, MutableRefObject } from 'react' import { gql } from '@apollo/client' import styled from 'styled-components' import { DateTime } from 'luxon' @@ -11,7 +11,7 @@ import { Typography, Avatar, Icon, Button } from '~/components/designSystem' import { theme, HEADER_TABLE_HEIGHT, NAV_HEIGHT } from '~/styles' import { formatAmountToCurrency } from '~/core/currencyTool' -import { AddAddOnToCustomerDialog, AddAddOnToCustomerDialogRef } from './AddAddOnToCustomerDialog' +import { AddAddOnToCustomerDialogRef } from './AddAddOnToCustomerDialog' gql` fragment CustomerAddOns on AppliedAddOn { @@ -26,27 +26,15 @@ gql` } ` -export interface CustomerAddOnsListRef { - openAddAddOnDialog: () => void -} - interface CustomerAddOnsProps { - customerId: string addOns?: CustomerAddOnsFragment[] | null | undefined } export const CustomerAddOns = memo( - forwardRef( - ({ addOns, customerId }: CustomerAddOnsProps, ref) => { - const addPlanToCustomerDialogRef = useRef(null) + forwardRef( + ({ addOns }: CustomerAddOnsProps, ref) => { const { translate } = useI18nContext() - useImperativeHandle(ref, () => ({ - openAddAddOnDialog: () => { - addPlanToCustomerDialogRef?.current?.openDialog() - }, - })) - return ( <> {!!(addOns || [])?.length && ( @@ -55,7 +43,9 @@ export const CustomerAddOns = memo( {translate('text_629781ec7c6c1500d94fbbfe')} @@ -95,7 +85,6 @@ export const CustomerAddOns = memo( ))} )} - ) } diff --git a/src/components/customers/CustomerCoupons.tsx b/src/components/customers/CustomerCoupons.tsx index 07ddf7189..6c6d01390 100644 --- a/src/components/customers/CustomerCoupons.tsx +++ b/src/components/customers/CustomerCoupons.tsx @@ -1,5 +1,5 @@ /* eslint-disable react/prop-types */ -import { useRef, useImperativeHandle, forwardRef, memo } from 'react' +import { useRef, memo } from 'react' import { gql } from '@apollo/client' import styled from 'styled-components' @@ -12,11 +12,6 @@ import { formatAmountToCurrency } from '~/core/currencyTool' import { WarningDialog, WarningDialogRef } from '~/components/WarningDialog' import { addToast } from '~/core/apolloClient' -import { - AddCouponToCustomerDialog, - AddCouponToCustomerDialogRef, -} from './AddCouponToCustomerDialog' - gql` fragment CustomerCoupon on AppliedCoupon { id @@ -35,106 +30,86 @@ gql` } ` -export interface CustomerCouponsListRef { - openAddCouponDialog: () => void -} - interface CustomerCouponsProps { - customerId: string coupons?: CustomerCouponFragment[] | null | undefined } -export const CustomerCoupons = memo( - forwardRef( - ({ coupons, customerId }: CustomerCouponsProps, ref) => { - const addPlanToCustomerDialogRef = useRef(null) - const removeDialogRef = useRef(null) - const deleteCouponId = useRef(null) - const { translate } = useI18nContext() - const [removeCoupon] = useRemoveCouponMutation({ - onCompleted({ terminateAppliedCoupon }) { - if (!!terminateAppliedCoupon) { - addToast({ - severity: 'success', - message: translate('text_628b8c693e464200e00e49d1'), +export const CustomerCoupons = memo(({ coupons }: CustomerCouponsProps) => { + const removeDialogRef = useRef(null) + const deleteCouponId = useRef(null) + const { translate } = useI18nContext() + const [removeCoupon] = useRemoveCouponMutation({ + onCompleted({ terminateAppliedCoupon }) { + if (!!terminateAppliedCoupon) { + addToast({ + severity: 'success', + message: translate('text_628b8c693e464200e00e49d1'), + }) + } + }, + }) + + return ( + <> + {!!(coupons || [])?.length && ( + + + {translate('text_628b8c693e464200e00e469d')} + + + + {translate('text_628b8c693e464200e00e46ab')} + + + {(coupons || []).map(({ amountCents, amountCurrency, coupon, id }) => ( + + + + + + + {coupon?.name} + + + {translate('text_62865498824cc10126ab2976', { + amount: formatAmountToCurrency(amountCents || 0, { + currencyDisplay: 'code', + currency: amountCurrency, + }), + })} + + + + - - {hasNoSubscription ? ( - {translate('text_6250304370f0f700a8fdc28f')} - ) : ( - <> - - - {translate('text_6253f11816f710014600b9ed')} - - - {translate('text_6253f11816f710014600b9ef')} - - - {translate('text_6253f11816f710014600b9f1')} - - - {subscriptions.map(({ id, plan, status, startedAt, pendingStartDate }) => { - const statusConfig = mapStatus(status) - - return ( - - - - - - - - {plan.name} - - - {plan.code} - - - - - - {DateTime.fromISO(startedAt ?? pendingStartDate).toFormat('yyyy/LL/dd')} - - - ) - })} - {downgradingTo && ( - - {translate('text_62681c60582e4f00aa82938a', { - planName: downgradingTo?.plan?.name, - dateStartNewPlan: DateTime.fromISO(downgradingTo?.pendingStartDate).toFormat( - 'yyyy/LL/dd' - ), - })} - - )} - - )} - - - - ) - } -) + > + {hasNoSubscription + ? translate('text_6250304370f0f700a8fdc28b') + : translate('text_6253f11816f710014600b9e9')} + + + {hasNoSubscription ? ( + {translate('text_6250304370f0f700a8fdc28f')} + ) : ( + <> + + + {translate('text_6253f11816f710014600b9ed')} + + + {translate('text_6253f11816f710014600b9ef')} + + + {translate('text_6253f11816f710014600b9f1')} + + + {subscriptions.map(({ id, plan, status, startedAt, pendingStartDate }) => { + const statusConfig = mapStatus(status) + + return ( + + + + + + + + {plan.name} + + + {plan.code} + + + + + + {DateTime.fromISO(startedAt ?? pendingStartDate).toFormat('yyyy/LL/dd')} + + + ) + })} + {downgradingTo && ( + + {translate('text_62681c60582e4f00aa82938a', { + planName: downgradingTo?.plan?.name, + dateStartNewPlan: DateTime.fromISO(downgradingTo?.pendingStartDate).toFormat( + 'yyyy/LL/dd' + ), + })} + + )} + + )} + + ) +}) CustomerSubscriptionsList.displayName = 'CustomerSubscriptionsList' diff --git a/src/pages/CustomerDetails.tsx b/src/pages/CustomerDetails.tsx index 518fe1c4c..a9bae4552 100644 --- a/src/pages/CustomerDetails.tsx +++ b/src/pages/CustomerDetails.tsx @@ -24,13 +24,11 @@ import { CustomerCouponFragmentDoc, CustomerMainInfosFragmentDoc, CustomerAddOnsFragmentDoc, + StatusTypeEnum, } from '~/generated/graphql' import { GenericPlaceholder } from '~/components/GenericPlaceholder' import EmojiError from '~/public/images/exploding-head.png' -import { - CustomerSubscriptionsList, - CustomerSubscriptionsListRef, -} from '~/components/customers/CustomerSubscriptionsList' +import { CustomerSubscriptionsList } from '~/components/customers/CustomerSubscriptionsList' import { CustomerInvoicesList } from '~/components/customers/CustomerInvoicesList' import { CustomerVatRate } from '~/components/customers/CustomerVatRate' import { theme, PageHeader, MenuPopper } from '~/styles' @@ -40,9 +38,21 @@ import { DeleteCustomerDialogRef, } from '~/components/customers/DeleteCustomerDialog' import { AddCustomerDialog, AddCustomerDialogRef } from '~/components/customers/AddCustomerDialog' -import { CustomerCoupons, CustomerCouponsListRef } from '~/components/customers/CustomerCoupons' -import { CustomerAddOns, CustomerAddOnsListRef } from '~/components/customers/CustomerAddOns' +import { CustomerCoupons } from '~/components/customers/CustomerCoupons' +import { CustomerAddOns } from '~/components/customers/CustomerAddOns' import { CustomerMainInfos } from '~/components/customers/CustomerMainInfos' +import { + AddCouponToCustomerDialog, + AddCouponToCustomerDialogRef, +} from '~/components/customers/AddCouponToCustomerDialog' +import { + AddAddOnToCustomerDialog, + AddAddOnToCustomerDialogRef, +} from '~/components/customers/AddAddOnToCustomerDialog' +import { + AddPlanToCustomerDialog, + AddPlanToCustomerDialogRef, +} from '~/components/customers/AddPlanToCustomerDialog' gql` fragment CustomerDetails on CustomerDetails { @@ -91,9 +101,9 @@ enum TabsOptions { const CustomerDetails = () => { const deleteDialogRef = useRef(null) const editDialogRef = useRef(null) - const couponListRef = useRef(null) - const addOnListRef = useRef(null) - const subscriptionsListRef = useRef(null) + const addCouponDialogRef = useRef(null) + const addOnDialogRef = useRef(null) + const subscriptionsDialogRef = useRef(null) const { translate } = useI18nContext() const navigate = useNavigate() const { id, tab } = useParams() @@ -121,6 +131,17 @@ const CustomerDetails = () => { ] }, [translate]) + const selectedPlansId = useMemo( + () => + (subscriptions || []).reduce((acc, s) => { + if ([StatusTypeEnum.Active, StatusTypeEnum.Pending].includes(s.status as StatusTypeEnum)) { + acc.push(s.plan?.id) + } + return acc + }, []), + [subscriptions] + ) + return (
@@ -150,7 +171,7 @@ const CustomerDetails = () => { variant="quaternary" align="left" onClick={() => { - subscriptionsListRef?.current?.openAddPlanDialog() + subscriptionsDialogRef?.current?.openDialog() closePopper() }} > @@ -175,7 +196,7 @@ const CustomerDetails = () => { align="left" disabled={!subscriptions || !subscriptions?.length} onClick={() => { - addOnListRef.current?.openAddAddOnDialog() + addOnDialogRef.current?.openDialog() closePopper() }} > @@ -188,7 +209,7 @@ const CustomerDetails = () => { !subscriptions || !subscriptions?.length || (appliedCoupons || []).length > 0 } onClick={() => { - couponListRef.current?.openAddCouponDialog() + addCouponDialogRef.current?.openDialog() closePopper() }} > @@ -288,22 +309,11 @@ const CustomerDetails = () => { )} {!loading && (!tab || tab === TabsOptions.overview) && ( <> - - + + )} @@ -324,6 +334,15 @@ const CustomerDetails = () => { // @ts-ignore customer={data?.customer} /> + + + )}