1
- " use client" ;
1
+ ' use client' ;
2
2
3
- import { useEffect } from " react" ;
4
- import { useQueryClient } from " @tanstack/react-query" ;
3
+ import { useEffect } from ' react' ;
4
+ import { useQueryClient } from ' @tanstack/react-query' ;
5
5
import {
6
6
Container ,
7
7
Input ,
@@ -12,111 +12,109 @@ import {
12
12
Textarea ,
13
13
Text ,
14
14
toast ,
15
- } from "@medusajs/ui" ;
16
- import { Spinner } from "@medusajs/icons" ;
17
-
18
- import { useCredentials } from "../hooks/useCredentials" ;
19
- import { useCreateCredentials } from "../hooks/useCreateCredentials" ;
20
- import { validateForm , extractFormData } from "../utils" ;
21
- import { useHyperswitchForm } from "../hooks/useHyperswitchForm" ;
22
-
15
+ } from '@medusajs/ui' ;
16
+ import { Spinner } from '@medusajs/icons' ;
23
17
18
+ import { useCredentials } from '../hooks/useCredentials' ;
19
+ import { useCreateCredentials } from '../hooks/useCreateCredentials' ;
20
+ import { validateForm , extractFormData } from '../utils' ;
21
+ import { useHyperswitchForm } from '../hooks/useHyperswitchForm' ;
24
22
25
23
const FormField = ( { label, error, children } ) => (
26
- < Container className = " col-span-5" >
24
+ < Container className = ' col-span-5' >
27
25
< Label id = { label } > { label } </ Label >
28
26
{ children }
29
- { error && < Text className = " text-red-500 text-sm mt-1" > { error } </ Text > }
27
+ { error && < Text className = ' text-red-500 text-sm mt-1' > { error } </ Text > }
30
28
</ Container >
31
29
) ;
32
30
33
31
const EnvironmentSelect = ( { isEditing, value, onChange, error } ) => (
34
- < Container className = " col-span-2" >
35
- < Label id = " environment" > Environment</ Label >
32
+ < Container className = ' col-span-2' >
33
+ < Label id = ' environment' > Environment</ Label >
36
34
< Select
37
35
disabled = { ! isEditing }
38
- name = " environment"
36
+ name = ' environment'
39
37
value = { value }
40
38
onValueChange = { onChange }
41
39
>
42
40
< Select . Trigger >
43
- < Select . Value placeholder = " Select Environment" />
41
+ < Select . Value placeholder = ' Select Environment' />
44
42
</ Select . Trigger >
45
43
< Select . Content >
46
- < Select . Item value = " sandbox" > Sandbox</ Select . Item >
47
- < Select . Item value = " production" > Production</ Select . Item >
44
+ < Select . Item value = ' sandbox' > Sandbox</ Select . Item >
45
+ < Select . Item value = ' production' > Production</ Select . Item >
48
46
</ Select . Content >
49
47
</ Select >
50
- { error && < Text className = " text-red-500 text-sm mt-1" > { error } </ Text > }
48
+ { error && < Text className = ' text-red-500 text-sm mt-1' > { error } </ Text > }
51
49
</ Container >
52
50
) ;
53
51
54
52
const CaptureMethodSelect = ( { isEditing, value, onChange, error } ) => (
55
- < Container className = " col-span-2" >
56
- < Label id = " capture-method" > Capture Method</ Label >
53
+ < Container className = ' col-span-2' >
54
+ < Label id = ' capture-method' > Capture Method</ Label >
57
55
< Select
58
56
disabled = { ! isEditing }
59
- name = " capture-method"
57
+ name = ' capture-method'
60
58
value = { value }
61
59
onValueChange = { onChange }
62
60
>
63
61
< Select . Trigger >
64
- < Select . Value placeholder = " Select Capture Method" />
62
+ < Select . Value placeholder = ' Select Capture Method' />
65
63
</ Select . Trigger >
66
64
< Select . Content >
67
- < Select . Item value = " manual" > Manual</ Select . Item >
68
- < Select . Item value = " automatic" > Automatic</ Select . Item >
65
+ < Select . Item value = ' manual' > Manual</ Select . Item >
66
+ < Select . Item value = ' automatic' > Automatic</ Select . Item >
69
67
</ Select . Content >
70
68
</ Select >
71
- { error && < Text className = " text-red-500 text-sm mt-1" > { error } </ Text > }
69
+ { error && < Text className = ' text-red-500 text-sm mt-1' > { error } </ Text > }
72
70
</ Container >
73
71
) ;
74
72
75
73
const FormContent = ( { formState, handleChange, isEditing, errors } ) => (
76
74
< >
77
- < FormField label = " API Key" error = { errors . publishable_key } >
75
+ < FormField label = ' API Key' error = { errors . publishable_key } >
78
76
< Input
79
- placeholder = " Enter your API Key"
80
- id = " publishable-key"
81
- name = " publishable-key"
77
+ placeholder = ' Enter your API Key'
78
+ id = ' publishable-key'
79
+ name = ' publishable-key'
82
80
onChange = { handleChange . publishableKey }
83
81
disabled = { ! isEditing }
84
82
value = { formState . publishable_key }
85
83
/>
86
84
</ FormField >
87
85
88
- < FormField label = " API Secret" error = { errors . secret_key } >
86
+ < FormField label = ' API Secret' error = { errors . secret_key } >
89
87
< Input
90
- placeholder = " Enter your API Secret"
91
- type = " password"
92
- id = " secret-key"
93
- name = " api-secret-key"
88
+ placeholder = ' Enter your API Secret'
89
+ type = ' password'
90
+ id = ' secret-key'
91
+ name = ' api-secret-key'
94
92
onChange = { handleChange . secretKey }
95
93
disabled = { ! isEditing }
96
94
value = { formState . secret_key }
97
95
/>
98
96
</ FormField >
99
97
100
98
< FormField
101
- label = " Payment Response Hash Key"
99
+ label = ' Payment Response Hash Key'
102
100
error = { errors . payment_hash_key }
103
101
>
104
102
< Input
105
- placeholder = " Enter your Payment Response Hash Key"
106
- type = " password"
107
- id = " payment-hash-key"
108
- name = " payment-response-hash-key"
103
+ placeholder = ' Enter your Payment Response Hash Key'
104
+ type = ' password'
105
+ id = ' payment-hash-key'
106
+ name = ' payment-response-hash-key'
109
107
onChange = { handleChange . paymentHashKey }
110
108
disabled = { ! isEditing }
111
109
value = { formState . payment_hash_key }
112
110
/>
113
111
</ FormField >
114
112
115
- < FormField label = " Webhook URL" error = { errors . webhook_url } >
113
+ < FormField label = ' Webhook URL' error = { errors . webhook_url } >
116
114
< Input
117
- placeholder = " Enter your Webhook URL"
118
- id = " webhook-url"
119
- name = " webhook-url"
115
+ placeholder = ' Enter your Webhook URL'
116
+ id = ' webhook-url'
117
+ name = ' webhook-url'
120
118
onChange = { handleChange . webhookURL }
121
119
disabled = { ! isEditing }
122
120
value = { formState . webhook_url }
@@ -137,21 +135,21 @@ const FormContent = ({ formState, handleChange, isEditing, errors }) => (
137
135
error = { errors . capture_method }
138
136
/>
139
137
140
- < Container className = " col-span-1 flex items-center gap-x-2" >
141
- < Label id = " enable" > Save Cards</ Label >
138
+ < Container className = ' col-span-1 flex items-center gap-x-2' >
139
+ < Label id = ' enable' > Save Cards</ Label >
142
140
< Switch
143
141
disabled = { ! isEditing }
144
- name = " enable-save-cards"
142
+ name = ' enable-save-cards'
145
143
checked = { formState . enable_save_cards }
146
144
onCheckedChange = { handleChange . enableSaveCards }
147
145
/>
148
146
</ Container >
149
147
150
- < FormField label = " Appearance" error = { errors . appearence } >
148
+ < FormField label = ' Appearance' error = { errors . appearence } >
151
149
< Textarea
152
- placeholder = " Customise Hyperswitch appearance"
153
- id = " appearence"
154
- name = " appearence"
150
+ placeholder = ' Customise Hyperswitch appearance'
151
+ id = ' appearence'
152
+ name = ' appearence'
155
153
onChange = { handleChange . appearence }
156
154
disabled = { ! isEditing }
157
155
value = { formState . appearence }
@@ -160,8 +158,7 @@ const FormContent = ({ formState, handleChange, isEditing, errors }) => (
160
158
</ >
161
159
) ;
162
160
163
-
164
- const HyperswitchForm = ( ) => {
161
+ const HyperswitchForm = ( ) => {
165
162
const queryClient = useQueryClient ( ) ;
166
163
const {
167
164
formState,
@@ -179,20 +176,18 @@ const FormContent = ({ formState, handleChange, isEditing, errors }) => (
179
176
180
177
useEffect ( ( ) => {
181
178
if ( isSuccess && data ?. credentials ) {
182
-
183
179
Object . entries ( data . credentials ) . forEach ( ( [ key , value ] ) => {
184
- const setter =
185
- formSetters [ ( `set${ key } ` ) ] ;
180
+ const setter = formSetters [ `set${ key } ` ] ;
186
181
if ( setter ) {
187
- setter ( value || "" ) ;
182
+ setter ( value || '' ) ;
188
183
}
189
184
} ) ;
190
185
}
191
186
} , [ isSuccess , data ] ) ;
192
187
193
188
const handleEdit = ( ) => setIsEditing ( ! isEditing ) ;
194
189
195
- const handleSubmit = async ( event ) => {
190
+ const handleSubmit = async event => {
196
191
event . preventDefault ( ) ;
197
192
198
193
if ( ! isEditing ) {
@@ -206,49 +201,50 @@ const FormContent = ({ formState, handleChange, isEditing, errors }) => (
206
201
207
202
try {
208
203
const formData = extractFormData ( event . target ) ;
209
- createCredentials ( formData , {
204
+ createCredentials ( formData , {
210
205
onSuccess : ( ) => {
211
- queryClient . invalidateQueries ( { queryKey :[ "credentials" ] } ) ;
212
- setErrors ( { } ) ;
213
- handleEdit ( ) ;
214
- toast . success ( "Success" , {
215
- description : "Settings saved successfully" ,
216
- } ) ;
217
- } } ) ;
206
+ queryClient . invalidateQueries ( { queryKey : [ 'credentials' ] } ) ;
207
+ setErrors ( { } ) ;
208
+ handleEdit ( ) ;
209
+ toast . success ( 'Success' , {
210
+ description : 'Settings saved successfully' ,
211
+ } ) ;
212
+ } ,
213
+ } ) ;
218
214
219
215
setErrors ( { } ) ;
220
216
} catch ( error ) {
221
- toast . error ( " Error" , {
222
- description : " Failed to save settings" ,
217
+ toast . error ( ' Error' , {
218
+ description : ' Failed to save settings' ,
223
219
} ) ;
224
220
}
225
221
} ;
226
222
227
223
if ( isLoadingData ) {
228
224
return (
229
- < Container className = " flex justify-center items-center h-64" >
230
- < Spinner className = " animate-spin" />
225
+ < Container className = ' flex justify-center items-center h-64' >
226
+ < Spinner className = ' animate-spin' />
231
227
</ Container >
232
228
) ;
233
229
}
234
230
235
231
return (
236
- < form className = " grid grid-cols-5 gap-3 mt-6" onSubmit = { handleSubmit } >
232
+ < form className = ' grid grid-cols-5 gap-3 mt-6' onSubmit = { handleSubmit } >
237
233
< FormContent
238
234
formState = { formState }
239
235
handleChange = { handleChange }
240
236
isEditing = { isEditing }
241
237
errors = { errors }
242
238
/>
243
239
244
- < div className = " col-span-2" >
245
- < Button variant = " primary" disabled = { isSubmitting } >
246
- { isEditing ? ( isSubmitting ? " Saving..." : " Save Changes" ) : " Edit" }
240
+ < div className = ' col-span-2' >
241
+ < Button variant = ' primary' disabled = { isSubmitting } >
242
+ { isEditing ? ( isSubmitting ? ' Saving...' : ' Save Changes' ) : ' Edit' }
247
243
</ Button >
248
244
{ isEditing && (
249
245
< Button
250
- variant = " secondary"
251
- type = " reset"
246
+ variant = ' secondary'
247
+ type = ' reset'
252
248
onClick = { handleEdit }
253
249
disabled = { isSubmitting }
254
250
>
@@ -260,4 +256,4 @@ const FormContent = ({ formState, handleChange, isEditing, errors }) => (
260
256
) ;
261
257
} ;
262
258
263
- export default HyperswitchForm ;
259
+ export default HyperswitchForm ;
0 commit comments