1+ import { usePost } from '@/core/react-query' ;
2+ import { useDownload } from '@/hooks' ;
13import { cn } from '@/lib/utils' ;
2- import { useState } from 'react' ;
4+ import { FormEvent , useState } from 'react' ;
5+ import { EC2Body , EC2Response } from './ec2.types' ;
6+ import { TerraformTemplateAPI } from '@/enums/api.enums' ;
7+ import { toast } from 'sonner' ;
38
49const EC2 = ( ) => {
10+ const { mutateAsync : ec2Mutate , isPending : ec2Pending } = usePost <
11+ EC2Response ,
12+ EC2Body
13+ > ( TerraformTemplateAPI . EC2 , 'ec2' ) ;
14+ const { download, isPending : downloadPending } = useDownload ( {
15+ folderName : 'MyTerraform' ,
16+ source : 'iam' ,
17+ downloadFileName : 'Iam' ,
18+ } ) ;
19+
520 const [ services , setServices ] = useState ( {
621 key_pair : false ,
722 security_group : false ,
@@ -16,11 +31,27 @@ const EC2 = () => {
1631 } ) ) ;
1732 } ;
1833
34+ const handleForm = async ( e : FormEvent ) => {
35+ e . preventDefault ( ) ;
36+
37+ try {
38+ const ec2Body : EC2Body = {
39+ ...services ,
40+ } ;
41+
42+ await ec2Mutate ( ec2Body ) ;
43+ await download ( ) ;
44+ } catch ( error ) {
45+ console . log ( error ) ;
46+ toast . error ( 'Something went wrong' ) ;
47+ }
48+ } ;
49+
1950 return (
20- < div className = "w-full max-w-96" >
21- < div className = "border border-gray-500 rounded-md " >
51+ < form onSubmit = { handleForm } className = "w-full max-w-96" >
52+ < div className = "rounded-md border border-gray-500" >
2253 < div className = "divide-y divide-gray-500" >
23- < div className = "flex items-center justify-between w-full px-3 py-3" >
54+ < div className = "flex w-full items-center justify-between px-3 py-3" >
2455 < p > Key Pair</ p >
2556 < input
2657 type = "checkbox"
@@ -30,7 +61,7 @@ const EC2 = () => {
3061 onChange = { ( ) => handleServices ( 'key_pair' ) }
3162 />
3263 </ div >
33- < div className = "flex items-center justify-between w-full px-3 py-3" >
64+ < div className = "flex w-full items-center justify-between px-3 py-3" >
3465 < p > Security Group</ p >
3566 < input
3667 type = "checkbox"
@@ -41,7 +72,7 @@ const EC2 = () => {
4172 onChange = { ( ) => handleServices ( 'security_group' ) }
4273 />
4374 </ div >
44- < div className = "flex items-center justify-between w-full px-3 py-3" >
75+ < div className = "flex w-full items-center justify-between px-3 py-3" >
4576 < p > AWS Instance</ p >
4677 < input
4778 type = "checkbox"
@@ -51,7 +82,7 @@ const EC2 = () => {
5182 onChange = { ( ) => handleServices ( 'aws_instance' ) }
5283 />
5384 </ div >
54- < div className = "flex items-center justify-between w-full px-3 py-3" >
85+ < div className = "flex w-full items-center justify-between px-3 py-3" >
5586 < p > AMI From Instance</ p >
5687 < input
5788 type = "checkbox"
@@ -64,10 +95,18 @@ const EC2 = () => {
6495 </ div >
6596 </ div >
6697 </ div >
67- < button className = "w-full mt-3 text-white btn bg-orange-base hover:bg-orange-base/70" >
68- Submit
98+ < button
99+ type = "submit"
100+ disabled = { ec2Pending || downloadPending }
101+ className = "btn mt-3 w-full bg-orange-base text-white hover:bg-orange-base/70 disabled:bg-orange-base/50 disabled:text-white/70"
102+ >
103+ { ec2Pending
104+ ? 'Generate Terraform...'
105+ : downloadPending
106+ ? 'Downloading Template...'
107+ : 'Generate Terraform' }
69108 </ button >
70- </ div >
109+ </ form >
71110 ) ;
72111} ;
73112
0 commit comments