1
+ import { useMutation } from '@tanstack/react-query' ;
1
2
import { useForm } from 'react-hook-form' ;
2
3
3
4
import { Button } from '@koyeb/design-system' ;
4
5
import { useOrganization } from 'src/api/hooks/session' ;
5
6
import { notify } from 'src/application/notify' ;
6
7
import { ControlledInput } from 'src/components/controlled' ;
7
8
import { SectionHeader } from 'src/components/section-header' ;
8
- import { handleSubmit } from 'src/hooks/form' ;
9
+ import { FormValues , handleSubmit } from 'src/hooks/form' ;
9
10
import { useSearchParams } from 'src/hooks/router' ;
10
- import { createTranslate , Translate } from 'src/intl/translate' ;
11
+ import { createTranslate } from 'src/intl/translate' ;
11
12
12
13
const T = createTranslate ( 'pages.organizationSettings.billing.coupon' ) ;
13
14
@@ -22,23 +23,32 @@ export function Coupon() {
22
23
} ,
23
24
} ) ;
24
25
26
+ const mutation = useMutation ( {
27
+ async mutationFn ( { coupon } : FormValues < typeof form > ) {
28
+ // eslint-disable-next-line no-console
29
+ console . log ( 'Sending coupon code to API:' , coupon ) ;
30
+ await new Promise ( ( r ) => setTimeout ( r , 1000 ) ) ;
31
+ } ,
32
+ onSuccess ( ) {
33
+ form . reset ( ) ;
34
+ notify . success ( t ( 'couponSent' ) ) ;
35
+ } ,
36
+ } ) ;
37
+
25
38
return (
26
39
< section className = "col gap-6" >
27
40
< SectionHeader title = { < T id = "title" /> } description = { < T id = "description" /> } />
28
41
29
- < form
30
- onSubmit = { handleSubmit ( form , ( { coupon } ) => {
31
- // eslint-disable-next-line no-console
32
- console . log ( 'Sending coupon code to API:' , coupon ) ;
33
- form . reset ( ) ;
34
- notify . success ( t ( 'couponSent' ) ) ;
35
- } ) }
36
- className = "row max-w-md items-center gap-4"
37
- >
38
- < ControlledInput control = { form . control } name = "coupon" className = "w-full" />
39
-
40
- < Button type = "submit" >
41
- < Translate id = "common.send" />
42
+ < form onSubmit = { handleSubmit ( form , mutation . mutateAsync ) } className = "row max-w-md items-center gap-4" >
43
+ < ControlledInput
44
+ control = { form . control }
45
+ name = "coupon"
46
+ placeholder = { t ( 'placeholder' ) }
47
+ className = "w-full"
48
+ />
49
+
50
+ < Button type = "submit" loading = { form . formState . isSubmitting } >
51
+ < T id = "submit" />
42
52
</ Button >
43
53
</ form >
44
54
0 commit comments