@@ -8,6 +8,8 @@ import { useState } from 'react'
88import { useForm } from 'react-hook-form'
99import { z } from 'zod'
1010
11+ import { Input } from '../ui/input'
12+
1113import { FAQSection } from './faq'
1214import { PaymentMethods } from './payment-methods'
1315import { 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'
2938import { ROUTES } from '@/src/constants'
3039import { 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
4151export 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