Skip to content

Commit 8abb24b

Browse files
committed
Add dialog to delete network.
Signed-off-by: Gautier Bureau <[email protected]>
1 parent 6edb7e4 commit 8abb24b

File tree

4 files changed

+67
-0
lines changed

4 files changed

+67
-0
lines changed

src/components/2-molecules/AttachDialog.jsx

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,15 +14,22 @@ import DialogTitle from '@material-ui/core/DialogTitle';
1414
import PropTypes from 'prop-types';
1515
import { Divider, Grid, Typography } from '@material-ui/core';
1616
import Box from '@material-ui/core/Box';
17+
import ListItem from '@material-ui/core/ListItem';
18+
import ListItemText from '@material-ui/core/ListItemText';
19+
import List from '@material-ui/core/List';
1720
import { useStyles } from './AttachDialogStyles';
1821
import Autocomplete from '../1-atoms/Autocomplete';
22+
import { useDispatch } from 'react-redux';
23+
import { deleteNetwork as deleteMappingAction } from '../../redux/slices/Network';
1924

2025
const AttachDialog = (props) => {
2126
const { open, handleClose, attachWithFile, networks, attachWithId } = props;
2227
const [file, setFile] = useState(null);
2328
const [networkId, setNetworkId] = useState('');
2429
const classes = useStyles();
2530

31+
const dispatch = useDispatch();
32+
2633
const onChangeFile = (event) => {
2734
setFile(event.target.files[0]);
2835
};
@@ -42,6 +49,10 @@ const AttachDialog = (props) => {
4249
setNetworkId('');
4350
};
4451

52+
const deleteNetwork = (networkId) => {
53+
dispatch(deleteMappingAction(networkId));
54+
};
55+
4556
return (
4657
<Dialog
4758
open={open}
@@ -103,6 +114,23 @@ const AttachDialog = (props) => {
103114
)}
104115
</DialogContent>
105116
<Divider />
117+
<DialogContent>
118+
<Typography>Delete a known network :</Typography>
119+
<List>
120+
{networks.map((network) => (
121+
<ListItem
122+
button
123+
key={network.networkName}
124+
onClick={() => deleteNetwork(network.networkId)}
125+
>
126+
<ListItemText>
127+
primary={`${network.networkName}`}
128+
</ListItemText>
129+
</ListItem>
130+
))}
131+
</List>
132+
</DialogContent>
133+
<Divider />
106134
<DialogActions>
107135
<Button onClick={closeDialog} color="primary">
108136
Cancel

src/redux/slices/Network.js

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,19 @@ export const getNetworkNames = createAsyncThunk(
9595
}
9696
);
9797

98+
export const deleteNetwork = createAsyncThunk(
99+
'network/delete',
100+
async (networkId, { getState }) => {
101+
const token = getState()?.user.user?.id_token;
102+
const response = await networkAPI.deleteNetwork(networkId, token);
103+
104+
if (!response.ok) {
105+
throw response;
106+
}
107+
return response.text();
108+
}
109+
);
110+
98111
const reducers = {
99112
cleanNetwork: (state) => {
100113
state.propertyValues = [];
@@ -140,6 +153,19 @@ const extraReducers = {
140153
[getNetworkNames.pending]: (state, _action) => {
141154
state.status = RequestStatus.PENDING;
142155
},
156+
[deleteNetwork.fulfilled]: (state, action) => {
157+
state.status = RequestStatus.SUCCESS;
158+
const name = action.payload;
159+
state.knownNetworks = state.knownNetworks.filter(
160+
(network) => network.networkId !== name
161+
);
162+
},
163+
[deleteNetwork.rejected]: (state, _action) => {
164+
state.status = RequestStatus.ERROR;
165+
},
166+
[deleteNetwork.pending]: (state, _action) => {
167+
state.status = RequestStatus.PENDING;
168+
},
143169
};
144170

145171
export const NetworkSlice = createSlice({

src/redux/slices/Script.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,7 @@ export const deleteScript = createAsyncThunk(
9393
return response.text();
9494
}
9595
);
96+
9697
export const copyScript = createAsyncThunk(
9798
'scripts/copy',
9899
async ({ originalName, copyName }, { getState }) => {

src/rest/networkAPI.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,3 +63,15 @@ export function getNetworkMatchesFromRule(networkId, ruleToMatch, token) {
6363
body: JSON.stringify(ruleToMatch),
6464
});
6565
}
66+
67+
export function deleteNetwork(networkId, token) {
68+
return fetch(`${API_URL}/${networkId}`, {
69+
method: 'DELETE',
70+
headers: {
71+
Accept: 'application/json',
72+
'Content-Type': 'application/json',
73+
Authorization: 'Bearer ' + token,
74+
},
75+
cache: 'default',
76+
});
77+
}

0 commit comments

Comments
 (0)