|
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'; |
2 | 11 |
|
3 | 12 | 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 | + |
4 | 55 | 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 | + > |
6 | 60 | <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"> |
8 | 62 | <input |
| 63 | + value={os} |
| 64 | + onChange={(e) => setOs(e.target.value)} |
9 | 65 | 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" |
11 | 67 | /> |
12 | 68 | <input |
| 69 | + value={tool} |
| 70 | + onChange={(e) => setTool(e.target.value)} |
13 | 71 | 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" |
15 | 73 | /> |
16 | 74 | </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'} |
19 | 81 | </button> |
20 | 82 | </div> |
21 | | - </div> |
| 83 | + </form> |
22 | 84 | ); |
23 | 85 | }; |
24 | 86 |
|
|
0 commit comments