2022-12-27 01:57:59 +01:00
|
|
|
import { useQuery } from '@tanstack/react-query';
|
2023-01-10 08:59:26 +01:00
|
|
|
import UserDelete from 'components/pages/settings/users/UserDelete';
|
|
|
|
import UserEditForm from 'components/pages/settings/users/UserEditForm';
|
|
|
|
import UserPasswordForm from 'components/pages/settings/users/UserPasswordForm';
|
2022-12-27 01:57:59 +01:00
|
|
|
import Page from 'components/layout/Page';
|
|
|
|
import PageHeader from 'components/layout/PageHeader';
|
2022-12-28 05:20:44 +01:00
|
|
|
import useApi from 'hooks/useApi';
|
2022-12-27 01:57:59 +01:00
|
|
|
import Link from 'next/link';
|
|
|
|
import { useRouter } from 'next/router';
|
|
|
|
import { useEffect, useState } from 'react';
|
2022-12-28 21:37:09 +01:00
|
|
|
import { Breadcrumbs, Item, Icon, Tabs, useToast, Modal, Button } from 'react-basics';
|
|
|
|
import Pen from 'assets/pen.svg';
|
2022-12-27 01:57:59 +01:00
|
|
|
|
|
|
|
export default function UserSettings({ userId }) {
|
2022-12-28 21:37:09 +01:00
|
|
|
const [edit, setEdit] = useState(false);
|
2022-12-27 01:57:59 +01:00
|
|
|
const [values, setValues] = useState(null);
|
|
|
|
const [tab, setTab] = useState('general');
|
2022-12-29 00:43:22 +01:00
|
|
|
const { get } = useApi();
|
2022-12-27 01:57:59 +01:00
|
|
|
const { toast, showToast } = useToast();
|
|
|
|
const router = useRouter();
|
|
|
|
const { data, isLoading } = useQuery(
|
|
|
|
['user', userId],
|
|
|
|
() => {
|
|
|
|
if (userId) {
|
|
|
|
return get(`/users/${userId}`);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
{ cacheTime: 0 },
|
|
|
|
);
|
|
|
|
|
|
|
|
const handleSave = data => {
|
|
|
|
showToast({ message: 'Saved successfully.', variant: 'success' });
|
|
|
|
if (data) {
|
|
|
|
setValues(state => ({ ...state, ...data }));
|
|
|
|
}
|
2022-12-28 21:37:09 +01:00
|
|
|
|
|
|
|
if (edit) {
|
|
|
|
setEdit(false);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
const handleAdd = () => {
|
|
|
|
setEdit(true);
|
|
|
|
};
|
|
|
|
|
|
|
|
const handleClose = () => {
|
|
|
|
setEdit(false);
|
2022-12-27 01:57:59 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
const handleDelete = async () => {
|
|
|
|
showToast({ message: 'Deleted successfully.', variant: 'danger' });
|
|
|
|
await router.push('/users');
|
|
|
|
};
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
if (data) {
|
|
|
|
setValues(data);
|
|
|
|
}
|
|
|
|
}, [data]);
|
|
|
|
|
|
|
|
return (
|
|
|
|
<Page loading={isLoading || !values}>
|
|
|
|
{toast}
|
|
|
|
<PageHeader>
|
|
|
|
<Breadcrumbs>
|
|
|
|
<Item>
|
|
|
|
<Link href="/users">Users</Link>
|
|
|
|
</Item>
|
|
|
|
<Item>{values?.username}</Item>
|
|
|
|
</Breadcrumbs>
|
2022-12-28 21:37:09 +01:00
|
|
|
<Button onClick={handleAdd}>
|
|
|
|
<Icon>
|
|
|
|
<Pen />
|
|
|
|
</Icon>
|
|
|
|
Change Password
|
|
|
|
</Button>
|
2022-12-27 01:57:59 +01:00
|
|
|
</PageHeader>
|
|
|
|
<Tabs selectedKey={tab} onSelect={setTab} style={{ marginBottom: 30, fontSize: 14 }}>
|
|
|
|
<Item key="general">General</Item>
|
|
|
|
<Item key="delete">Danger Zone</Item>
|
|
|
|
</Tabs>
|
|
|
|
{tab === 'general' && <UserEditForm userId={userId} data={values} onSave={handleSave} />}
|
|
|
|
{tab === 'delete' && <UserDelete userId={userId} onSave={handleDelete} />}
|
2022-12-28 21:37:09 +01:00
|
|
|
{edit && (
|
|
|
|
<Modal title="Add website" onClose={handleClose}>
|
|
|
|
{close => (
|
|
|
|
<UserPasswordForm userId={userId} data={values} onSave={handleSave} onClose={close} />
|
|
|
|
)}
|
|
|
|
</Modal>
|
|
|
|
)}
|
2022-12-27 01:57:59 +01:00
|
|
|
</Page>
|
|
|
|
);
|
|
|
|
}
|