-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathSecretProperty.tsx
112 lines (107 loc) · 3.11 KB
/
SecretProperty.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
import { Switch } from '@material-ui/core';
import RemoveCircleOutlineIcon from '@material-ui/icons/RemoveCircleOutline';
import { Button, Flex, Icon, IconType, Text } from '@weaveworks/weave-gitops';
import { Dispatch } from 'react';
import styled from 'styled-components';
import { InputDebounced } from '../../../utils/form';
import { ExternalSecret } from '../Shared/utils';
const PropertiesSwitch = styled(Switch)`
.MuiSwitch-track {
background-color: ${({ theme }) => theme.colors.primary30};
}
`;
export const SecretProperty = ({
formData,
setFormData,
formError,
}: {
formData: ExternalSecret;
setFormData: Dispatch<React.SetStateAction<any>>;
formError: string;
}) => {
const handleSecretChange = (id: number, isKey: boolean, value: string) => {
setFormData((f: ExternalSecret) => ({
...f,
data: f.data.map(p => {
if (p.id !== id) return p;
if (isKey) p.key = value;
else p.value = value;
return p;
}),
}));
};
const handleRemoveProp = (id: number) => {
setFormData((f: ExternalSecret) => ({
...f,
data: f.data.filter(e => e.id !== id),
}));
};
const handleNewProp = () => {
setFormData((f: ExternalSecret) => ({
...f,
data: [
...f.data,
{
id: formData.data[formData.data.length - 1].id + 1,
key: '',
value: '',
},
],
}));
};
return (
<Flex gap="16" column wide>
<Text size="medium" semiBold>
<PropertiesSwitch
color="primary"
value={formData.includeAllProps}
onChange={(evt, checked) =>
setFormData((f: ExternalSecret) => ({
...f,
includeAllProps: checked,
}))
}
/>
Include all properties
</Text>
{!formData.includeAllProps && (
<Flex column wide>
{formData.data.map(obj => (
<div key={obj.id} className="secret-data-list">
<InputDebounced
required
name="dataSecretKey"
label="PROPERTY"
placeholder="Secret Property"
value={obj.key}
handleFormData={val => handleSecretChange(obj.id, true, val)}
formError={formError}
/>
<InputDebounced
name="dataSecretValue"
label="Secret Key"
placeholder="Secret key"
value={obj.value}
handleFormData={val => handleSecretChange(obj.id, false, val)}
/>
{formData.data.length > 1 && (
<RemoveCircleOutlineIcon
style={{ marginRight: '-30px' }}
className="remove-icon"
onClick={() => handleRemoveProp(obj.id)}
/>
)}
</div>
))}
<Button
className="add-secret-data"
startIcon={<Icon type={IconType.AddIcon} size="base" />}
onClick={() => handleNewProp()}
>
Add
</Button>
</Flex>
)}
</Flex>
);
};