|
| 1 | +import { Dialog, Portal, Transition } from "@headlessui/react"; |
| 2 | +import { Fragment, useState } from "react"; |
| 3 | +import { tw } from "twind"; |
| 4 | +import Button from "../components/Button"; |
| 5 | +import type { ManagementFeatureFlagItem } from "../generated/common/types"; |
| 6 | +import { useManagementFeatureFlagUpdate } from "../generated/managementFeatureFlag/reactQueries"; |
| 7 | + |
| 8 | +export default function EditTenantSettingsFFModal({ |
| 9 | + show, |
| 10 | + onClose, |
| 11 | + flag, |
| 12 | + tenants, |
| 13 | +}: { |
| 14 | + show: boolean; |
| 15 | + onClose: () => void; |
| 16 | + flag: ManagementFeatureFlagItem; |
| 17 | + tenants: string[]; |
| 18 | +}) { |
| 19 | + const { mutate, status, error } = useManagementFeatureFlagUpdate( |
| 20 | + { |
| 21 | + onSuccess: () => { |
| 22 | + onClose(); |
| 23 | + }, |
| 24 | + }, |
| 25 | + { invalidateQueries: true }, |
| 26 | + ); |
| 27 | + |
| 28 | + const [tenantValues, setTenantValues] = useState({ |
| 29 | + ...(flag.tenantValues ?? {}), |
| 30 | + }); |
| 31 | + |
| 32 | + return ( |
| 33 | + <Portal> |
| 34 | + <Transition.Root show={show} as={Fragment}> |
| 35 | + <Dialog as="div" className={tw`relative z-10`} onClose={onClose}> |
| 36 | + <Transition.Child |
| 37 | + as={Fragment} |
| 38 | + enter={tw`ease-out duration-300`} |
| 39 | + enterFrom={tw`opacity-0`} |
| 40 | + enterTo={tw`opacity-100`} |
| 41 | + leave={tw`ease-in duration-200`} |
| 42 | + leaveFrom={tw`opacity-100`} |
| 43 | + leaveTo={tw`opacity-0`} |
| 44 | + > |
| 45 | + <div className={tw`fixed inset-0 bg-gray-500 bg-opacity-75 transition-opacity`} /> |
| 46 | + </Transition.Child> |
| 47 | + |
| 48 | + <div className={tw`fixed inset-0 z-10 overflow-y-auto`}> |
| 49 | + <div |
| 50 | + className={tw`flex min-h-full items-end justify-center p-4 text-center sm:items-center sm:p-0`} |
| 51 | + > |
| 52 | + <Transition.Child |
| 53 | + as={Fragment} |
| 54 | + enter={tw`ease-out duration-300`} |
| 55 | + enterFrom={tw`opacity-0 translate-y-4 sm:translate-y-0 sm:scale-95`} |
| 56 | + enterTo={tw`opacity-100 translate-y-0 sm:scale-100`} |
| 57 | + leave={tw`ease-in duration-200`} |
| 58 | + leaveFrom={tw`opacity-100 translate-y-0 sm:scale-100`} |
| 59 | + leaveTo={tw`opacity-0 translate-y-4 sm:translate-y-0 sm:scale-95`} |
| 60 | + > |
| 61 | + <Dialog.Panel |
| 62 | + as="form" |
| 63 | + data-testid="TenantSettingsFeatureFlag.modal" |
| 64 | + onSubmit={e => { |
| 65 | + e.preventDefault(); |
| 66 | + mutate({ |
| 67 | + featureFlagId: flag.id, |
| 68 | + tenantValues, |
| 69 | + globalValue: flag.globalValue, |
| 70 | + description: flag.description, |
| 71 | + }); |
| 72 | + }} |
| 73 | + className={tw`relative w-full transform overflow-hidden rounded-lg bg-white text-left shadow-xl transition-all sm:my-8 sm:max-w-lg`} |
| 74 | + > |
| 75 | + <div className={tw`bg-white`}> |
| 76 | + <div className={tw`border-b bg-white p-4 shadow-sm`}> |
| 77 | + <Dialog.Title className={tw`font-medium`}>Edit feature flag</Dialog.Title> |
| 78 | + </div> |
| 79 | + <div className={tw`p-4 sm:p-6`}> |
| 80 | + <div className={tw`space-y-4`}> |
| 81 | + {tenants.map((tenant, index) => ( |
| 82 | + <div key={`${flag.name}-${index}`} className={tw`group space-y-1`}> |
| 83 | + <h3 className={tw`mb-2 font-semibold text-sm text-gray-900`}>{tenant}</h3> |
| 84 | + <ul |
| 85 | + className={tw`items-center w-full text-sm font-medium text-gray-900 bg-white border border-gray-200 rounded-lg sm:flex`} |
| 86 | + > |
| 87 | + <li className={tw`w-full border-b border-gray-200 sm:border-b-0 sm:border-r`}> |
| 88 | + <div className={tw`flex items-center pl-3`}> |
| 89 | + <input |
| 90 | + id={`list-radio-${tenant}-${index}-global-value`} |
| 91 | + type="radio" |
| 92 | + name={`list-radio-${tenant}-${index}-global-value`} |
| 93 | + checked={!tenantValues.hasOwnProperty(tenant)} |
| 94 | + onClick={() => { |
| 95 | + /* eslint-disable-next-line @typescript-eslint/no-unused-vars */ |
| 96 | + const { [tenant]: remove, ...rest } = tenantValues; |
| 97 | + setTenantValues(rest); |
| 98 | + }} |
| 99 | + className={tw`w-4 h-4 text-blue-600 bg-gray-100 border-gray-300`} |
| 100 | + /> |
| 101 | + <label |
| 102 | + htmlFor={`list-radio-${tenant}-${index}-global-value`} |
| 103 | + className={tw`w-full py-3 ml-2 text-sm font-medium text-gray-900 select-none`} |
| 104 | + > |
| 105 | + Global value |
| 106 | + </label> |
| 107 | + </div> |
| 108 | + </li> |
| 109 | + <li className={tw`w-full border-b border-gray-200 sm:border-b-0 sm:border-r`}> |
| 110 | + <div className={tw`flex items-center pl-3`}> |
| 111 | + <input |
| 112 | + id={`list-radio-${tenant}-${index}-enabled`} |
| 113 | + type="radio" |
| 114 | + name={`list-radio-${tenant}-${index}-enabled`} |
| 115 | + checked={tenantValues.hasOwnProperty(tenant) && tenantValues[tenant]} |
| 116 | + onClick={() => setTenantValues({ ...tenantValues, [tenant]: true })} |
| 117 | + className={tw`w-4 h-4 text-blue-600 bg-gray-100 border-gray-300`} |
| 118 | + /> |
| 119 | + <label |
| 120 | + htmlFor={`list-radio-${tenant}-${index}-enabled`} |
| 121 | + className={tw`w-full py-3 ml-2 text-sm font-medium text-gray-900 select-none`} |
| 122 | + > |
| 123 | + Enabled |
| 124 | + </label> |
| 125 | + </div> |
| 126 | + </li> |
| 127 | + <li className={tw`w-full border-b border-gray-200 sm:border-b-0`}> |
| 128 | + <div className={tw`flex items-center pl-3`}> |
| 129 | + <input |
| 130 | + id={`list-radio-${tenant}-${index}-disabled`} |
| 131 | + type="radio" |
| 132 | + name={`list-radio-${tenant}-${index}-disabled`} |
| 133 | + checked={tenantValues.hasOwnProperty(tenant) && !tenantValues[tenant]} |
| 134 | + onClick={() => setTenantValues({ ...tenantValues, [tenant]: false })} |
| 135 | + className={tw`w-4 h-4 text-blue-600 bg-gray-100 border-gray-300`} |
| 136 | + /> |
| 137 | + <label |
| 138 | + htmlFor={`list-radio-${tenant}-${index}-disabled`} |
| 139 | + className={tw`w-full py-3 ml-2 text-sm font-medium text-gray-900 select-none`} |
| 140 | + > |
| 141 | + Disabled |
| 142 | + </label> |
| 143 | + </div> |
| 144 | + </li> |
| 145 | + </ul> |
| 146 | + </div> |
| 147 | + ))} |
| 148 | + {!!error && ( |
| 149 | + <div className={tw`rounded-md bg-red-50`}> |
| 150 | + <p className={tw`p-4 text-sm font-medium text-red-800`} role="alert"> |
| 151 | + Something went wrong |
| 152 | + </p> |
| 153 | + {!!error.response?.data && ( |
| 154 | + <div |
| 155 | + className={tw`max-h-[100px] overflow-y-auto p-4 shadow-inner`} |
| 156 | + aria-hidden="true" |
| 157 | + > |
| 158 | + <pre className={tw`whitespace-pre-line text-sm text-red-800`}> |
| 159 | + {JSON.stringify(error.response.data)} |
| 160 | + </pre> |
| 161 | + </div> |
| 162 | + )} |
| 163 | + </div> |
| 164 | + )} |
| 165 | + </div> |
| 166 | + </div> |
| 167 | + </div> |
| 168 | + <div className={tw`border-t px-4 py-3 sm:flex sm:flex-row-reverse sm:px-6`}> |
| 169 | + <Button |
| 170 | + data-testid="TenantSettingsFeatureFlag.modal.submit" |
| 171 | + type="submit" |
| 172 | + isLoading={ |
| 173 | + // Compatible between RQ4 & RQ5 |
| 174 | + ["loading", "pending"].includes(status as string) |
| 175 | + } |
| 176 | + > |
| 177 | + Save |
| 178 | + </Button> |
| 179 | + <Button |
| 180 | + variant="default" |
| 181 | + className={tw`ml-2 lg:ml-0 lg:mr-2`} |
| 182 | + onClick={() => { |
| 183 | + onClose(); |
| 184 | + }} |
| 185 | + > |
| 186 | + Cancel |
| 187 | + </Button> |
| 188 | + </div> |
| 189 | + </Dialog.Panel> |
| 190 | + </Transition.Child> |
| 191 | + </div> |
| 192 | + </div> |
| 193 | + </Dialog> |
| 194 | + </Transition.Root> |
| 195 | + </Portal> |
| 196 | + ); |
| 197 | +} |
0 commit comments