Skip to content

Commit 487ed65

Browse files
authored
Merge pull request #52 from teacoder-team/dev
feat: add email field to payment
2 parents 8961868 + bccde2b commit 487ed65

2 files changed

Lines changed: 60 additions & 3 deletions

File tree

src/api/generated/initPaymentRequest.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,4 +10,6 @@ import type { InitPaymentRequestMethod } from './initPaymentRequestMethod';
1010
export interface InitPaymentRequest {
1111
/** Payment method */
1212
method: InitPaymentRequestMethod;
13+
/** User email (required if the account does not have one) */
14+
email?: string;
1315
}

src/components/premium/premium.tsx

Lines changed: 58 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ import { useState } from 'react'
88
import { useForm } from 'react-hook-form'
99
import { z } from 'zod'
1010

11+
import { Input } from '../ui/input'
12+
1113
import { FAQSection } from './faq'
1214
import { PaymentMethods } from './payment-methods'
1315
import { InitPaymentRequestMethod } from '@/src/api/generated'
@@ -25,7 +27,14 @@ import {
2527
DialogHeader,
2628
DialogTitle
2729
} from '@/src/components/ui/dialog'
28-
import { Form } from '@/src/components/ui/form'
30+
import {
31+
Form,
32+
FormControl,
33+
FormField,
34+
FormItem,
35+
FormLabel,
36+
FormMessage
37+
} from '@/src/components/ui/form'
2938
import { ROUTES } from '@/src/constants'
3039
import { useAuth } from '@/src/hooks'
3140

@@ -35,7 +44,8 @@ export const paymentSchema = z.object({
3544
{
3645
required_error: 'Выберите метод оплаты'
3746
}
38-
)
47+
),
48+
email: z.string().email('Введите корректный email').optional()
3949
})
4050

4151
export type PaymentFormValues = z.infer<typeof paymentSchema>
@@ -50,6 +60,28 @@ export function Premium() {
5060
onSuccess(data) {
5161
setIsOpen(false)
5262
router.push(data.url)
63+
},
64+
onError(error: any) {
65+
if (error.response?.status === 409) {
66+
form.setError('email', {
67+
type: 'manual',
68+
message: 'Этот email уже используется'
69+
})
70+
return
71+
}
72+
73+
if (error.response?.status === 400) {
74+
form.setError('email', {
75+
type: 'manual',
76+
message: 'Для проведения платежа необходимо указать почту'
77+
})
78+
return
79+
}
80+
81+
form.setError('method', {
82+
type: 'manual',
83+
message: 'Не удалось создать платеж. Попробуйте позже.'
84+
})
5385
}
5486
})
5587

@@ -65,7 +97,10 @@ export function Premium() {
6597
})
6698

6799
const onSubmit = (data: PaymentFormValues) => {
68-
mutate({ method: data.method as InitPaymentRequestMethod })
100+
mutate({
101+
method: data.method as InitPaymentRequestMethod,
102+
email: data.email
103+
})
69104
}
70105

71106
return (
@@ -180,6 +215,26 @@ export function Premium() {
180215
</p>
181216
)}
182217

218+
{!user?.email && (
219+
<FormField
220+
control={form.control}
221+
name='email'
222+
render={({ field }) => (
223+
<FormItem className='mt-4'>
224+
<FormLabel>Email</FormLabel>
225+
<FormControl>
226+
<Input
227+
placeholder='tony@starkindustries.com'
228+
disabled={isPending}
229+
{...field}
230+
/>
231+
</FormControl>
232+
<FormMessage />
233+
</FormItem>
234+
)}
235+
/>
236+
)}
237+
183238
<div className='flex gap-x-3 pt-6'>
184239
<Button
185240
type='button'

0 commit comments

Comments
 (0)