forked from composable-com/composable-ui
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcoupon-form.tsx
156 lines (150 loc) · 4.13 KB
/
coupon-form.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
import * as yup from 'yup'
import { useIntl } from 'react-intl'
import { useForm } from 'react-hook-form'
import {
Alert,
AlertIcon,
Box,
Flex,
TagLeftIcon,
Wrap,
WrapItem,
} from '@chakra-ui/react'
import { yupResolver } from '@hookform/resolvers/yup'
import { useState } from 'react'
import { IconButton, Text } from '@chakra-ui/react'
import { ArrowForwardIcon } from '@chakra-ui/icons'
import { InputField } from '@composable/ui'
import { Tag, TagLabel, TagCloseButton } from '@chakra-ui/react'
import { useCart } from 'hooks'
import { Price } from 'components/price'
import { CartSummaryItem } from 'components/cart'
import { Icon } from '@chakra-ui/react'
import { MdDiscount } from 'react-icons/md'
import { displayValue } from '@tanstack/react-query-devtools/build/lib/utils'
export const CouponForm = () => {
const intl = useIntl()
const [errorMessage, setErrorMessage] = useState<false | string>(false)
const {
register,
handleSubmit,
setError,
setValue,
formState: { errors },
} = useForm<{ coupon: string }>({
resolver: yupResolver(couponFormSchema()),
mode: 'all',
})
const { cart, addCartCoupon, deleteCartCoupon } = useCart({
onCartCouponAddError: (msg) => {
setErrorMessage(msg || 'Could not add coupon')
},
})
const content = {
input: {
coupon: {
label: intl.formatMessage({ id: 'cart.summary.label.coupon' }),
placeholder: intl.formatMessage({ id: 'cart.summary.label.coupon' }),
},
},
button: {
login: intl.formatMessage({ id: 'action.addCoupon' }),
},
}
const vouchers =
cart.redeemables?.filter((redeemable) => redeemable.object === 'voucher') ||
[]
return (
<>
<CartSummaryItem
label={intl.formatMessage({
id: 'cart.summary.couponCodes',
})}
></CartSummaryItem>
<form
role={'form'}
onSubmit={handleSubmit(async (data) => {
setErrorMessage(false)
// setError('coupon', {message: 'Could not add coupon' })
await addCartCoupon.mutate({
cartId: cart.id || '',
coupon: data.coupon,
})
setValue('coupon', '')
})}
>
<Box
display={'flex'}
flexDirection={'row'}
alignItems={'flex-start'}
justifyContent={'center'}
height={'60px'}
gap={3}
>
<InputField
inputProps={{
size: 'sm',
fontSize: 'sm',
placeholder: content.input.coupon.placeholder,
...register('coupon'),
}}
error={errors.coupon}
label={''}
/>
<IconButton
mt={2}
aria-label="Search database"
icon={<ArrowForwardIcon />}
type="submit"
size="sm"
variant={'outline'}
/>
</Box>
{errorMessage && (
<Alert mt={2} status="warning" borderRadius={'6px'}>
<AlertIcon alignSelf={'flex-start'} />
{errorMessage}
</Alert>
)}
</form>
{vouchers.map((redeemable) => (
<Flex
key={redeemable.id}
justify="space-between"
textStyle={{ base: 'Mobile/S', md: 'Desktop/S' }}
>
<Tag
size="md"
paddingRight={2}
paddingLeft={2}
borderRadius="sm"
variant="outline"
colorScheme="whiteAlpha"
>
<TagLeftIcon boxSize="12px" as={MdDiscount} />
<TagLabel>{redeemable.label}</TagLabel>
<TagCloseButton
onClick={() =>
deleteCartCoupon.mutate({
cartId: cart.id || '',
coupon: redeemable.id,
})
}
/>
</Tag>
<Box>
<Price
rootProps={{ textStyle: 'Body-S', color: 'green' }}
price={`-${redeemable.discount}`}
/>
</Box>
</Flex>
))}
</>
)
}
const couponFormSchema = () => {
return yup.object().shape({
coupon: yup.string().required(),
})
}