Converted user and website settings.
This commit is contained in:
1 parent
4c24e54fdd
commit
b5c6194f36
59 files changed
+363
-554
No files matched your search
@@ -1,29 +1,40 @@
|
||||
import { Button, Icon, Text, Modal, Icons, ModalTrigger, useToasts } from 'react-basics';
|
||||
import {
|
||||
Button,
|
||||
Icon,
|
||||
Text,
|
||||
Modal,
|
||||
Icons,
|
||||
DialogTrigger,
|
||||
Dialog,
|
||||
useToast,
|
||||
} from '@umami/react-zen';
|
||||
import { UserAddForm } from './UserAddForm';
|
||||
import { useMessages, useModified } from '@/components/hooks';
|
||||
|
||||
export function UserAddButton({ onSave }: { onSave?: () => void }) {
|
||||
const { formatMessage, labels, messages } = useMessages();
|
||||
const { showToast } = useToasts();
|
||||
const { toast } = useToast();
|
||||
const { touch } = useModified();
|
||||
|
||||
const handleSave = () => {
|
||||
showToast({ message: formatMessage(messages.saved), variant: 'success' });
|
||||
toast(formatMessage(messages.saved));
|
||||
touch('users');
|
||||
onSave?.();
|
||||
};
|
||||
|
||||
return (
|
||||
<ModalTrigger>
|
||||
<DialogTrigger>
|
||||
<Button variant="primary">
|
||||
<Icon>
|
||||
<Icons.Plus />
|
||||
</Icon>
|
||||
<Text>{formatMessage(labels.createUser)}</Text>
|
||||
</Button>
|
||||
<Modal title={formatMessage(labels.createUser)}>
|
||||
{(close: () => void) => <UserAddForm onSave={handleSave} onClose={close} />}
|
||||
<Modal>
|
||||
<Dialog title={formatMessage(labels.createUser)}>
|
||||
{({ close }) => <UserAddForm onSave={handleSave} onClose={close} />}
|
||||
</Dialog>
|
||||
</Modal>
|
||||
</ModalTrigger>
|
||||
</DialogTrigger>
|
||||
);
|
||||
}
|
||||
@@ -1,4 +1,4 @@
|
||||
import { Button, Icon, Icons, Modal, ModalTrigger, Text } from 'react-basics';
|
||||
import { Button, Icon, Icons, Modal, DialogTrigger, Dialog, Text } from '@umami/react-zen';
|
||||
import { useMessages, useLogin } from '@/components/hooks';
|
||||
import { UserDeleteForm } from './UserDeleteForm';
|
||||
|
||||
@@ -15,18 +15,20 @@ export function UserDeleteButton({
|
||||
const { user } = useLogin();
|
||||
|
||||
return (
|
||||
<ModalTrigger disabled={userId === user?.id}>
|
||||
<Button disabled={userId === user?.id} variant="quiet">
|
||||
<Icon>
|
||||
<DialogTrigger>
|
||||
<Button isDisabled={userId === user?.id}>
|
||||
<Icon size="sm">
|
||||
<Icons.Trash />
|
||||
</Icon>
|
||||
<Text>{formatMessage(labels.delete)}</Text>
|
||||
</Button>
|
||||
<Modal title={formatMessage(labels.deleteUser)}>
|
||||
{(close: () => void) => (
|
||||
<UserDeleteForm userId={userId} username={username} onSave={onDelete} onClose={close} />
|
||||
)}
|
||||
<Modal>
|
||||
<Dialog title={formatMessage(labels.deleteUser)}>
|
||||
{({ close }) => (
|
||||
<UserDeleteForm userId={userId} username={username} onSave={onDelete} onClose={close} />
|
||||
)}
|
||||
</Dialog>
|
||||
</Modal>
|
||||
</ModalTrigger>
|
||||
</DialogTrigger>
|
||||
);
|
||||
}
|
||||
@@ -1,3 +1,4 @@
|
||||
import { useToast } from '@umami/react-zen';
|
||||
import { useApi, useMessages, useModified } from '@/components/hooks';
|
||||
import { ConfirmationForm } from '@/components/common/ConfirmationForm';
|
||||
|
||||
@@ -6,11 +7,13 @@ export function UserDeleteForm({ userId, username, onSave, onClose }) {
|
||||
const { del, useMutation } = useApi();
|
||||
const { mutate, error, isPending } = useMutation({ mutationFn: () => del(`/users/${userId}`) });
|
||||
const { touch } = useModified();
|
||||
const { toast } = useToast();
|
||||
|
||||
const handleConfirm = async () => {
|
||||
mutate(null, {
|
||||
onSuccess: async () => {
|
||||
touch('users');
|
||||
toast(formatMessage(messages.successMessage));
|
||||
onSave?.();
|
||||
onClose?.();
|
||||
},
|
||||
@@ -23,6 +26,7 @@ export function UserDeleteForm({ userId, username, onSave, onClose }) {
|
||||
onConfirm={handleConfirm}
|
||||
onClose={onClose}
|
||||
buttonLabel={formatMessage(labels.delete)}
|
||||
buttonVariant="danger"
|
||||
isLoading={isPending}
|
||||
error={error}
|
||||
/>
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
import { DataTable } from '@/components/common/DataTable';
|
||||
import { DataGrid } from '@/components/common/DataGrid';
|
||||
import { useUsers } from '@/components/hooks';
|
||||
import { UsersTable } from './UsersTable';
|
||||
import { ReactNode } from 'react';
|
||||
@@ -13,8 +13,8 @@ export function UsersDataTable({
|
||||
const queryResult = useUsers();
|
||||
|
||||
return (
|
||||
<DataTable queryResult={queryResult} renderEmpty={() => children}>
|
||||
<DataGrid queryResult={queryResult} renderEmpty={() => children}>
|
||||
{({ data }) => <UsersTable data={data} showActions={showActions} />}
|
||||
</DataTable>
|
||||
</DataGrid>
|
||||
);
|
||||
}
|
||||
@@ -1,9 +1,9 @@
|
||||
import { Text, Icon, Icons, GridTable, GridColumn } from 'react-basics';
|
||||
import { Row, Button, Text, Icon, Icons, DataTable, DataColumn } from '@umami/react-zen';
|
||||
import Link from 'next/link';
|
||||
import { formatDistance } from 'date-fns';
|
||||
import { ROLES } from '@/lib/constants';
|
||||
import { useMessages, useLocale } from '@/components/hooks';
|
||||
import { UserDeleteButton } from './UserDeleteButton';
|
||||
import { LinkButton } from '@/components/common/LinkButton';
|
||||
|
||||
export function UsersTable({
|
||||
data = [],
|
||||
@@ -16,44 +16,46 @@ export function UsersTable({
|
||||
const { dateLocale } = useLocale();
|
||||
|
||||
return (
|
||||
<GridTable data={data}>
|
||||
<GridColumn name="username" label={formatMessage(labels.username)} style={{ minWidth: 0 }} />
|
||||
<GridColumn name="role" label={formatMessage(labels.role)} width={'120px'}>
|
||||
{row =>
|
||||
<DataTable data={data}>
|
||||
<DataColumn id="username" label={formatMessage(labels.username)} style={{ minWidth: 0 }} />
|
||||
<DataColumn id="role" label={formatMessage(labels.role)} style={{ maxWidth: 60 }}>
|
||||
{(row: any) =>
|
||||
formatMessage(
|
||||
labels[Object.keys(ROLES).find(key => ROLES[key] === row.role)] || labels.unknown,
|
||||
)
|
||||
}
|
||||
</GridColumn>
|
||||
<GridColumn name="created" label={formatMessage(labels.created)} width={'150px'}>
|
||||
{row =>
|
||||
</DataColumn>
|
||||
<DataColumn id="created" label={formatMessage(labels.created)} style={{ maxWidth: 60 }}>
|
||||
{(row: any) =>
|
||||
formatDistance(new Date(row.createdAt), new Date(), {
|
||||
addSuffix: true,
|
||||
locale: dateLocale,
|
||||
})
|
||||
}
|
||||
</GridColumn>
|
||||
<GridColumn name="websites" label={formatMessage(labels.websites)} width={'120px'}>
|
||||
{row => row._count.website}
|
||||
</GridColumn>
|
||||
</DataColumn>
|
||||
<DataColumn id="websites" label={formatMessage(labels.websites)} style={{ maxWidth: 60 }}>
|
||||
{(row: any) => row._count.websiteUser}
|
||||
</DataColumn>
|
||||
{showActions && (
|
||||
<GridColumn name="action" label=" " alignment="end">
|
||||
{row => {
|
||||
<DataColumn id="action" align="end">
|
||||
{(row: any) => {
|
||||
const { id, username } = row;
|
||||
return (
|
||||
<>
|
||||
<Row gap="3">
|
||||
<UserDeleteButton userId={id} username={username} />
|
||||
<LinkButton href={`/settings/users/${id}`}>
|
||||
<Icon>
|
||||
<Icons.Edit />
|
||||
</Icon>
|
||||
<Text>{formatMessage(labels.edit)}</Text>
|
||||
</LinkButton>
|
||||
</>
|
||||
<Button asChild>
|
||||
<Link href={`/settings/users/${id}`}>
|
||||
<Icon>
|
||||
<Icons.Edit />
|
||||
</Icon>
|
||||
<Text>{formatMessage(labels.edit)}</Text>
|
||||
</Link>
|
||||
</Button>
|
||||
</Row>
|
||||
);
|
||||
}}
|
||||
</GridColumn>
|
||||
</DataColumn>
|
||||
)}
|
||||
</GridTable>
|
||||
</DataTable>
|
||||
);
|
||||
}
|
||||
@@ -1,22 +1,27 @@
|
||||
import {
|
||||
Dropdown,
|
||||
Item,
|
||||
Select,
|
||||
ListItem,
|
||||
Form,
|
||||
FormRow,
|
||||
FormField,
|
||||
FormButtons,
|
||||
FormInput,
|
||||
TextField,
|
||||
SubmitButton,
|
||||
FormSubmitButton,
|
||||
PasswordField,
|
||||
} from 'react-basics';
|
||||
import { useApi, useLogin, useMessages } from '@/components/hooks';
|
||||
useToast,
|
||||
} from '@umami/react-zen';
|
||||
import { useApi, useLogin, useMessages, useModified } from '@/components/hooks';
|
||||
import { ROLES } from '@/lib/constants';
|
||||
import { useContext, useRef } from 'react';
|
||||
import { useContext } from 'react';
|
||||
import { UserContext } from './UserProvider';
|
||||
|
||||
export function UserEditForm({ userId, onSave }: { userId: string; onSave?: () => void }) {
|
||||
const { formatMessage, labels, messages } = useMessages();
|
||||
const { formatMessage, labels, messages, getMessage } = useMessages();
|
||||
const { post, useMutation } = useApi();
|
||||
const user = useContext(UserContext);
|
||||
const { user: login } = useLogin();
|
||||
const { toast } = useToast();
|
||||
const { touch } = useModified();
|
||||
|
||||
const { mutate, error } = useMutation({
|
||||
mutationFn: ({
|
||||
username,
|
||||
@@ -28,61 +33,47 @@ export function UserEditForm({ userId, onSave }: { userId: string; onSave?: () =
|
||||
role: string;
|
||||
}) => post(`/users/${userId}`, { username, password, role }),
|
||||
});
|
||||
const ref = useRef(null);
|
||||
const user = useContext(UserContext);
|
||||
const { user: login } = useLogin();
|
||||
|
||||
const handleSubmit = async (data: any) => {
|
||||
mutate(data, {
|
||||
onSuccess: async () => {
|
||||
ref.current.reset(data);
|
||||
toast(formatMessage(messages.saved));
|
||||
touch(`user:${user.id}`);
|
||||
onSave?.();
|
||||
},
|
||||
});
|
||||
};
|
||||
|
||||
const renderValue = (value: string) => {
|
||||
if (value === ROLES.user) {
|
||||
return formatMessage(labels.user);
|
||||
}
|
||||
if (value === ROLES.admin) {
|
||||
return formatMessage(labels.admin);
|
||||
}
|
||||
if (value === ROLES.viewOnly) {
|
||||
return formatMessage(labels.viewOnly);
|
||||
}
|
||||
};
|
||||
|
||||
return (
|
||||
<Form ref={ref} onSubmit={handleSubmit} error={error} values={user} style={{ width: 300 }}>
|
||||
<FormRow label={formatMessage(labels.username)}>
|
||||
<FormInput name="username">
|
||||
<TextField />
|
||||
</FormInput>
|
||||
</FormRow>
|
||||
<FormRow label={formatMessage(labels.password)}>
|
||||
<FormInput
|
||||
name="password"
|
||||
rules={{
|
||||
minLength: { value: 8, message: formatMessage(messages.minPasswordLength, { n: 8 }) },
|
||||
}}
|
||||
>
|
||||
<PasswordField autoComplete="new-password" />
|
||||
</FormInput>
|
||||
</FormRow>
|
||||
<Form onSubmit={handleSubmit} error={getMessage(error)} values={user} style={{ width: 300 }}>
|
||||
<FormField name="username" label={formatMessage(labels.username)}>
|
||||
<TextField />
|
||||
</FormField>
|
||||
<FormField
|
||||
name="password"
|
||||
label={formatMessage(labels.password)}
|
||||
rules={{
|
||||
minLength: { value: 8, message: formatMessage(messages.minPasswordLength, { n: 8 }) },
|
||||
}}
|
||||
>
|
||||
<PasswordField autoComplete="new-password" />
|
||||
</FormField>
|
||||
|
||||
{user.id !== login.id && (
|
||||
<FormRow label={formatMessage(labels.role)}>
|
||||
<FormInput name="role" rules={{ required: formatMessage(labels.required) }}>
|
||||
<Dropdown renderValue={renderValue}>
|
||||
<Item key={ROLES.viewOnly}>{formatMessage(labels.viewOnly)}</Item>
|
||||
<Item key={ROLES.user}>{formatMessage(labels.user)}</Item>
|
||||
<Item key={ROLES.admin}>{formatMessage(labels.admin)}</Item>
|
||||
</Dropdown>
|
||||
</FormInput>
|
||||
</FormRow>
|
||||
<FormField
|
||||
name="role"
|
||||
label={formatMessage(labels.role)}
|
||||
rules={{ required: formatMessage(labels.required) }}
|
||||
>
|
||||
<Select defaultSelectedKey={user.role}>
|
||||
<ListItem id={ROLES.viewOnly}>{formatMessage(labels.viewOnly)}</ListItem>
|
||||
<ListItem id={ROLES.user}>{formatMessage(labels.user)}</ListItem>
|
||||
<ListItem id={ROLES.admin}>{formatMessage(labels.admin)}</ListItem>
|
||||
</Select>
|
||||
</FormField>
|
||||
)}
|
||||
<FormButtons>
|
||||
<SubmitButton variant="primary">{formatMessage(labels.save)}</SubmitButton>
|
||||
<FormSubmitButton variant="primary">{formatMessage(labels.save)}</FormSubmitButton>
|
||||
</FormButtons>
|
||||
</Form>
|
||||
);
|
||||
|
||||
@@ -2,7 +2,7 @@
|
||||
import { UserSettings } from './UserSettings';
|
||||
import { UserProvider } from './UserProvider';
|
||||
|
||||
export default function ({ userId }: { userId: string }) {
|
||||
export function UserPage({ userId }: { userId: string }) {
|
||||
return (
|
||||
<UserProvider userId={userId}>
|
||||
<UserSettings userId={userId} />
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
import { createContext, ReactNode, useEffect } from 'react';
|
||||
import { Loading } from '@umami/react-zen';
|
||||
import { useModified, useUser } from '@/components/hooks';
|
||||
import { Loading } from 'react-basics';
|
||||
|
||||
export const UserContext = createContext(null);
|
||||
|
||||
@@ -18,5 +18,5 @@ export function UserProvider({ userId, children }: { userId: string; children: R
|
||||
return <Loading position="page" />;
|
||||
}
|
||||
|
||||
return <UserContext.Provider value={user}>{children}</UserContext.Provider>;
|
||||
return <UserContext.Provider value={{ ...user, modified }}>{children}</UserContext.Provider>;
|
||||
}
|
||||
@@ -1,46 +1,31 @@
|
||||
import { Key, useContext, useState } from 'react';
|
||||
import { Item, Tabs, useToasts } from 'react-basics';
|
||||
import { useContext } from 'react';
|
||||
import { Tabs, Tab, TabList, TabPanel } from '@umami/react-zen';
|
||||
import { Icons } from '@/components/icons';
|
||||
import { UserEditForm } from './UserEditForm';
|
||||
import { PageHeader } from '@/components/layout/PageHeader';
|
||||
import { useMessages } from '@/components/hooks';
|
||||
import { UserWebsites } from './UserWebsites';
|
||||
import { UserContext } from './UserProvider';
|
||||
import { Breadcrumb } from '@/components/common/Breadcrumb';
|
||||
|
||||
export function UserSettings({ userId }: { userId: string }) {
|
||||
const { formatMessage, labels, messages } = useMessages();
|
||||
const [tab, setTab] = useState<Key>('details');
|
||||
const { formatMessage, labels } = useMessages();
|
||||
const user = useContext(UserContext);
|
||||
const { showToast } = useToasts();
|
||||
|
||||
const handleSave = () => {
|
||||
showToast({ message: formatMessage(messages.saved), variant: 'success' });
|
||||
};
|
||||
|
||||
const breadcrumb = (
|
||||
<Breadcrumb
|
||||
data={[
|
||||
{
|
||||
label: formatMessage(labels.users),
|
||||
url: '/settings/users',
|
||||
},
|
||||
{
|
||||
label: user.username,
|
||||
},
|
||||
]}
|
||||
/>
|
||||
);
|
||||
|
||||
return (
|
||||
<>
|
||||
<PageHeader title={user?.username} icon={<Icons.User />} breadcrumb={breadcrumb} />
|
||||
<Tabs selectedKey={tab} onSelect={setTab} style={{ marginBottom: 30, fontSize: 14 }}>
|
||||
<Item key="details">{formatMessage(labels.details)}</Item>
|
||||
<Item key="websites">{formatMessage(labels.websites)}</Item>
|
||||
<PageHeader title={user?.username} icon={<Icons.User />} />
|
||||
<Tabs>
|
||||
<TabList>
|
||||
<Tab id="details">{formatMessage(labels.details)}</Tab>
|
||||
<Tab id="websites">{formatMessage(labels.websites)}</Tab>
|
||||
</TabList>
|
||||
<TabPanel id="details">
|
||||
<UserEditForm userId={userId} />
|
||||
</TabPanel>
|
||||
<TabPanel id="websites">
|
||||
<UserWebsites userId={userId} />
|
||||
</TabPanel>
|
||||
</Tabs>
|
||||
{tab === 'details' && <UserEditForm userId={userId} onSave={handleSave} />}
|
||||
{tab === 'websites' && <UserWebsites userId={userId} />}
|
||||
</>
|
||||
);
|
||||
}
|
||||
@@ -1,15 +1,15 @@
|
||||
import { WebsitesTable } from '@/app/(main)/settings/websites/WebsitesTable';
|
||||
import { DataTable } from '@/components/common/DataTable';
|
||||
import { DataGrid } from '@/components/common/DataGrid';
|
||||
import { useWebsites } from '@/components/hooks';
|
||||
|
||||
export function UserWebsites({ userId }) {
|
||||
const queryResult = useWebsites({ userId });
|
||||
|
||||
return (
|
||||
<DataTable queryResult={queryResult}>
|
||||
<DataGrid queryResult={queryResult}>
|
||||
{({ data }) => (
|
||||
<WebsitesTable data={data} showActions={true} allowEdit={true} allowView={true} />
|
||||
)}
|
||||
</DataTable>
|
||||
</DataGrid>
|
||||
);
|
||||
}
|
||||
@@ -1,7 +1,7 @@
|
||||
import { UserPage } from './UserPage';
|
||||
import { Metadata } from 'next';
|
||||
|
||||
export default async function ({ params }: { params: { userId: string } }) {
|
||||
export default async function ({ params }: { params: Promise<{ userId: string }> }) {
|
||||
const { userId } = await params;
|
||||
|
||||
return <UserPage userId={userId} />;
|
||||
|
||||
Reference in new issue
Block a user