|
1 | | -import PlatformBox from "../../../components/internal-ui/PlatformBox"; |
2 | | -import useFindPlatform from "../../../hooks/useFindPlatform"; |
3 | | -import { TerraformServices } from "../../constants"; |
4 | | -import { |
5 | | - ApiRequestTerraformArgocd, |
6 | | - TerraformArgocdFormData, |
7 | | -} from "../../models"; |
| 1 | +import { useEffect, useState } from "react"; |
| 2 | +import { cn } from "../../../utils/tailwind"; |
| 3 | +import { FaChevronDown } from "react-icons/fa"; |
| 4 | +import apiClient from "../../../utils/apiClient"; |
| 5 | +import { Endpoints } from "../../constants"; |
| 6 | +import { useMutation } from "@tanstack/react-query"; |
| 7 | +import { AxiosResponse } from "axios"; |
8 | 8 |
|
9 | 9 | const Argocd = () => { |
10 | | - const { platform } = useFindPlatform(TerraformServices.ARGOCD); |
| 10 | + const [buttons, setButtons] = useState({ |
| 11 | + auto_prune: false, |
| 12 | + self_heal: false, |
| 13 | + argocd_repository: false, |
| 14 | + application_depends_repository: false |
| 15 | + }); |
| 16 | + |
| 17 | + const [menus, setMenus] = useState({ |
| 18 | + argocd_application: true, |
| 19 | + sync_policy: true |
| 20 | + }); |
| 21 | + |
| 22 | + const [error, setError] = useState(false); |
| 23 | + |
| 24 | + const {data, isPending, isError, isSuccess, mutate} = useMutation<AxiosResponse, Error, {body: {argocd_application: {sync_policy: {auto_prune: boolean, self_heal: boolean}} | null, argocd_repository: boolean, application_depends_repository: boolean}}>({ |
| 25 | + mutationKey: ["argocd"], |
| 26 | + mutationFn: async ({body}) => { |
| 27 | + await apiClient.post(Endpoints.POST_IAC_ARGOCD, {...body}); |
| 28 | + return await apiClient.get(`${Endpoints.GET_DOWNLOAD_TERRAFORM}MyTerraform/argocd`, {responseType: "blob"}); |
| 29 | + } |
| 30 | + }) |
| 31 | + |
| 32 | + useEffect(() => { |
| 33 | + if (isError) { |
| 34 | + setError(true); |
| 35 | + setTimeout(() => { |
| 36 | + setError(false); |
| 37 | + }, 3000); |
| 38 | + } |
| 39 | + }, [isError]); |
| 40 | + |
| 41 | + useEffect(() => { |
| 42 | + if (isSuccess && data) { |
| 43 | + const blob = new Blob([data.data], { type: data.headers['content-type'] }); |
| 44 | + const link = document.createElement('a'); |
| 45 | + link.href = URL.createObjectURL(blob); |
| 46 | + link.download = "Argocd.zip"; |
| 47 | + document.body.appendChild(link); |
| 48 | + link.click(); |
| 49 | + document.body.removeChild(link); |
| 50 | + } |
| 51 | + }, [isSuccess, data]) |
| 52 | + |
| 53 | + useEffect(() => { |
| 54 | + if (!menus.argocd_application) { |
| 55 | + setButtons({ |
| 56 | + ...buttons, |
| 57 | + auto_prune: false, |
| 58 | + self_heal: false |
| 59 | + }) |
| 60 | + } |
| 61 | + }, [menus]) |
| 62 | + |
| 63 | + const handleButtons = (button: keyof typeof buttons) => { |
| 64 | + setButtons({...buttons, [button]: !buttons[button]}) |
| 65 | + } |
| 66 | + |
| 67 | + const handleMenus = (menu: keyof typeof menus) => { |
| 68 | + setMenus({...menus, [menu]: !menus[menu]}) |
| 69 | + } |
| 70 | + |
| 71 | + const handleSubmit = async () => { |
| 72 | + const body = { |
| 73 | + argocd_application: ( |
| 74 | + menus.argocd_application |
| 75 | + ? { |
| 76 | + sync_policy: { |
| 77 | + auto_prune: buttons.auto_prune, |
| 78 | + self_heal: buttons.self_heal |
| 79 | + } |
| 80 | + } |
| 81 | + : null |
| 82 | + ), |
| 83 | + argocd_repository: buttons.argocd_repository, |
| 84 | + application_depends_repository: buttons.application_depends_repository |
| 85 | + }; |
| 86 | + |
| 87 | + mutate({body}) |
| 88 | + } |
11 | 89 |
|
12 | 90 | return ( |
13 | | - <> |
14 | | - {platform && ( |
15 | | - <PlatformBox |
16 | | - defaultValues={platform.defaultValues as TerraformArgocdFormData} |
17 | | - endpoint={platform.endpoint} |
18 | | - fieldProperties={platform.fieldProperties} |
19 | | - mapperFunction={ |
20 | | - platform.mapperFunction as () => ApiRequestTerraformArgocd |
21 | | - } |
22 | | - serviceName={platform.serviceName} |
23 | | - /> |
24 | | - )} |
25 | | - </> |
| 91 | + <div className="flex flex-col items-center justify-center"> |
| 92 | + <div className="w-full bg-orange-800 divide-y-2 divide-gray-300 rounded-md max-w-96"> |
| 93 | + <div className="divide-y-2 divide-gray-300"> |
| 94 | + <div className="flex items-center justify-between w-full px-4 py-2"> |
| 95 | + <p>Argocd Application</p> |
| 96 | + <input type="checkbox" className={cn('border-orange-[#2e323a] toggle [--tglbg:#2e323a] bg-orange-300 hover:bg-orange-400', { |
| 97 | + 'bg-[#5b6372] hover:bg-[#5b6372]': !menus.argocd_application, |
| 98 | + }) } checked={menus.argocd_application} onChange={() => {handleMenus("argocd_application")}} /> |
| 99 | + </div> |
| 100 | + <div className={cn("divide-y-2 divide-gray-300 max-h-0 overflow-hidden transition-all ease-linear duration-300", {"max-h-96": menus.argocd_application})}> |
| 101 | + <button className="flex items-center justify-between w-full py-2 pl-10 pr-4" onClick={() => handleMenus("sync_policy")}> |
| 102 | + <p>Sync Policy</p> |
| 103 | + <FaChevronDown className={cn('transition-all', {"rotate-180": menus.sync_policy})} /> |
| 104 | + </button> |
| 105 | + <div className={cn("divide-y-2 divide-gray-300 max-h-0 overflow-hidden transition-all ease-linear duration-300", {"max-h-96": menus.sync_policy})}> |
| 106 | + <div className="py-2 pl-16 pr-4"> |
| 107 | + <div className="flex items-center justify-between"> |
| 108 | + <p>Auto Prune</p> |
| 109 | + <input type="checkbox" className={cn('border-orange-[#2e323a] toggle [--tglbg:#2e323a] bg-orange-300 hover:bg-orange-400', { |
| 110 | + 'bg-[#5b6372] hover:bg-[#5b6372]': !buttons.auto_prune, |
| 111 | + }) } checked={buttons.auto_prune} onChange={() => handleButtons("auto_prune")} /> |
| 112 | + </div> |
| 113 | + </div> |
| 114 | + <div className="py-2 pl-16 pr-4"> |
| 115 | + <div className="flex items-center justify-between"> |
| 116 | + <p>Self Heal</p> |
| 117 | + <input type="checkbox" className={cn('border-orange-[#2e323a] toggle [--tglbg:#2e323a] bg-orange-300 hover:bg-orange-400', { |
| 118 | + 'bg-[#5b6372] hover:bg-[#5b6372]': !buttons.self_heal, |
| 119 | + }) } checked={buttons.self_heal} onChange={() => handleButtons("self_heal")} /> |
| 120 | + </div> |
| 121 | + </div> |
| 122 | + </div> |
| 123 | + </div> |
| 124 | + <div className="px-4 py-2"> |
| 125 | + <div className="flex items-center justify-between"> |
| 126 | + <p>Argocd Repository</p> |
| 127 | + <input type="checkbox" className={cn('border-orange-[#2e323a] toggle [--tglbg:#2e323a] bg-orange-300 hover:bg-orange-400', { |
| 128 | + 'bg-[#5b6372] hover:bg-[#5b6372]': !buttons.argocd_repository, |
| 129 | + }) } checked={buttons.argocd_repository} onChange={() => handleButtons("argocd_repository")} /> |
| 130 | + </div> |
| 131 | + </div> |
| 132 | + <div className="px-4 py-2"> |
| 133 | + <div className="flex items-center justify-between"> |
| 134 | + <p>Application Depends Repository</p> |
| 135 | + <input type="checkbox" className={cn('border-orange-[#2e323a] toggle [--tglbg:#2e323a] bg-orange-300 hover:bg-orange-400', { |
| 136 | + 'bg-[#5b6372] hover:bg-[#5b6372]': !buttons.application_depends_repository, |
| 137 | + }) } checked={buttons.application_depends_repository} onChange={() => handleButtons("application_depends_repository")} /> |
| 138 | + </div> |
| 139 | + </div> |
| 140 | + </div> |
| 141 | + </div> |
| 142 | + <button disabled={isPending} onClick={() => handleSubmit()} className="w-full py-2 mt-4 text-center transition-all bg-orange-800 rounded-md btn max-w-96 hover:bg-orange-900">{isPending ? <div className="flex items-center justify-center gap-4 disabled:opacity-80"><span className="loading loading-spinner"></span>Loading</div>: error ? "Error" : "Submit"}</button> |
| 143 | + </div> |
26 | 144 | ); |
27 | 145 | }; |
28 | 146 |
|
|
0 commit comments