diff --git a/src/app.js b/src/app.js index 67893aa..0220155 100644 --- a/src/app.js +++ b/src/app.js @@ -2,22 +2,25 @@ import Layout from './components/layout'; import { MantineProvider, ColorSchemeProvider } from '@mantine/core'; import { AuthProvider } from './tools/auth-provider'; 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 [ colorScheme, toggleColorScheme ] = useColorSchemeToggle(); + const [colorScheme, toggleColorScheme] = useColorSchemeToggle(); return ( <> - - - - + + + + + + > ); }; - +// todo theme for modal provider export default App; diff --git a/src/components/confirmation-modal.jsx b/src/components/confirmation-modal.jsx new file mode 100644 index 0000000..92b0ebc --- /dev/null +++ b/src/components/confirmation-modal.jsx @@ -0,0 +1,37 @@ +import { Modal, Text, Group, Button } from '@mantine/core'; + +const ConfirmationModal = ({ opened, handler, text, action }) => { + const confirm = () => { + action(); + handler(); + }; + + return ( + + + + + + + Confirmation + + + + + + {text ?? 'Are you sure you want to do this ?'} + + + Cancel + + + Confirm + + + + + + ); +}; + +export default ConfirmationModal; diff --git a/src/pages/users/index.jsx b/src/pages/users/index.jsx index 3096d71..479d727 100644 --- a/src/pages/users/index.jsx +++ b/src/pages/users/index.jsx @@ -16,6 +16,7 @@ const Users = () => { const toggleModalUpdate = () => setShowUpdate(!showUpdate); const [users, setUsers] = useState([]); const [roles, setRoles] = useState([]); + const [deleteLoading, setDeleteLoading] = useState(false); useEffect(() => { API.listUsers() @@ -68,23 +69,22 @@ const Users = () => { { - console.log(item); setItemToUpdate(item); toggleModalUpdate(); }} - onDelete={(id) => setUsers(users.filter((item) => item.id != id))} + deleteUser={(id) => setUsers(users.filter((item) => item.id != id))} updateHandler={toggleModalUpdate} /> updateUser(item)} + handler={(item) => addUser(item)} handlerClose={toggleModalCreate} APICall={API.createUser} name="Create" /> addUser(item)} + handler={(item) => updateUser(item)} handlerClose={toggleModalUpdate} APICall={API.updateUser} item={itemToUpdate} diff --git a/src/pages/users/users-table.jsx b/src/pages/users/users-table.jsx index c633503..3cd7335 100644 --- a/src/pages/users/users-table.jsx +++ b/src/pages/users/users-table.jsx @@ -1,22 +1,57 @@ -import { Button, Center, Table, Paper, ScrollArea, Group } from '@mantine/core'; -import { useNavigate } from 'react-router-dom'; +import { Button, Center, Table, Paper, ScrollArea, Group, Text } from '@mantine/core'; +import { useState } from 'react'; +import setNotification from '../errors/error-notification'; +import API from '../../services/api'; +import { modals } from '@mantine/modals'; -const UserTable = (props) => { - const rows = props.data.map((user) => ( - +const OpenConfirmationModal = (action) => + modals.openConfirmModal({ + title: 'Please confirm your action', + children: Are you sure you want to delete this user ?, + 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 ( + {user.login} - + 1} color="green"> View props.updateItem(user)}>Update - 1} color="red"> + OpenConfirmationModal(deleteUser)} color="red" loading={isLoading}> Delete + ); +}; + +const UserTable = (props) => { + const rows = props.data.map((user) => ( + )); return ( @@ -25,8 +60,8 @@ const UserTable = (props) => { - Name - Actions + Login + Actions {rows} diff --git a/src/services/api.js b/src/services/api.js index 927684e..831f331 100644 --- a/src/services/api.js +++ b/src/services/api.js @@ -61,6 +61,9 @@ const API = { createUser(data) { return caller().post('/users', data); }, + deleteUser(userId) { + return caller().delete(`/users/${userId}`); + }, }; export default API;