umami/components/settings/AccountSettings.js

107 lines
3.2 KiB
JavaScript
Raw Normal View History

import React, { useState } from 'react';
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';
import AccountEditForm from 'components/forms/AccountEditForm';
import ButtonLayout from 'components/layout/ButtonLayout';
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-08-09 08:48:43 +02:00
import styles from './AccountSettings.module.css';
2020-09-01 00:57:59 +02:00
import Toast from '../common/Toast';
2020-08-09 08:48:43 +02:00
export default function AccountSettings() {
const [addAccount, setAddAccount] = useState();
const [editAccount, setEditAccount] = useState();
const [deleteAccount, setDeleteAccount] = useState();
const [saved, setSaved] = useState(0);
2020-09-01 00:57:59 +02:00
const [message, setMessage] = useState();
const { data } = useFetch(`/api/accounts`, {}, { update: [saved] });
2020-08-09 08:48:43 +02:00
2020-08-12 07:24:41 +02:00
const Checkmark = ({ is_admin }) => (is_admin ? <Icon icon={<Check />} size="medium" /> : null);
const Buttons = row =>
row.username !== 'admin' ? (
<ButtonLayout>
2020-08-12 07:24:41 +02:00
<Button icon={<Pen />} size="small" onClick={() => setEditAccount(row)}>
<div>Edit</div>
</Button>
<Button icon={<Trash />} size="small" onClick={() => setDeleteAccount(row)}>
<div>Delete</div>
</Button>
</ButtonLayout>
2020-08-12 07:24:41 +02:00
) : null;
2020-08-09 08:48:43 +02:00
const columns = [
2020-08-18 07:47:58 +02:00
{ key: 'username', label: 'Username', className: 'col-6 col-md-4' },
2020-08-09 08:48:43 +02:00
{
2020-08-09 11:03:37 +02:00
key: 'is_admin',
label: 'Administrator',
2020-08-18 07:47:58 +02:00
className: 'col-6 col-md-4',
2020-08-12 07:24:41 +02:00
render: Checkmark,
2020-08-09 11:03:37 +02:00
},
{
2020-08-18 09:51:32 +02:00
className: classNames(styles.buttons, 'col-12 col-md-4 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-01 00:57:59 +02:00
setMessage('Saved successfully.');
2020-08-09 08:48:43 +02:00
handleClose();
}
function handleClose() {
setEditAccount(null);
setAddAccount(null);
setDeleteAccount(null);
}
if (!data) {
return null;
}
return (
<>
<PageHeader>
<div>Accounts</div>
<Button icon={<Plus />} size="small" onClick={() => setAddAccount(true)}>
<div>Add account</div>
</Button>
</PageHeader>
<Table columns={columns} rows={data} />
{editAccount && (
<Modal title="Edit account">
2020-08-09 11:03:37 +02:00
<AccountEditForm
values={{ ...editAccount, password: '' }}
onSave={handleSave}
onClose={handleClose}
/>
2020-08-09 08:48:43 +02:00
</Modal>
)}
{addAccount && (
<Modal title="Add account">
<AccountEditForm onSave={handleSave} onClose={handleClose} />
</Modal>
)}
2020-08-09 11:03:37 +02:00
{deleteAccount && (
<Modal title="Delete account">
<DeleteForm
values={{ type: 'account', id: deleteAccount.user_id, name: deleteAccount.username }}
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
</>
);
}