@@ -6,7 +6,7 @@ import { z } from 'zod'
66import { zodResolver } from '@hookform/resolvers/zod'
77import { useTranslations } from 'next-intl'
88
9- import { Currencies , CyclePeriod , ISubscription , RememberPeriod } from '@poveroh/types'
9+ import { AppearanceMode , Currencies , CyclePeriod , ISubscription , RememberPeriod } from '@poveroh/types'
1010
1111import { Form , FormControl , FormField , FormItem , FormLabel , FormMessage } from '@poveroh/ui/components/form'
1212import { Input } from '@poveroh/ui/components/input'
@@ -42,28 +42,41 @@ export const SubscriptionForm = forwardRef(
4242 description : '' ,
4343 amount : 0 ,
4444 currency : Currencies . EUR ,
45- logo : '' ,
46- icon : iconList [ 0 ] as string ,
45+ appearance_mode : AppearanceMode . ICON ,
46+ appearance_logo_icon : iconList [ 0 ] as string ,
4747 first_payment : new Date ( ) . toISOString ( ) . split ( 'T' ) [ 0 ] ,
4848 cycle_number : '1' ,
4949 cycle_period : CyclePeriod . MONTH ,
5050 remember_period : RememberPeriod . THREE_DAYS ,
5151 bank_account_id : ''
5252 }
5353
54- const formSchema = z . object ( {
55- title : z . string ( ) . nonempty ( t ( 'messages.errors.required' ) ) ,
56- description : z . string ( ) . optional ( ) ,
57- amount : z . number ( ) . min ( 0 ) ,
58- currency : z . nativeEnum ( Currencies ) ,
59- logo : z . string ( ) . url ( ) . optional ( ) . or ( z . literal ( '' ) ) ,
60- icon : z . string ( ) ,
61- first_payment : z . string ( ) ,
62- cycle_number : z . string ( ) ,
63- cycle_period : z . nativeEnum ( CyclePeriod ) ,
64- remember_period : z . nativeEnum ( RememberPeriod ) . optional ( ) ,
65- bank_account_id : z . string ( ) . nonempty ( t ( 'messages.errors.required' ) )
66- } )
54+ const formSchema = z
55+ . object ( {
56+ title : z . string ( ) . nonempty ( t ( 'messages.errors.required' ) ) ,
57+ description : z . string ( ) . optional ( ) ,
58+ amount : z . number ( ) . min ( 0 ) ,
59+ currency : z . nativeEnum ( Currencies ) ,
60+ appearance_mode : z . nativeEnum ( AppearanceMode ) ,
61+ appearance_logo_icon : z . string ( ) . nonempty ( t ( 'messages.errors.required' ) ) ,
62+ first_payment : z . string ( ) ,
63+ cycle_number : z . string ( ) ,
64+ cycle_period : z . nativeEnum ( CyclePeriod ) ,
65+ remember_period : z . nativeEnum ( RememberPeriod ) . optional ( ) ,
66+ bank_account_id : z . string ( ) . nonempty ( t ( 'messages.errors.required' ) )
67+ } )
68+ . refine (
69+ data => {
70+ if ( data . appearance_mode === AppearanceMode . LOGO ) {
71+ return z . string ( ) . safeParse ( data . appearance_logo_icon ) . success
72+ }
73+ return true
74+ } ,
75+ {
76+ message : t ( 'messages.errors.url' ) ,
77+ path : [ 'appearance_logo_icon' ]
78+ }
79+ )
6780
6881 const form = useForm < z . infer < typeof formSchema > > ( {
6982 resolver : zodResolver ( formSchema ) ,
@@ -77,8 +90,8 @@ export const SubscriptionForm = forwardRef(
7790 } ) )
7891
7992 useEffect ( ( ) => {
80- console . debug ( 'Form errors:' , form . formState . errors )
81- } , [ form . formState . errors ] )
93+ console . log ( ! fromTemplate )
94+ } )
8295
8396 const handleLocalSubmit = async ( values : z . infer < typeof formSchema > ) => {
8497 try {
@@ -258,7 +271,7 @@ export const SubscriptionForm = forwardRef(
258271 ) }
259272 />
260273
261- { ! fromTemplate && (
274+ { ( ! fromTemplate || initialData ?. appearance_mode == AppearanceMode . ICON ) && (
262275 < div className = 'flex flex-col space-y-4' >
263276 < FormItem >
264277 < FormLabel mandatory = { ! inEditingMode } > { t ( 'form.icon.label' ) } </ FormLabel >
@@ -275,7 +288,7 @@ export const SubscriptionForm = forwardRef(
275288 className = { `box-border p-1 cursor-pointer flex justify-center items-center rounded-md h-[30px] w-[30px]
276289 ${ icon === x ? 'bg-white text-black border border-hr' : '' } ` }
277290 onClick = { ( ) => {
278- form . setValue ( 'icon ' , x )
291+ form . setValue ( 'appearance_logo_icon ' , x )
279292 setIcon ( x )
280293 } }
281294 >
0 commit comments