11import { cn } from '@/lib/utils' ;
2- import { FC , useState } from 'react' ;
2+ import { FC , FormEvent , useState } from 'react' ;
33import { ChevronDown } from 'lucide-react' ;
4+ import { TerraformTemplateAPI } from '@/enums/api.enums' ;
5+ import { usePost } from '@/core/react-query' ;
6+ import { ArgocdBody , ArgocdResponse } from './argocd.types' ;
7+ import { toast } from 'sonner' ;
8+ import { useDownload } from '@/hooks' ;
49
510const Argocd : FC = ( ) => {
11+ const { mutateAsync : argocdMutate , isPending : argocdPending } = usePost <
12+ ArgocdResponse ,
13+ ArgocdBody
14+ > ( TerraformTemplateAPI . Argocd , 'argocd' ) ;
15+ const { download, isPending : downloadPending } = useDownload ( {
16+ folderName : 'MyTerraform' ,
17+ source : 'argocd' ,
18+ downloadFileName : 'Argocd' ,
19+ } ) ;
20+
621 const [ dropdown , setDropdown ] = useState ( {
722 argo_application : false ,
823 sync_policy : false ,
@@ -28,8 +43,33 @@ const Argocd: FC = () => {
2843 } ) ) ;
2944 } ;
3045
46+ const handleForm = async ( e : FormEvent ) => {
47+ e . preventDefault ( ) ;
48+
49+ try {
50+ const argocdBody : ArgocdBody = {
51+ argocd_application : dropdown . argo_application
52+ ? {
53+ sync_policy : {
54+ auto_prune : services . auto_prune ,
55+ self_heal : services . self_heal ,
56+ } ,
57+ }
58+ : null ,
59+ argocd_repository : services . argocd_repository ,
60+ application_depends_repository : services . application_depends_repository ,
61+ } ;
62+
63+ await argocdMutate ( argocdBody ) ;
64+ await download ( ) ;
65+ } catch ( error ) {
66+ console . log ( error ) ;
67+ toast . error ( 'Something went wrong' ) ;
68+ }
69+ } ;
70+
3171 return (
32- < div className = "w-full max-w-96" >
72+ < form onSubmit = { handleForm } className = "w-full max-w-96" >
3373 < div className = "rounded-md border border-gray-500" >
3474 < div className = "divide-y divide-gray-500" >
3575 < div className = "flex w-full items-center justify-between px-3 py-3" >
@@ -118,10 +158,18 @@ const Argocd: FC = () => {
118158 </ div >
119159 </ div >
120160 </ div >
121- < button className = "btn mt-3 w-full bg-orange-base text-white hover:bg-orange-base/70" >
122- Submit
161+ < button
162+ type = "submit"
163+ disabled = { argocdPending || downloadPending }
164+ 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"
165+ >
166+ { argocdPending
167+ ? 'GPT Answer...'
168+ : downloadPending
169+ ? 'Generate Terraform...'
170+ : 'Generate Terraform' }
123171 </ button >
124- </ div >
172+ </ form >
125173 ) ;
126174} ;
127175
0 commit comments