2020-08-31 00:29:31 +02:00
|
|
|
import React, { useState } from 'react';
|
2020-09-11 08:55:29 +02:00
|
|
|
import { FormattedMessage } from 'react-intl';
|
2020-09-19 04:53:50 +02:00
|
|
|
import Link from 'next/link';
|
2020-08-18 07:47:58 +02:00
|
|
|
import classNames from 'classnames';
|
2020-08-09 11:03:37 +02:00
|
|
|
import PageHeader from 'components/layout/PageHeader';
|
|
|
|
import Button from 'components/common/Button';
|
|
|
|
import Icon from 'components/common/Icon';
|
|
|
|
import Table from 'components/common/Table';
|
|
|
|
import Modal from 'components/common/Modal';
|
2020-09-11 08:55:29 +02:00
|
|
|
import Toast from 'components/common/Toast';
|
2022-11-01 07:42:37 +01:00
|
|
|
import UserEditForm from 'components/forms/UserEditForm';
|
2020-08-23 04:05:07 +02:00
|
|
|
import ButtonLayout from 'components/layout/ButtonLayout';
|
2020-08-31 00:29:31 +02:00
|
|
|
import DeleteForm from 'components/forms/DeleteForm';
|
|
|
|
import useFetch from 'hooks/useFetch';
|
2020-08-09 08:48:43 +02:00
|
|
|
import Pen from 'assets/pen.svg';
|
|
|
|
import Plus from 'assets/plus.svg';
|
|
|
|
import Trash from 'assets/trash.svg';
|
2020-08-09 11:03:37 +02:00
|
|
|
import Check from 'assets/check.svg';
|
2020-09-19 04:53:50 +02:00
|
|
|
import LinkIcon from 'assets/external-link.svg';
|
2022-11-01 07:42:37 +01:00
|
|
|
import styles from './UserSettings.module.css';
|
2020-08-09 08:48:43 +02:00
|
|
|
|
2022-11-01 07:42:37 +01:00
|
|
|
export default function UserSettings() {
|
|
|
|
const [addUser, setAddUser] = useState();
|
|
|
|
const [editUser, setEditUser] = useState();
|
|
|
|
const [deleteUser, setDeleteUser] = useState();
|
2020-08-09 08:48:43 +02:00
|
|
|
const [saved, setSaved] = useState(0);
|
2020-09-01 00:57:59 +02:00
|
|
|
const [message, setMessage] = useState();
|
2022-11-01 07:42:37 +01:00
|
|
|
const { data } = useFetch(`/users`, {}, [saved]);
|
2020-08-09 08:48:43 +02:00
|
|
|
|
2022-10-10 22:42:18 +02:00
|
|
|
const Checkmark = ({ isAdmin }) => (isAdmin ? <Icon icon={<Check />} size="medium" /> : null);
|
2020-08-12 07:24:41 +02:00
|
|
|
|
2022-10-26 00:48:49 +02:00
|
|
|
const DashboardLink = row => {
|
|
|
|
return (
|
2022-11-01 07:42:37 +01:00
|
|
|
<Link href={`/dashboard/${row.id}/${row.username}`}>
|
2022-10-26 00:48:49 +02:00
|
|
|
<a>
|
|
|
|
<Icon icon={<LinkIcon />} />
|
|
|
|
</a>
|
|
|
|
</Link>
|
|
|
|
);
|
|
|
|
};
|
2020-09-19 04:53:50 +02:00
|
|
|
|
2022-04-04 07:25:32 +02:00
|
|
|
const Buttons = row => (
|
|
|
|
<ButtonLayout align="right">
|
2022-11-01 07:42:37 +01:00
|
|
|
<Button icon={<Pen />} size="small" onClick={() => setEditUser(row)}>
|
2022-04-04 07:25:32 +02:00
|
|
|
<FormattedMessage id="label.edit" defaultMessage="Edit" />
|
|
|
|
</Button>
|
2022-10-10 22:42:18 +02:00
|
|
|
{!row.isAdmin && (
|
2022-11-01 07:42:37 +01:00
|
|
|
<Button icon={<Trash />} size="small" onClick={() => setDeleteUser(row)}>
|
2020-10-13 07:53:59 +02:00
|
|
|
<FormattedMessage id="label.delete" defaultMessage="Delete" />
|
2020-08-12 07:24:41 +02:00
|
|
|
</Button>
|
2022-04-04 07:25:32 +02:00
|
|
|
)}
|
|
|
|
</ButtonLayout>
|
|
|
|
);
|
2020-08-12 07:24:41 +02:00
|
|
|
|
2020-08-09 08:48:43 +02:00
|
|
|
const columns = [
|
2020-09-06 02:27:01 +02:00
|
|
|
{
|
|
|
|
key: 'username',
|
|
|
|
label: <FormattedMessage id="label.username" defaultMessage="Username" />,
|
2022-03-02 04:28:44 +01:00
|
|
|
className: 'col-12 col-lg-4',
|
2020-09-06 02:27:01 +02:00
|
|
|
},
|
2020-08-09 08:48:43 +02:00
|
|
|
{
|
2022-10-10 22:42:18 +02:00
|
|
|
key: 'isAdmin',
|
2020-09-11 08:55:29 +02:00
|
|
|
label: <FormattedMessage id="label.administrator" defaultMessage="Administrator" />,
|
2022-03-02 04:28:44 +01:00
|
|
|
className: 'col-12 col-lg-3',
|
2020-08-12 07:24:41 +02:00
|
|
|
render: Checkmark,
|
2020-08-09 11:03:37 +02:00
|
|
|
},
|
2020-09-19 04:53:50 +02:00
|
|
|
{
|
|
|
|
key: 'dashboard',
|
|
|
|
label: <FormattedMessage id="label.dashboard" defaultMessage="Dashboard" />,
|
2022-03-02 04:28:44 +01:00
|
|
|
className: 'col-12 col-lg-3',
|
2020-09-19 04:53:50 +02:00
|
|
|
render: DashboardLink,
|
|
|
|
},
|
2020-08-09 11:03:37 +02:00
|
|
|
{
|
2020-09-11 08:55:29 +02:00
|
|
|
key: 'actions',
|
2022-03-02 04:28:44 +01:00
|
|
|
className: classNames(styles.buttons, 'col-12 col-lg-2 pt-2 pt-md-0'),
|
2020-08-12 07:24:41 +02:00
|
|
|
render: Buttons,
|
2020-08-09 08:48:43 +02:00
|
|
|
},
|
|
|
|
];
|
|
|
|
|
|
|
|
function handleSave() {
|
|
|
|
setSaved(state => state + 1);
|
2020-09-06 02:27:01 +02:00
|
|
|
setMessage(<FormattedMessage id="message.save-success" defaultMessage="Saved successfully." />);
|
2020-08-09 08:48:43 +02:00
|
|
|
handleClose();
|
|
|
|
}
|
|
|
|
|
|
|
|
function handleClose() {
|
2022-11-01 07:42:37 +01:00
|
|
|
setEditUser(null);
|
|
|
|
setAddUser(null);
|
|
|
|
setDeleteUser(null);
|
2020-08-09 08:48:43 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!data) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
return (
|
|
|
|
<>
|
|
|
|
<PageHeader>
|
2020-09-09 19:43:21 +02:00
|
|
|
<div>
|
2022-11-01 07:42:37 +01:00
|
|
|
<FormattedMessage id="label.users" defaultMessage="Users" />
|
2020-09-09 19:43:21 +02:00
|
|
|
</div>
|
2022-11-01 07:42:37 +01:00
|
|
|
<Button icon={<Plus />} size="small" onClick={() => setAddUser(true)}>
|
|
|
|
<FormattedMessage id="label.add-user" defaultMessage="Add user" />
|
2020-08-09 08:48:43 +02:00
|
|
|
</Button>
|
|
|
|
</PageHeader>
|
|
|
|
<Table columns={columns} rows={data} />
|
2022-11-01 07:42:37 +01:00
|
|
|
{editUser && (
|
|
|
|
<Modal title={<FormattedMessage id="label.edit-user" defaultMessage="Edit user" />}>
|
|
|
|
<UserEditForm
|
|
|
|
values={{ ...editUser, password: '' }}
|
2020-08-09 11:03:37 +02:00
|
|
|
onSave={handleSave}
|
|
|
|
onClose={handleClose}
|
|
|
|
/>
|
2020-08-09 08:48:43 +02:00
|
|
|
</Modal>
|
|
|
|
)}
|
2022-11-01 07:42:37 +01:00
|
|
|
{addUser && (
|
|
|
|
<Modal title={<FormattedMessage id="label.add-user" defaultMessage="Add user" />}>
|
|
|
|
<UserEditForm onSave={handleSave} onClose={handleClose} />
|
2020-08-09 08:48:43 +02:00
|
|
|
</Modal>
|
|
|
|
)}
|
2022-11-01 07:42:37 +01:00
|
|
|
{deleteUser && (
|
|
|
|
<Modal title={<FormattedMessage id="label.delete-user" defaultMessage="Delete user" />}>
|
2020-08-09 11:03:37 +02:00
|
|
|
<DeleteForm
|
2022-11-01 07:42:37 +01:00
|
|
|
values={{ type: 'users', id: deleteUser.id, name: deleteUser.username }}
|
2020-08-09 11:03:37 +02:00
|
|
|
onSave={handleSave}
|
|
|
|
onClose={handleClose}
|
|
|
|
/>
|
|
|
|
</Modal>
|
|
|
|
)}
|
2020-09-01 00:57:59 +02:00
|
|
|
{message && <Toast message={message} onClose={() => setMessage(null)} />}
|
2020-08-09 08:48:43 +02:00
|
|
|
</>
|
|
|
|
);
|
|
|
|
}
|