1
- import { useCallback , useEffect } from "react" ;
1
+ import { useCallback , useEffect , useMemo } from "react" ;
2
2
3
3
import { zodResolver } from "@hookform/resolvers/zod" ;
4
4
import { useRouter } from "next/router" ;
5
5
import { SubmitHandler , useForm , useWatch } from "react-hook-form" ;
6
- import { Temporal } from "temporal-polyfill" ;
7
6
import { infer as FromSchema } from "zod" ;
8
7
9
8
import { campaignsContractClient } from "@/common/contracts/core/campaigns" ;
@@ -13,29 +12,41 @@ import { toast } from "@/common/ui/layout/hooks";
13
12
import { useWalletUserSession } from "@/common/wallet" ;
14
13
import { dispatch } from "@/store" ;
15
14
16
- import { campaignFormSchema } from "../models/schema" ;
15
+ import { createCampaignSchema , updateCampaignSchema } from "../models/schema" ;
17
16
import { CampaignEnumType } from "../types" ;
18
17
19
18
export const useCampaignForm = ( { campaignId } : { campaignId ?: CampaignId } ) => {
20
19
const viewer = useWalletUserSession ( ) ;
21
20
const router = useRouter ( ) ;
22
21
23
- type Values = FromSchema < typeof campaignFormSchema > ;
22
+ const schema = campaignId ? updateCampaignSchema : createCampaignSchema ;
23
+
24
+ type Values = FromSchema < typeof schema > ;
24
25
25
26
const self = useForm < Values > ( {
26
- resolver : zodResolver ( campaignFormSchema ) ,
27
+ resolver : zodResolver ( schema ) ,
27
28
mode : "onChange" ,
28
29
resetOptions : { keepDirtyValues : false } ,
29
30
} ) ;
30
31
31
32
const values = useWatch ( self ) ;
32
33
34
+ const isDisabled = useMemo (
35
+ ( ) => ! self . formState . isDirty || ! self . formState . isValid || self . formState . isSubmitting ,
36
+ [ self . formState . isDirty , self . formState . isSubmitting , self . formState . isValid ] ,
37
+ ) ;
38
+
33
39
useEffect ( ( ) => {
34
40
const { min_amount, max_amount, target_amount } = values ;
35
41
const errors : Record < string , { message : string } > = { } ;
36
42
43
+ // Convert string values to numbers for comparison
44
+ const minAmount = min_amount ? Number ( min_amount ) : null ;
45
+ const maxAmount = max_amount ? Number ( max_amount ) : null ;
46
+ const targetAmount = target_amount ? Number ( target_amount ) : null ;
47
+
37
48
// Validate min_amount vs max_amount
38
- if ( min_amount && max_amount && min_amount > max_amount ) {
49
+ if ( minAmount && maxAmount && minAmount > maxAmount ) {
39
50
errors . min_amount = {
40
51
message : "Minimum amount cannot be greater than maximum amount" ,
41
52
} ;
@@ -46,7 +57,7 @@ export const useCampaignForm = ({ campaignId }: { campaignId?: CampaignId }) =>
46
57
}
47
58
48
59
// Validate target_amount vs max_amount
49
- if ( target_amount && max_amount && target_amount > max_amount ) {
60
+ if ( targetAmount && maxAmount && targetAmount > maxAmount ) {
50
61
errors . target_amount = {
51
62
message : "Target amount cannot be greater than maximum amount" ,
52
63
} ;
@@ -57,7 +68,7 @@ export const useCampaignForm = ({ campaignId }: { campaignId?: CampaignId }) =>
57
68
}
58
69
59
70
// Validate min_amount vs target_amount
60
- if ( min_amount && target_amount && min_amount > target_amount ) {
71
+ if ( minAmount && targetAmount && minAmount > targetAmount ) {
61
72
errors . min_amount = {
62
73
message : "Minimum amount cannot be greater than target amount" ,
63
74
} ;
@@ -169,9 +180,11 @@ export const useCampaignForm = ({ campaignId }: { campaignId?: CampaignId }) =>
169
180
. update_campaign ( {
170
181
args : { ...args , campaign_id : campaignId } ,
171
182
} )
172
- . then ( ( ) => {
183
+ . then ( ( updateValues ) => {
184
+ console . log ( updateValues ) ;
185
+
173
186
toast ( {
174
- title : `You’ve successfully updated this Campaign` ,
187
+ title : `You’ve successfully updated this ${ updateValues . name } Campaign` ,
175
188
} ) ;
176
189
} )
177
190
. catch ( ( error ) => {
@@ -220,8 +233,8 @@ export const useCampaignForm = ({ campaignId }: { campaignId?: CampaignId }) =>
220
233
) ;
221
234
222
235
const onChange = async ( field : keyof Values , value : string ) => {
223
- self . setValue ( field , value ) ; // Update field value
224
- await self . trigger ( ) ; // Trigger validation
236
+ self . setValue ( field , value ) ;
237
+ await self . trigger ( ) ;
225
238
} ;
226
239
227
240
return {
@@ -236,7 +249,7 @@ export const useCampaignForm = ({ campaignId }: { campaignId?: CampaignId }) =>
236
249
values,
237
250
watch : self . watch ,
238
251
onChange,
239
- isValid : Object . keys ( self . formState . errors ) . length === 0 ,
252
+ isDisabled ,
240
253
handleDeleteCampaign,
241
254
handleProcessEscrowedDonations,
242
255
handleDonationsRefund,
0 commit comments