diff --git a/src/components/customers/CustomerCoupons.tsx b/src/components/customers/CustomerCoupons.tsx index 929eb25d4..c39c0a6d9 100644 --- a/src/components/customers/CustomerCoupons.tsx +++ b/src/components/customers/CustomerCoupons.tsx @@ -2,10 +2,11 @@ import { useRef, memo } from 'react' import { gql } from '@apollo/client' import styled from 'styled-components' +import { useParams } from 'react-router-dom' import { AppliedCouponCaptionFragmentDoc, - CustomerCouponFragment, + useGetCustomerCouponsQuery, useRemoveCouponMutation, } from '~/generated/graphql' import { SectionHeader } from '~/styles/customer' @@ -31,6 +32,21 @@ gql` } } + fragment CustomerAppliedCoupons on Customer { + id + appliedCoupons { + ...CustomerCoupon + } + } + + query getCustomerCoupons($id: ID!) { + customer(id: $id) { + id + name + ...CustomerAppliedCoupons + } + } + mutation removeCoupon($input: TerminateAppliedCouponInput!) { terminateAppliedCoupon(input: $input) { id @@ -40,114 +56,109 @@ gql` ${AppliedCouponCaptionFragmentDoc} ` -interface CustomerCouponsProps { - coupons?: CustomerCouponFragment[] | null | undefined - customerId: string - customerName: string -} - -export const CustomerCoupons = memo( - ({ coupons, customerId, customerName }: CustomerCouponsProps) => { - const removeDialogRef = useRef(null) - const addCouponDialogRef = useRef(null) - const deleteCouponId = useRef(null) - const { translate } = useInternationalization() - const [removeCoupon] = useRemoveCouponMutation({ - onCompleted({ terminateAppliedCoupon }) { - if (!!terminateAppliedCoupon) { - addToast({ - severity: 'success', - message: translate('text_628b8c693e464200e00e49d1'), - }) - } - }, - update(cache, { data }) { - if (!data?.terminateAppliedCoupon) return - - const cacheId = cache.identify({ - id: data?.terminateAppliedCoupon.id, - __typename: 'AppliedCoupon', +export const CustomerCoupons = memo(() => { + const { id: customerId } = useParams() + const removeDialogRef = useRef(null) + const addCouponDialogRef = useRef(null) + const deleteCouponId = useRef(null) + const { translate } = useInternationalization() + const { data } = useGetCustomerCouponsQuery({ + variables: { id: customerId as string }, + skip: !customerId, + }) + const coupons = data?.customer?.appliedCoupons + const [removeCoupon] = useRemoveCouponMutation({ + onCompleted({ terminateAppliedCoupon }) { + if (!!terminateAppliedCoupon) { + addToast({ + severity: 'success', + message: translate('text_628b8c693e464200e00e49d1'), }) - - cache.evict({ id: cacheId }) - }, - }) - - return ( - <> - {!!(coupons || [])?.length && ( - - - {translate('text_628b8c693e464200e00e469d')} - - - - - {translate('text_628b8c693e464200e00e46ab')} - - - {(coupons || []).map((appliedCoupon) => ( - - - - - - - {appliedCoupon.coupon?.name} - - - - - + + + + {translate('text_628b8c693e464200e00e46ab')} + + + {(coupons || []).map((appliedCoupon) => ( + + + + + + + {appliedCoupon.coupon?.name} + + + + +