delete user with confirmation modal

This commit is contained in:
grimhilt 2023-08-08 17:21:09 +02:00
parent f19a6fa57c
commit 18f487832e
5 changed files with 98 additions and 20 deletions

View File

@ -2,22 +2,25 @@ import Layout from './components/layout';
import { MantineProvider, ColorSchemeProvider } from '@mantine/core'; import { MantineProvider, ColorSchemeProvider } from '@mantine/core';
import { AuthProvider } from './tools/auth-provider'; import { AuthProvider } from './tools/auth-provider';
import { Notifications } from '@mantine/notifications'; import { Notifications } from '@mantine/notifications';
import { useColorSchemeToggle } from './tools/color-scheme-toggle' import { useColorSchemeToggle } from './tools/color-scheme-toggle';
import { ModalsProvider } from '@mantine/modals';
const App = () => { const App = () => {
const [ colorScheme, toggleColorScheme ] = useColorSchemeToggle(); const [colorScheme, toggleColorScheme] = useColorSchemeToggle();
return ( return (
<> <>
<AuthProvider> <AuthProvider>
<ColorSchemeProvider colorScheme={colorScheme} toggleColorScheme={toggleColorScheme}> <ColorSchemeProvider colorScheme={colorScheme} toggleColorScheme={toggleColorScheme}>
<ModalsProvider>
<MantineProvider withGlobalStyles withNormalizeCSS theme={{ colorScheme }}> <MantineProvider withGlobalStyles withNormalizeCSS theme={{ colorScheme }}>
<Notifications /> <Notifications />
<Layout /> <Layout />
</MantineProvider> </MantineProvider>
</ModalsProvider>
</ColorSchemeProvider> </ColorSchemeProvider>
</AuthProvider> </AuthProvider>
</> </>
); );
}; };
// todo theme for modal provider
export default App; export default App;

View File

@ -0,0 +1,37 @@
import { Modal, Text, Group, Button } from '@mantine/core';
const ConfirmationModal = ({ opened, handler, text, action }) => {
const confirm = () => {
action();
handler();
};
return (
<Modal.Root opened={opened} onClose={handler}>
<Modal.Overlay />
<Modal.Content>
<Modal.Header>
<Modal.Title>
<Text fw={700} fz="lg">
Confirmation
</Text>
</Modal.Title>
<Modal.CloseButton />
</Modal.Header>
<Modal.Body>
<Text>{text ?? 'Are you sure you want to do this ?'}</Text>
<Group position="right" mt="md">
<Button variant="light" color="red" onClick={handler}>
Cancel
</Button>
<Button variant="light" color="green" onClick={confirm}>
Confirm
</Button>
</Group>
</Modal.Body>
</Modal.Content>
</Modal.Root>
);
};
export default ConfirmationModal;

View File

@ -16,6 +16,7 @@ const Users = () => {
const toggleModalUpdate = () => setShowUpdate(!showUpdate); const toggleModalUpdate = () => setShowUpdate(!showUpdate);
const [users, setUsers] = useState([]); const [users, setUsers] = useState([]);
const [roles, setRoles] = useState([]); const [roles, setRoles] = useState([]);
const [deleteLoading, setDeleteLoading] = useState(false);
useEffect(() => { useEffect(() => {
API.listUsers() API.listUsers()
@ -68,23 +69,22 @@ const Users = () => {
<UserTable <UserTable
data={users} data={users}
updateItem={(item) => { updateItem={(item) => {
console.log(item);
setItemToUpdate(item); setItemToUpdate(item);
toggleModalUpdate(); toggleModalUpdate();
}} }}
onDelete={(id) => setUsers(users.filter((item) => item.id != id))} deleteUser={(id) => setUsers(users.filter((item) => item.id != id))}
updateHandler={toggleModalUpdate} updateHandler={toggleModalUpdate}
/> />
<ModalUserEditor <ModalUserEditor
opened={showCreate} opened={showCreate}
handler={(item) => updateUser(item)} handler={(item) => addUser(item)}
handlerClose={toggleModalCreate} handlerClose={toggleModalCreate}
APICall={API.createUser} APICall={API.createUser}
name="Create" name="Create"
/> />
<ModalUserEditor <ModalUserEditor
opened={showUpdate} opened={showUpdate}
handler={(item) => addUser(item)} handler={(item) => updateUser(item)}
handlerClose={toggleModalUpdate} handlerClose={toggleModalUpdate}
APICall={API.updateUser} APICall={API.updateUser}
item={itemToUpdate} item={itemToUpdate}

View File

@ -1,22 +1,57 @@
import { Button, Center, Table, Paper, ScrollArea, Group } from '@mantine/core'; import { Button, Center, Table, Paper, ScrollArea, Group, Text } from '@mantine/core';
import { useNavigate } from 'react-router-dom'; import { useState } from 'react';
import setNotification from '../errors/error-notification';
import API from '../../services/api';
import { modals } from '@mantine/modals';
const UserTable = (props) => { const OpenConfirmationModal = (action) =>
const rows = props.data.map((user) => ( modals.openConfirmModal({
<tr key={user.id}> title: 'Please confirm your action',
children: <Text size="sm">Are you sure you want to delete this user ?</Text>,
labels: { confirm: 'Confirm', cancel: 'Cancel' },
onCancel: () => {},
onConfirm: () => action(),
});
const Line = ({ user, ...props }) => {
const [isLoading, setIsLoading] = useState(false);
const deleteUser = () => {
setIsLoading(true);
API.deleteUser(user.id)
.then((res) => {
setIsLoading(false);
if (res.status === 200) {
props.deleteUser(user.id);
} else {
setNotification(true, res);
}
})
.catch((err) => {
setNotification(true, err);
setIsLoading(false);
});
};
return (
<tr>
<td>{user.login}</td> <td>{user.login}</td>
<td> <td>
<Group> <Group position="right">
<Button onClick={() => 1} color="green"> <Button onClick={() => 1} color="green">
View View
</Button> </Button>
<Button onClick={() => props.updateItem(user)}>Update</Button> <Button onClick={() => props.updateItem(user)}>Update</Button>
<Button onClick={() => 1} color="red"> <Button onClick={() => OpenConfirmationModal(deleteUser)} color="red" loading={isLoading}>
Delete Delete
</Button> </Button>
</Group> </Group>
</td> </td>
</tr> </tr>
);
};
const UserTable = (props) => {
const rows = props.data.map((user) => (
<Line key={user.id} user={user} deleteUser={props.deleteUser} updateItem={props.updateItem} />
)); ));
return ( return (
@ -25,8 +60,8 @@ const UserTable = (props) => {
<Table> <Table>
<thead> <thead>
<tr> <tr>
<th>Name</th> <th>Login</th>
<th>Actions</th> <th position="right">Actions</th>
</tr> </tr>
</thead> </thead>
<tbody>{rows}</tbody> <tbody>{rows}</tbody>

View File

@ -61,6 +61,9 @@ const API = {
createUser(data) { createUser(data) {
return caller().post('/users', data); return caller().post('/users', data);
}, },
deleteUser(userId) {
return caller().delete(`/users/${userId}`);
},
}; };
export default API; export default API;