umami/components/pages/settings/users/UserSettings.js

68 lines
2.0 KiB
JavaScript
Raw Normal View History

2023-01-21 02:12:53 +01:00
import { useEffect, useState } from 'react';
2023-06-13 20:59:12 +02:00
import { Breadcrumbs, Item, Tabs, useToasts } from 'react-basics';
2023-01-21 02:12:53 +01:00
import Link from 'next/link';
import UserEditForm from 'components/pages/settings/users//UserEditForm';
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';
2023-01-25 16:42:46 +01:00
import UserWebsites from './UserWebsites';
2023-03-22 22:05:55 +01:00
import useMessages from 'hooks/useMessages';
2023-04-21 17:00:42 +02:00
export function UserSettings({ userId }) {
2023-03-22 22:05:55 +01:00
const { formatMessage, labels, messages } = useMessages();
const [edit, setEdit] = useState(false);
const [values, setValues] = useState(null);
2023-01-21 02:12:53 +01:00
const [tab, setTab] = useState('details');
const { get, useQuery } = useApi();
2023-06-13 20:59:12 +02:00
const { showToast } = useToasts();
const { data, isLoading } = useQuery(
['user', userId],
() => {
if (userId) {
return get(`/users/${userId}`);
}
},
{ cacheTime: 0 },
);
const handleSave = data => {
2023-01-24 00:32:35 +01:00
showToast({ message: formatMessage(messages.saved), variant: 'success' });
if (data) {
setValues(state => ({ ...state, ...data }));
}
if (edit) {
setEdit(false);
}
};
useEffect(() => {
if (data) {
setValues(data);
}
}, [data]);
return (
<Page loading={isLoading || !values}>
2023-02-02 03:39:54 +01:00
<PageHeader
title={
<Breadcrumbs>
<Item>
<Link href="/settings/users">{formatMessage(labels.users)}</Link>
</Item>
<Item>{values?.username}</Item>
</Breadcrumbs>
}
/>
<Tabs selectedKey={tab} onSelect={setTab} style={{ marginBottom: 30, fontSize: 14 }}>
2023-01-25 16:42:46 +01:00
<Item key="details">{formatMessage(labels.details)}</Item>
<Item key="websites">{formatMessage(labels.websites)}</Item>
</Tabs>
2023-01-21 02:12:53 +01:00
{tab === 'details' && <UserEditForm userId={userId} data={values} onSave={handleSave} />}
2023-01-25 16:42:46 +01:00
{tab === 'websites' && <UserWebsites userId={userId} />}
</Page>
);
}
2023-04-21 17:00:42 +02:00
export default UserSettings;