-
Notifications
You must be signed in to change notification settings - Fork 401
Expand file tree
/
Copy pathAccessControls.tsx
More file actions
209 lines (190 loc) · 5.87 KB
/
AccessControls.tsx
File metadata and controls
209 lines (190 loc) · 5.87 KB
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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
import { useDatabaseMutation } from '@linode/queries';
import { ActionsPanel, Notice, Typography } from '@linode/ui';
import { Button } from '@akamai/cds-components/react/Button';
import * as React from 'react';
import type { JSX } from 'react';
import { makeStyles } from 'tss-react/mui';
import { ConfirmationDialog } from 'src/components/ConfirmationDialog/ConfirmationDialog';
import { InlineMenuAction } from 'src/components/InlineMenuAction/InlineMenuAction';
import { Table } from 'src/components/Table';
import { TableBody } from 'src/components/TableBody';
import { TableCell } from 'src/components/TableCell';
import { TableRow } from 'src/components/TableRow';
import { ManageAccessControlDrawer } from './ManageAccessControlDrawer';
import type { APIError, Database } from '@linode/api-v4';
import type { Theme } from '@mui/material/styles';
const useStyles = makeStyles()((theme: Theme) => ({
addAccessControlBtn: {
minWidth: 225,
[theme.breakpoints.down('md')]: {
alignSelf: 'flex-start',
marginBottom: '1rem',
},
},
cell: {
alignItems: 'center',
borderBottom: `solid 1px ${theme.borderColors.borderTable}`,
display: 'flex',
justifyContent: 'space-between',
},
removeButton: {
float: 'right',
},
restrictWarning: {
width: '50%',
},
restrictWarningText: {
fontSize: '0.875rem,',
},
row: {
'&:last-of-type td': {
borderBottom: 'none',
},
},
sectionText: {
marginBottom: '1rem',
marginRight: 0,
[theme.breakpoints.down('sm')]: {
width: '100%',
},
width: '65%',
},
sectionTitle: {
marginBottom: '0.25rem',
},
sectionTitleAndText: {
width: '100%',
},
table: {
border: `solid 1px ${theme.borderColors.borderTable}`,
[theme.breakpoints.down('sm')]: {
width: '100%',
},
width: '50%',
},
topSection: {
alignItems: 'center',
display: 'flex',
justifyContent: 'space-between',
[theme.breakpoints.down('md')]: {
flexDirection: 'column',
},
},
}));
interface Props {
database: Database;
description?: JSX.Element;
disabled?: boolean;
}
export const AccessControls = (props: Props) => {
const { database, description, disabled } = props;
const { classes } = useStyles();
const [isDialogOpen, setDialogOpen] = React.useState<boolean>(false);
const [error, setError] = React.useState<string | undefined>();
const [accessControlToBeRemoved, setAccessControlToBeRemoved] =
React.useState<null | string>(null);
const [manageAccessControlDrawerOpen, setManageAccessControlDrawerOpen] =
React.useState<boolean>(false);
const { isPending: databaseUpdating, mutateAsync: updateDatabase } =
useDatabaseMutation(database.engine, database.id);
const handleClickRemove = (accessControl: string) => {
setError(undefined);
setDialogOpen(true);
setAccessControlToBeRemoved(accessControl);
};
const handleDialogClose = () => {
setDialogOpen(false);
};
const handleRemoveIPAddress = () => {
updateDatabase({
allow_list: database.allow_list.filter(
(ipAddress) => ipAddress !== accessControlToBeRemoved
),
})
.then(() => {
handleDialogClose();
})
.catch((e: APIError[]) => {
setError(e[0].reason);
});
};
const ipTable = (accessControlsList: string[]) => {
if (accessControlsList.length === 0) {
return null;
}
return (
<Table className={classes.table} data-qa-access-controls>
<TableBody>
{accessControlsList.map((accessControl) => (
<TableRow className={classes.row} key={`${accessControl}-row`}>
<TableCell
className={classes.cell}
key={`${accessControl}-tablecell`}
>
{accessControl}
<InlineMenuAction
actionText="Remove"
className={classes.removeButton}
disabled={disabled}
onClick={() => handleClickRemove(accessControl)}
/>
</TableCell>
</TableRow>
))}
</TableBody>
</Table>
);
};
const actionsPanel = (
<ActionsPanel
primaryButtonProps={{
label: 'Remove IP Address',
loading: databaseUpdating,
onClick: handleRemoveIPAddress,
}}
secondaryButtonProps={{ label: 'Cancel', onClick: handleDialogClose }}
/>
);
return (
<>
<div className={classes.topSection}>
<div className={classes.sectionTitleAndText}>
<div className={classes.sectionTitle}>
<Typography variant="h3">Manage Access</Typography>
</div>
<div className={classes.sectionText}>{description ?? null}</div>
</div>
<Button
className={classes.addAccessControlBtn}
data-testid="button-access-control"
disabled={disabled}
onClick={() => setManageAccessControlDrawerOpen(true)}
variant="secondary"
>
Manage Access
</Button>
</div>
{ipTable(database.allow_list)}
<ConfirmationDialog
actions={actionsPanel}
onClose={handleDialogClose}
open={isDialogOpen}
title={`Remove IP Address ${accessControlToBeRemoved}`}
>
{error ? <Notice text={error} variant="error" /> : null}
<Typography data-testid="ip-removal-confirmation-warning">
IP {accessControlToBeRemoved} will lose all access to the data on this
database cluster. This action cannot be undone, but you can re-enable
access by clicking Manage Access Controls and adding the same IP
address.
</Typography>
</ConfirmationDialog>
<ManageAccessControlDrawer
database={database}
onClose={() => setManageAccessControlDrawerOpen(false)}
open={manageAccessControlDrawerOpen}
/>
</>
);
};
export default AccessControls;