Skip to content

Commit 1ccb235

Browse files
committed
feat(web): Implement download iam template functionality
1 parent b85fc23 commit 1ccb235

2 files changed

Lines changed: 55 additions & 8 deletions

File tree

web/src/pages/terraform-template/IAM/iam.tsx

Lines changed: 47 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,22 @@
1+
import { usePost } from '@/core/react-query';
12
import { cn } from '@/lib/utils';
2-
import { FC, useState } from 'react';
3+
import { FC, FormEvent, useState } from 'react';
4+
import { IAMBody, IAMResponse } from './iam.types';
5+
import { TerraformTemplateAPI } from '@/enums/api.enums';
6+
import { useDownload } from '@/hooks';
7+
import { toast } from 'sonner';
38

49
const IAM: FC = () => {
10+
const { mutateAsync: iamMutate, isPending: iamPending } = usePost<
11+
IAMResponse,
12+
IAMBody
13+
>(TerraformTemplateAPI.Iam, 'iam');
14+
const { download, isPending: downloadPending } = useDownload({
15+
folderName: 'MyTerraform',
16+
source: 'iam',
17+
downloadFileName: 'Iam',
18+
});
19+
520
const [services, setServices] = useState({
621
iam_user: false,
722
iam_group: false,
@@ -14,11 +29,27 @@ const IAM: FC = () => {
1429
}));
1530
};
1631

32+
const handleForm = async (e: FormEvent) => {
33+
e.preventDefault();
34+
35+
try {
36+
const iamBody: IAMBody = {
37+
...services,
38+
};
39+
40+
await iamMutate(iamBody);
41+
await download();
42+
} catch (error) {
43+
console.log(error);
44+
toast.error('Something went wrong');
45+
}
46+
};
47+
1748
return (
18-
<div className="w-full max-w-96">
19-
<div className="border border-gray-500 rounded-md">
49+
<form onSubmit={handleForm} className="w-full max-w-96">
50+
<div className="rounded-md border border-gray-500">
2051
<div className="divide-y divide-gray-500">
21-
<div className="flex items-center justify-between w-full px-3 py-3">
52+
<div className="flex w-full items-center justify-between px-3 py-3">
2253
<p>IAM User</p>
2354
<input
2455
type="checkbox"
@@ -28,7 +59,7 @@ const IAM: FC = () => {
2859
onChange={() => handleServices('iam_user')}
2960
/>
3061
</div>
31-
<div className="flex items-center justify-between w-full px-3 py-3">
62+
<div className="flex w-full items-center justify-between px-3 py-3">
3263
<p>IAM Group</p>
3364
<input
3465
type="checkbox"
@@ -40,10 +71,18 @@ const IAM: FC = () => {
4071
</div>
4172
</div>
4273
</div>
43-
<button className="w-full mt-3 text-white btn bg-orange-base hover:bg-orange-base/70">
44-
Submit
74+
<button
75+
type="submit"
76+
disabled={iamPending || downloadPending}
77+
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"
78+
>
79+
{iamPending
80+
? 'GPT Answer...'
81+
: downloadPending
82+
? 'Generate Terraform...'
83+
: 'Generate Terraform'}
4584
</button>
46-
</div>
85+
</form>
4786
);
4887
};
4988

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
export interface IAMBody {
2+
iam_user: boolean;
3+
iam_group: boolean;
4+
}
5+
6+
export interface IAMResponse {
7+
output: string;
8+
}

0 commit comments

Comments
 (0)