1- import { SubmitButton } from 'components/Forms/Button/SubmitButton' ;
21import { Checkbox } from 'components/Forms/Checkbox/Checkbox' ;
3- import { Form } from 'components/Forms/Form/Form' ;
4- import { TextInputControlled } from 'components/Forms/TextInput/TextInputControlled' ;
52import { TIDs } from 'cypress/tids' ;
3+ import dynamic from 'next/dynamic' ;
64import { useState } from 'react' ;
7- import { FormProvider , SubmitHandler } from 'react-hook-form' ;
8- import { PromoCodeFormType } from 'types/form' ;
9- import { useApplyPromoCodeToCart } from 'utils/cart/useApplyPromoCodeToCart' ;
105import { useCurrentCart } from 'utils/cart/useCurrentCart' ;
11- import { blurInput } from 'utils/forms/blurInput' ;
126import useTranslation from 'utils/i18n/useTranslationWrapper' ;
13- import { usePromoCodeForm , usePromoCodeFormMeta } from './promoCodeFormMeta' ;
7+
8+ const PromoCodeForm = dynamic ( ( ) => import ( './PromoCodeForm' ) . then ( ( component ) => component . PromoCodeForm ) , {
9+ ssr : false ,
10+ } ) ;
1411
1512export const PromoCode : FC = ( ) => {
1613 const { promoCodes } = useCurrentCart ( ) ;
17- const [ formProviderMethods , defaultValues ] = usePromoCodeForm ( ) ;
18- const formMeta = usePromoCodeFormMeta ( ) ;
1914 const { t } = useTranslation ( ) ;
20- const { applyPromoCodeToCart } = useApplyPromoCodeToCart ( {
21- success : t ( 'Promo code was added to the order.' ) ,
22- } ) ;
15+ const hasAppliedPromoCode = promoCodes . length > 0 ;
16+ const [ isContentVisible , setIsContentVisible ] = useState ( hasAppliedPromoCode ) ;
17+ const [ wasContentRequested , setWasContentRequested ] = useState ( hasAppliedPromoCode ) ;
2318
24- const [ isContentVisible , setIsContentVisible ] = useState ( ! ! defaultValues . promoCode ) ;
19+ const togglePromoCodeVisibility = ( ) => {
20+ if ( ! isContentVisible ) {
21+ setWasContentRequested ( true ) ;
22+ }
2523
26- const onApplyPromoCodeHandler : SubmitHandler < PromoCodeFormType > = async ( promoCodeFormData ) => {
27- blurInput ( ) ;
28- await applyPromoCodeToCart ( promoCodeFormData . promoCode ) ;
24+ setIsContentVisible ( ! isContentVisible ) ;
2925 } ;
3026
31- if ( promoCodes . length > 0 ) {
27+ if ( hasAppliedPromoCode ) {
3228 return null ;
3329 }
3430
@@ -42,42 +38,11 @@ export const PromoCode: FC = () => {
4238 data-tid = { TIDs . blocks_promocode_add_button }
4339 label = { t ( 'I have a discount coupon' ) }
4440 value = { isContentVisible }
45- onChange = { ( ) => setIsContentVisible ( ! isContentVisible ) }
41+ onChange = { togglePromoCodeVisibility }
4642 />
4743 </ div >
48- { isContentVisible && (
49- < FormProvider { ...formProviderMethods } >
50- < Form
51- className = "flex flex-col gap-2.5 sm:flex-row"
52- formName = { formMeta . formName }
53- onSubmit = { formProviderMethods . handleSubmit ( onApplyPromoCodeHandler ) }
54- >
55- < div className = "max-w-60" >
56- < TextInputControlled
57- isWithoutFormLineError
58- control = { formProviderMethods . control }
59- formName = { formMeta . formName }
60- name = { formMeta . fields . promoCode . name }
61- textInputProps = { {
62- label : formMeta . fields . promoCode . label ,
63- required : true ,
64- } }
65- />
66- </ div >
6744
68- < SubmitButton
69- aria-label = { t ( 'Apply code. Apply promo code' , { ns : 'accessibility' } ) }
70- className = "self-start"
71- hasDisabledCursor = { ! formProviderMethods . formState . isValid }
72- size = "xlarge"
73- tid = { TIDs . blocks_promocode_apply_button }
74- variant = "inverted"
75- >
76- { t ( 'Apply code' ) }
77- </ SubmitButton >
78- </ Form >
79- </ FormProvider >
80- ) }
45+ { wasContentRequested && < PromoCodeForm isContentVisible = { isContentVisible } /> }
8146 </ div >
8247 ) ;
8348} ;
0 commit comments