|
| 1 | +import React, { useEffect, useMemo, useState } from 'react'; |
| 2 | +import { useFieldArray, useForm } from 'react-hook-form'; |
| 3 | +import { useTranslation } from 'react-i18next'; |
| 4 | + |
| 5 | +import { |
| 6 | + Button, |
| 7 | + ButtonWithConfirmation, |
| 8 | + FormInput, |
| 9 | + Header, |
| 10 | + ListEmptyMessage, |
| 11 | + Pagination, |
| 12 | + SpaceBetween, |
| 13 | + Table, |
| 14 | +} from 'components'; |
| 15 | + |
| 16 | +import { useCollection } from 'hooks'; |
| 17 | +import { useDeleteSecretsMutation, useGetAllSecretsQuery, useUpdateSecretMutation } from 'services/secrets'; |
| 18 | + |
| 19 | +import { IProps, TFormSecretValue, TFormValues, TProjectSecretWithIndex } from './types'; |
| 20 | + |
| 21 | +import styles from './styles.module.scss'; |
| 22 | + |
| 23 | +export const ProjectSecrets: React.FC<IProps> = ({ project, loading }) => { |
| 24 | + const { t } = useTranslation(); |
| 25 | + const [editableRowIndex, setEditableRowIndex] = useState<number | null>(null); |
| 26 | + const projectName = project?.project_name ?? ''; |
| 27 | + |
| 28 | + const { data, isLoading, isFetching } = useGetAllSecretsQuery({ project_name: projectName }); |
| 29 | + const [updateSecret, { isLoading: isUpdating }] = useUpdateSecretMutation(); |
| 30 | + const [deleteSecret, { isLoading: isDeleting }] = useDeleteSecretsMutation(); |
| 31 | + |
| 32 | + const { handleSubmit, control, getValues, setValue } = useForm<TFormValues>({ |
| 33 | + defaultValues: { secrets: [] }, |
| 34 | + }); |
| 35 | + |
| 36 | + useEffect(() => { |
| 37 | + if (data) { |
| 38 | + setValue( |
| 39 | + 'secrets', |
| 40 | + data.map((s) => ({ ...s, serverId: s.id })), |
| 41 | + ); |
| 42 | + } |
| 43 | + }, [data]); |
| 44 | + |
| 45 | + const { fields, append, remove } = useFieldArray({ |
| 46 | + control, |
| 47 | + name: 'secrets', |
| 48 | + }); |
| 49 | + |
| 50 | + const fieldsWithIndex = useMemo(() => { |
| 51 | + return fields.map<TProjectSecretWithIndex>((field, index) => ({ ...field, index })); |
| 52 | + }, [fields]); |
| 53 | + |
| 54 | + const { items, paginationProps, collectionProps } = useCollection(fieldsWithIndex, { |
| 55 | + filtering: { |
| 56 | + empty: ( |
| 57 | + <ListEmptyMessage |
| 58 | + title={t('projects.edit.secrets.empty_message_title')} |
| 59 | + message={t('projects.edit.secrets.empty_message_text')} |
| 60 | + /> |
| 61 | + ), |
| 62 | + }, |
| 63 | + pagination: { pageSize: 10 }, |
| 64 | + selection: {}, |
| 65 | + }); |
| 66 | + |
| 67 | + const { selectedItems } = collectionProps; |
| 68 | + |
| 69 | + const deleteSelectedSecrets = () => { |
| 70 | + const names = selectedItems?.map((s) => s.name ?? ''); |
| 71 | + |
| 72 | + if (names?.length) { |
| 73 | + deleteSecret({ project_name: projectName, names }).then(() => { |
| 74 | + selectedItems?.forEach((s) => remove(s.index)); |
| 75 | + }); |
| 76 | + } |
| 77 | + }; |
| 78 | + |
| 79 | + const removeSecretByIndex = (index: number) => { |
| 80 | + const secretData = getValues().secrets?.[index]; |
| 81 | + |
| 82 | + if (!secretData || !secretData.name) { |
| 83 | + return; |
| 84 | + } |
| 85 | + |
| 86 | + deleteSecret({ project_name: projectName, names: [secretData.name] }).then(() => { |
| 87 | + remove(index); |
| 88 | + }); |
| 89 | + }; |
| 90 | + |
| 91 | + const saveSecretByIndex = (index: number) => { |
| 92 | + const secretData = getValues().secrets?.[index]; |
| 93 | + |
| 94 | + if (!secretData || !secretData.name || !secretData.value) { |
| 95 | + return; |
| 96 | + } |
| 97 | + |
| 98 | + updateSecret({ project_name: projectName, name: secretData.name, value: secretData.value }) |
| 99 | + .unwrap() |
| 100 | + .then(() => { |
| 101 | + setEditableRowIndex(null); |
| 102 | + }); |
| 103 | + }; |
| 104 | + |
| 105 | + const isDisabledEditableRowActions = loading || isLoading || isFetching || isUpdating; |
| 106 | + const isDisabledNotEditableRowActions = loading || isLoading || isFetching || isDeleting; |
| 107 | + |
| 108 | + const COLUMN_DEFINITIONS = [ |
| 109 | + { |
| 110 | + id: 'name', |
| 111 | + header: t('projects.edit.secrets.name'), |
| 112 | + cell: (field: TFormSecretValue & { index: number }) => { |
| 113 | + const isEditable = editableRowIndex === field.index; |
| 114 | + |
| 115 | + return ( |
| 116 | + <div className={styles.value}> |
| 117 | + <div className={styles.valueFieldWrapper}> |
| 118 | + <FormInput |
| 119 | + key={field.name} |
| 120 | + control={control} |
| 121 | + name={`secrets.${field.index}.name`} |
| 122 | + disabled={loading || !isEditable || !!field.serverId} |
| 123 | + /> |
| 124 | + </div> |
| 125 | + </div> |
| 126 | + ); |
| 127 | + }, |
| 128 | + }, |
| 129 | + { |
| 130 | + id: 'value', |
| 131 | + header: t('projects.edit.secrets.value'), |
| 132 | + cell: (field: TFormSecretValue & { index: number }) => { |
| 133 | + const isEditable = editableRowIndex === field.index; |
| 134 | + |
| 135 | + return ( |
| 136 | + <div className={styles.value}> |
| 137 | + <div className={styles.valueFieldWrapper}> |
| 138 | + <FormInput |
| 139 | + readOnly={!isEditable} |
| 140 | + key={field.value} |
| 141 | + control={control} |
| 142 | + name={`secrets.${field.index}.value`} |
| 143 | + disabled={loading || !isEditable} |
| 144 | + /> |
| 145 | + </div> |
| 146 | + |
| 147 | + <div className={styles.buttonsWrapper}> |
| 148 | + {isEditable && ( |
| 149 | + <Button |
| 150 | + disabled={isDisabledEditableRowActions} |
| 151 | + formAction="none" |
| 152 | + onClick={() => saveSecretByIndex(field.index)} |
| 153 | + variant="icon" |
| 154 | + iconName="check" |
| 155 | + /> |
| 156 | + )} |
| 157 | + |
| 158 | + {!isEditable && ( |
| 159 | + <Button |
| 160 | + disabled={isDisabledNotEditableRowActions} |
| 161 | + formAction="none" |
| 162 | + onClick={() => setEditableRowIndex(field.index)} |
| 163 | + variant="icon" |
| 164 | + iconName="edit" |
| 165 | + /> |
| 166 | + )} |
| 167 | + |
| 168 | + {!isEditable && ( |
| 169 | + <ButtonWithConfirmation |
| 170 | + disabled={isDisabledNotEditableRowActions} |
| 171 | + formAction="none" |
| 172 | + onClick={() => removeSecretByIndex(field.index)} |
| 173 | + confirmTitle={t('projects.edit.secrets.delete_confirm_title')} |
| 174 | + confirmContent={t('projects.edit.secrets.delete_confirm_message')} |
| 175 | + variant="icon" |
| 176 | + iconName="remove" |
| 177 | + /> |
| 178 | + )} |
| 179 | + </div> |
| 180 | + </div> |
| 181 | + ); |
| 182 | + }, |
| 183 | + }, |
| 184 | + ]; |
| 185 | + |
| 186 | + const addSecretHandler = () => { |
| 187 | + append({}); |
| 188 | + setEditableRowIndex(fields.length); |
| 189 | + }; |
| 190 | + |
| 191 | + const renderActions = () => { |
| 192 | + const actions = [ |
| 193 | + <Button key="add" formAction="none" onClick={addSecretHandler}> |
| 194 | + {t('common.add')} |
| 195 | + </Button>, |
| 196 | + |
| 197 | + <ButtonWithConfirmation |
| 198 | + key="delete" |
| 199 | + disabled={isDisabledNotEditableRowActions || !selectedItems?.length} |
| 200 | + formAction="none" |
| 201 | + onClick={deleteSelectedSecrets} |
| 202 | + confirmTitle={t('projects.edit.secrets.delete_confirm_title')} |
| 203 | + confirmContent={t('projects.edit.secrets.delete_confirm_message')} |
| 204 | + > |
| 205 | + {t('common.delete')} |
| 206 | + </ButtonWithConfirmation>, |
| 207 | + ]; |
| 208 | + |
| 209 | + return actions.length > 0 ? ( |
| 210 | + <SpaceBetween size="xs" direction="horizontal"> |
| 211 | + {actions} |
| 212 | + </SpaceBetween> |
| 213 | + ) : undefined; |
| 214 | + }; |
| 215 | + |
| 216 | + return ( |
| 217 | + <form onSubmit={handleSubmit(() => {})}> |
| 218 | + <Table |
| 219 | + {...collectionProps} |
| 220 | + selectionType="multi" |
| 221 | + columnDefinitions={COLUMN_DEFINITIONS} |
| 222 | + items={items} |
| 223 | + loading={isLoading} |
| 224 | + header={ |
| 225 | + <Header variant="h2" counter={`(${items?.length})`} actions={renderActions()}> |
| 226 | + {t('projects.edit.secrets.section_title')} |
| 227 | + </Header> |
| 228 | + } |
| 229 | + pagination={<Pagination {...paginationProps} />} |
| 230 | + /> |
| 231 | + </form> |
| 232 | + ); |
| 233 | +}; |
0 commit comments