Skip to content

Commit 5d036cd

Browse files
committed
feat(web): Implement download installation shell script functionality
1 parent 553b7ac commit 5d036cd

3 files changed

Lines changed: 93 additions & 8 deletions

File tree

web/src/enums/api.enums.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,4 +9,5 @@ export enum TerraformTemplateAPI {
99
export enum API {
1010
Basic = '/IaC-basic',
1111
BugFix = '/IaC-bugfix',
12+
Installation = '/IaC-install',
1213
}

web/src/pages/installation/installation.tsx

Lines changed: 70 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,86 @@
1-
import { FC } from 'react';
1+
import { usePost } from '@/core/react-query';
2+
import { API } from '@/enums/api.enums';
3+
import { FC, FormEvent, useState } from 'react';
4+
import {
5+
InstallationBody,
6+
InstallationResponse,
7+
InstallationValidationError,
8+
} from './installation.types';
9+
import { toast } from 'sonner';
10+
import { isAxiosError } from 'axios';
211

312
const Installation: FC = () => {
13+
const { mutateAsync, isPending } = usePost<
14+
InstallationResponse,
15+
InstallationBody
16+
>(API.Installation, 'installation');
17+
18+
const [os, setOs] = useState('');
19+
const [tool, setTool] = useState('');
20+
21+
const handleInstall = async (e: FormEvent) => {
22+
e.preventDefault();
23+
24+
try {
25+
const installationBody: InstallationBody = {
26+
os,
27+
service: tool,
28+
};
29+
30+
const {
31+
data: { output },
32+
} = await mutateAsync(installationBody);
33+
34+
const blob = new Blob([output], { type: 'text/plain' });
35+
36+
const url = URL.createObjectURL(blob);
37+
38+
const link = document.createElement('a');
39+
link.href = url;
40+
link.download = 'installation.sh';
41+
document.body.appendChild(link);
42+
link.click();
43+
44+
document.body.removeChild(link);
45+
URL.revokeObjectURL(url);
46+
} catch (error) {
47+
if (isAxiosError<InstallationValidationError>(error)) {
48+
toast.error(error.response?.data.detail[0].msg);
49+
} else {
50+
toast.error('Something went wrong');
51+
}
52+
}
53+
};
54+
455
return (
5-
<div className="flex items-center justify-center w-full h-full">
56+
<form
57+
onSubmit={handleInstall}
58+
className="flex h-full w-full items-center justify-center"
59+
>
660
<div className="w-full max-w-96">
7-
<div className="border border-gray-500 divide-y divide-gray-500 rounded-md">
61+
<div className="divide-y divide-gray-500 rounded-md border border-gray-500">
862
<input
63+
value={os}
64+
onChange={(e) => setOs(e.target.value)}
965
placeholder="os (example: ubuntu)"
10-
className="block w-full p-2 outline-none rounded-t-md"
66+
className="block w-full rounded-t-md p-2 outline-none"
1167
/>
1268
<input
69+
value={tool}
70+
onChange={(e) => setTool(e.target.value)}
1371
placeholder="tool (example: nginx)"
14-
className="block w-full p-2 outline-none rounded-b-md"
72+
className="block w-full rounded-b-md p-2 outline-none"
1573
/>
1674
</div>
17-
<button className="w-full mt-3 text-white btn bg-orange-base hover:bg-orange-base/70">
18-
Submit
75+
<button
76+
type="submit"
77+
disabled={isPending}
78+
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"
79+
>
80+
{isPending ? 'Wait...' : 'Install'}
1981
</button>
2082
</div>
21-
</div>
83+
</form>
2284
);
2385
};
2486

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
export interface InstallationBody {
2+
os: string;
3+
service: string;
4+
}
5+
6+
export interface InstallationResponse {
7+
output: string;
8+
}
9+
10+
export interface InstallationValidationError {
11+
detail: [
12+
{
13+
type: string;
14+
loc: string[];
15+
msg: string;
16+
input: string;
17+
ctx: {
18+
error: {};
19+
};
20+
},
21+
];
22+
}

0 commit comments

Comments
 (0)