ui(admin): add "working" React admin ui
This commit is contained in:
parent
d1c7494933
commit
5402584508
199 changed files with 13387 additions and 151 deletions
|
@ -0,0 +1,49 @@
|
|||
import type { Actions } from 'easy-peasy';
|
||||
import { useStoreActions } from 'easy-peasy';
|
||||
import type { FormikHelpers } from 'formik';
|
||||
import { useNavigate } from 'react-router-dom';
|
||||
import tw from 'twin.macro';
|
||||
|
||||
import type { UpdateUserValues } from '@/api/admin/users';
|
||||
import { createUser } from '@/api/admin/users';
|
||||
import AdminContentBlock from '@/components/admin/AdminContentBlock';
|
||||
import UserForm from '@/components/admin/users/UserForm';
|
||||
import FlashMessageRender from '@/components/FlashMessageRender';
|
||||
import type { ApplicationStore } from '@/state';
|
||||
|
||||
export default () => {
|
||||
const navigate = useNavigate();
|
||||
|
||||
const { clearFlashes, clearAndAddHttpError } = useStoreActions(
|
||||
(actions: Actions<ApplicationStore>) => actions.flashes,
|
||||
);
|
||||
|
||||
const submit = (values: UpdateUserValues, { setSubmitting }: FormikHelpers<UpdateUserValues>) => {
|
||||
clearFlashes('user:create');
|
||||
|
||||
createUser(values)
|
||||
.then(user => navigate(`/admin/users/${user.id}`))
|
||||
.catch(error => {
|
||||
console.error(error);
|
||||
clearAndAddHttpError({ key: 'user:create', error });
|
||||
})
|
||||
.then(() => setSubmitting(false));
|
||||
};
|
||||
|
||||
return (
|
||||
<AdminContentBlock title={'New User'}>
|
||||
<div css={tw`w-full flex flex-row items-center mb-8`}>
|
||||
<div css={tw`flex flex-col flex-shrink`} style={{ minWidth: '0' }}>
|
||||
<h2 css={tw`text-2xl text-neutral-50 font-header font-medium`}>New User</h2>
|
||||
<p css={tw`text-base text-neutral-400 whitespace-nowrap overflow-ellipsis overflow-hidden`}>
|
||||
Add a new user to the panel.
|
||||
</p>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<FlashMessageRender byKey={'user:create'} css={tw`mb-4`} />
|
||||
|
||||
<UserForm title={'Create User'} onSubmit={submit} role={null} />
|
||||
</AdminContentBlock>
|
||||
);
|
||||
};
|
56
resources/scripts/components/admin/users/RoleSelect.tsx
Normal file
56
resources/scripts/components/admin/users/RoleSelect.tsx
Normal file
|
@ -0,0 +1,56 @@
|
|||
import { useFormikContext } from 'formik';
|
||||
import { useState } from 'react';
|
||||
|
||||
import { searchRoles } from '@/api/admin/roles';
|
||||
import SearchableSelect, { Option } from '@/components/elements/SearchableSelect';
|
||||
import type { UserRole } from '@definitions/admin';
|
||||
|
||||
export default ({ selected }: { selected: UserRole | null }) => {
|
||||
const context = useFormikContext();
|
||||
|
||||
const [role, setRole] = useState<UserRole | null>(selected);
|
||||
const [roles, setRoles] = useState<UserRole[] | null>(null);
|
||||
|
||||
const onSearch = (query: string): Promise<void> => {
|
||||
return new Promise((resolve, reject) => {
|
||||
searchRoles({ name: query })
|
||||
.then(roles => {
|
||||
setRoles(roles);
|
||||
return resolve();
|
||||
})
|
||||
.catch(reject);
|
||||
});
|
||||
};
|
||||
|
||||
const onSelect = (role: UserRole | null) => {
|
||||
setRole(role);
|
||||
context.setFieldValue('adminRoleId', role?.id || null);
|
||||
};
|
||||
|
||||
const getSelectedText = (role: UserRole | null): string | undefined => {
|
||||
return role?.name;
|
||||
};
|
||||
|
||||
return (
|
||||
<SearchableSelect
|
||||
id={'adminRoleId'}
|
||||
name={'adminRoleId'}
|
||||
label={'Role'}
|
||||
placeholder={'Select a role...'}
|
||||
items={roles}
|
||||
selected={role}
|
||||
setSelected={setRole}
|
||||
setItems={setRoles}
|
||||
onSearch={onSearch}
|
||||
onSelect={onSelect}
|
||||
getSelectedText={getSelectedText}
|
||||
nullable
|
||||
>
|
||||
{roles?.map(d => (
|
||||
<Option key={d.id} selectId={'adminRoleId'} id={d.id} item={d} active={d.id === role?.id}>
|
||||
{d.name}
|
||||
</Option>
|
||||
))}
|
||||
</SearchableSelect>
|
||||
);
|
||||
};
|
|
@ -0,0 +1,62 @@
|
|||
import type { Actions } from 'easy-peasy';
|
||||
import { useStoreActions } from 'easy-peasy';
|
||||
import type { FormikHelpers } from 'formik';
|
||||
import { useNavigate } from 'react-router-dom';
|
||||
|
||||
import type { UpdateUserValues } from '@/api/admin/users';
|
||||
import { updateUser } from '@/api/admin/users';
|
||||
import UserDeleteButton from '@/components/admin/users/UserDeleteButton';
|
||||
import UserForm from '@/components/admin/users/UserForm';
|
||||
import { Context } from '@/components/admin/users/UserRouter';
|
||||
import type { ApplicationStore } from '@/state';
|
||||
import tw from 'twin.macro';
|
||||
|
||||
const UserAboutContainer = () => {
|
||||
const navigate = useNavigate();
|
||||
|
||||
const { clearFlashes, clearAndAddHttpError } = useStoreActions(
|
||||
(actions: Actions<ApplicationStore>) => actions.flashes,
|
||||
);
|
||||
|
||||
const user = Context.useStoreState(state => state.user);
|
||||
const setUser = Context.useStoreActions(actions => actions.setUser);
|
||||
|
||||
if (user === undefined) {
|
||||
return <></>;
|
||||
}
|
||||
|
||||
const submit = (values: UpdateUserValues, { setSubmitting }: FormikHelpers<UpdateUserValues>) => {
|
||||
clearFlashes('user');
|
||||
|
||||
updateUser(user.id, values)
|
||||
.then(() => setUser({ ...user, ...values }))
|
||||
.catch(error => {
|
||||
console.error(error);
|
||||
clearAndAddHttpError({ key: 'user', error });
|
||||
})
|
||||
.then(() => setSubmitting(false));
|
||||
};
|
||||
|
||||
return (
|
||||
<UserForm
|
||||
title={'Edit User'}
|
||||
initialValues={{
|
||||
externalId: user.externalId,
|
||||
username: user.username,
|
||||
email: user.email,
|
||||
adminRoleId: user.adminRoleId,
|
||||
password: '',
|
||||
rootAdmin: user.isRootAdmin,
|
||||
}}
|
||||
onSubmit={submit}
|
||||
uuid={user.uuid}
|
||||
role={user.relationships.role || null}
|
||||
>
|
||||
<div css={tw`flex`}>
|
||||
<UserDeleteButton userId={user.id} onDeleted={() => navigate('/admin/users')} />
|
||||
</div>
|
||||
</UserForm>
|
||||
);
|
||||
};
|
||||
|
||||
export default UserAboutContainer;
|
|
@ -0,0 +1,73 @@
|
|||
import type { Actions } from 'easy-peasy';
|
||||
import { useStoreActions } from 'easy-peasy';
|
||||
import { useState } from 'react';
|
||||
import tw from 'twin.macro';
|
||||
|
||||
import { deleteUser } from '@/api/admin/users';
|
||||
import Button from '@/components/elements/Button';
|
||||
import ConfirmationModal from '@/components/elements/ConfirmationModal';
|
||||
import type { ApplicationStore } from '@/state';
|
||||
|
||||
interface Props {
|
||||
userId: number;
|
||||
onDeleted: () => void;
|
||||
}
|
||||
|
||||
export default ({ userId, onDeleted }: Props) => {
|
||||
const [visible, setVisible] = useState(false);
|
||||
const [loading, setLoading] = useState(false);
|
||||
|
||||
const { clearFlashes, clearAndAddHttpError } = useStoreActions(
|
||||
(actions: Actions<ApplicationStore>) => actions.flashes,
|
||||
);
|
||||
|
||||
const onDelete = () => {
|
||||
setLoading(true);
|
||||
clearFlashes('user');
|
||||
|
||||
deleteUser(userId)
|
||||
.then(() => {
|
||||
setLoading(false);
|
||||
onDeleted();
|
||||
})
|
||||
.catch(error => {
|
||||
console.error(error);
|
||||
clearAndAddHttpError({ key: 'user', error });
|
||||
|
||||
setLoading(false);
|
||||
setVisible(false);
|
||||
});
|
||||
};
|
||||
|
||||
return (
|
||||
<>
|
||||
<ConfirmationModal
|
||||
visible={visible}
|
||||
title={'Delete user?'}
|
||||
buttonText={'Yes, delete user'}
|
||||
onConfirmed={onDelete}
|
||||
showSpinnerOverlay={loading}
|
||||
onModalDismissed={() => setVisible(false)}
|
||||
>
|
||||
Are you sure you want to delete this user?
|
||||
</ConfirmationModal>
|
||||
|
||||
<Button type={'button'} size={'xsmall'} color={'red'} onClick={() => setVisible(true)}>
|
||||
<svg
|
||||
xmlns="http://www.w3.org/2000/svg"
|
||||
fill="none"
|
||||
viewBox="0 0 24 24"
|
||||
stroke="currentColor"
|
||||
css={tw`h-5 w-5`}
|
||||
>
|
||||
<path
|
||||
strokeLinecap="round"
|
||||
strokeLinejoin="round"
|
||||
strokeWidth={2}
|
||||
d="M19 7l-.867 12.142A2 2 0 0116.138 21H7.862a2 2 0 01-1.995-1.858L5 7m5 4v6m4-6v6m1-10V4a1 1 0 00-1-1h-4a1 1 0 00-1 1v3M4 7h16"
|
||||
/>
|
||||
</svg>
|
||||
</Button>
|
||||
</>
|
||||
);
|
||||
};
|
148
resources/scripts/components/admin/users/UserForm.tsx
Normal file
148
resources/scripts/components/admin/users/UserForm.tsx
Normal file
|
@ -0,0 +1,148 @@
|
|||
import type { Action } from 'easy-peasy';
|
||||
import { action, createContextStore } from 'easy-peasy';
|
||||
import type { FormikHelpers } from 'formik';
|
||||
import { Form, Formik } from 'formik';
|
||||
import tw from 'twin.macro';
|
||||
import { bool, object, string } from 'yup';
|
||||
|
||||
import type { UpdateUserValues } from '@/api/admin/users';
|
||||
import AdminBox from '@/components/admin/AdminBox';
|
||||
import RoleSelect from '@/components/admin/users/RoleSelect';
|
||||
import CopyOnClick from '@/components/elements/CopyOnClick';
|
||||
import FormikSwitch from '@/components/elements/FormikSwitch';
|
||||
import Input from '@/components/elements/Input';
|
||||
import Label from '@/components/elements/Label';
|
||||
import SpinnerOverlay from '@/components/elements/SpinnerOverlay';
|
||||
import Button from '@/components/elements/Button';
|
||||
import Field, { FieldRow } from '@/components/elements/Field';
|
||||
import type { User, UserRole } from '@definitions/admin';
|
||||
|
||||
interface ctx {
|
||||
user: User | undefined;
|
||||
setUser: Action<ctx, User | undefined>;
|
||||
}
|
||||
|
||||
export const Context = createContextStore<ctx>({
|
||||
user: undefined,
|
||||
|
||||
setUser: action((state, payload) => {
|
||||
state.user = payload;
|
||||
}),
|
||||
});
|
||||
|
||||
export interface Params {
|
||||
title: string;
|
||||
initialValues?: UpdateUserValues;
|
||||
children?: React.ReactNode;
|
||||
|
||||
onSubmit: (values: UpdateUserValues, helpers: FormikHelpers<UpdateUserValues>) => void;
|
||||
|
||||
uuid?: string;
|
||||
role: UserRole | null;
|
||||
}
|
||||
|
||||
export default function UserForm({ title, initialValues, children, onSubmit, uuid, role }: Params) {
|
||||
const submit = (values: UpdateUserValues, helpers: FormikHelpers<UpdateUserValues>) => {
|
||||
onSubmit(values, helpers);
|
||||
};
|
||||
|
||||
if (!initialValues) {
|
||||
initialValues = {
|
||||
externalId: '',
|
||||
username: '',
|
||||
email: '',
|
||||
password: '',
|
||||
adminRoleId: null,
|
||||
rootAdmin: false,
|
||||
};
|
||||
}
|
||||
|
||||
return (
|
||||
<Formik
|
||||
onSubmit={submit}
|
||||
initialValues={initialValues}
|
||||
validationSchema={object().shape({
|
||||
username: string().min(1).max(32),
|
||||
email: string(),
|
||||
rootAdmin: bool().required(),
|
||||
})}
|
||||
>
|
||||
{({ isSubmitting, isValid }) => (
|
||||
<>
|
||||
<AdminBox title={title} css={tw`relative`}>
|
||||
<SpinnerOverlay visible={isSubmitting} />
|
||||
|
||||
<Form css={tw`mb-0`}>
|
||||
<FieldRow>
|
||||
{uuid && (
|
||||
<div>
|
||||
<Label>UUID</Label>
|
||||
<CopyOnClick text={uuid}>
|
||||
<Input type={'text'} value={uuid} readOnly />
|
||||
</CopyOnClick>
|
||||
</div>
|
||||
)}
|
||||
<Field
|
||||
id={'externalId'}
|
||||
name={'externalId'}
|
||||
label={'External ID'}
|
||||
type={'text'}
|
||||
description={
|
||||
'Used by external integrations, this field should not be modified unless you know what you are doing.'
|
||||
}
|
||||
/>
|
||||
<Field
|
||||
id={'username'}
|
||||
name={'username'}
|
||||
label={'Username'}
|
||||
type={'text'}
|
||||
description={"The user's username, what else would go here?"}
|
||||
/>
|
||||
<Field
|
||||
id={'email'}
|
||||
name={'email'}
|
||||
label={'Email Address'}
|
||||
type={'email'}
|
||||
description={"The user's email address, what else would go here?"}
|
||||
/>
|
||||
<Field
|
||||
id={'password'}
|
||||
name={'password'}
|
||||
label={'Password'}
|
||||
type={'password'}
|
||||
placeholder={'••••••••'}
|
||||
autoComplete={'new-password'}
|
||||
/* TODO: Change description depending on if user is being created or updated. */
|
||||
description={
|
||||
'Leave empty to email the user a link where they will be required to set a password.'
|
||||
}
|
||||
/>
|
||||
<RoleSelect selected={role} />
|
||||
</FieldRow>
|
||||
|
||||
{/* TODO: Remove toggle once role permissions are implemented. */}
|
||||
<div css={tw`w-full flex flex-row mb-6`}>
|
||||
<div css={tw`w-full bg-neutral-800 border border-neutral-900 shadow-inner p-4 rounded`}>
|
||||
<FormikSwitch
|
||||
name={'rootAdmin'}
|
||||
label={'Root Admin'}
|
||||
description={'Should this user be a root administrator?'}
|
||||
/>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div css={tw`w-full flex flex-row items-center mt-6`}>
|
||||
{children}
|
||||
<div css={tw`flex ml-auto`}>
|
||||
<Button type={'submit'} disabled={isSubmitting || !isValid}>
|
||||
Save Changes
|
||||
</Button>
|
||||
</div>
|
||||
</div>
|
||||
</Form>
|
||||
</AdminBox>
|
||||
</>
|
||||
)}
|
||||
</Formik>
|
||||
);
|
||||
}
|
114
resources/scripts/components/admin/users/UserRouter.tsx
Normal file
114
resources/scripts/components/admin/users/UserRouter.tsx
Normal file
|
@ -0,0 +1,114 @@
|
|||
import type { Action, Actions } from 'easy-peasy';
|
||||
import { action, createContextStore, useStoreActions } from 'easy-peasy';
|
||||
import { useEffect, useState } from 'react';
|
||||
import { Route, Routes, useParams } from 'react-router-dom';
|
||||
import tw from 'twin.macro';
|
||||
|
||||
import { getUser } from '@/api/admin/users';
|
||||
import AdminContentBlock from '@/components/admin/AdminContentBlock';
|
||||
import { SubNavigation, SubNavigationLink } from '@/components/admin/SubNavigation';
|
||||
import UserAboutContainer from '@/components/admin/users/UserAboutContainer';
|
||||
import UserServers from '@/components/admin/users/UserServers';
|
||||
import Spinner from '@/components/elements/Spinner';
|
||||
import FlashMessageRender from '@/components/FlashMessageRender';
|
||||
import type { ApplicationStore } from '@/state';
|
||||
import type { User } from '@definitions/admin';
|
||||
|
||||
interface ctx {
|
||||
user: User | undefined;
|
||||
setUser: Action<ctx, User | undefined>;
|
||||
}
|
||||
|
||||
export const Context = createContextStore<ctx>({
|
||||
user: undefined,
|
||||
|
||||
setUser: action((state, payload) => {
|
||||
state.user = payload;
|
||||
}),
|
||||
});
|
||||
|
||||
const UserRouter = () => {
|
||||
const params = useParams<'id'>();
|
||||
|
||||
const { clearFlashes, clearAndAddHttpError } = useStoreActions(
|
||||
(actions: Actions<ApplicationStore>) => actions.flashes,
|
||||
);
|
||||
const [loading, setLoading] = useState(true);
|
||||
|
||||
const user = Context.useStoreState(state => state.user);
|
||||
const setUser = Context.useStoreActions(actions => actions.setUser);
|
||||
|
||||
useEffect(() => {
|
||||
clearFlashes('user');
|
||||
|
||||
getUser(Number(params.id), ['role'])
|
||||
.then(user => setUser(user))
|
||||
.catch(error => {
|
||||
console.error(error);
|
||||
clearAndAddHttpError({ key: 'user', error });
|
||||
})
|
||||
.then(() => setLoading(false));
|
||||
}, []);
|
||||
|
||||
if (loading || user === undefined) {
|
||||
return (
|
||||
<AdminContentBlock>
|
||||
<FlashMessageRender byKey={'user'} css={tw`mb-4`} />
|
||||
|
||||
<div css={tw`w-full flex flex-col items-center justify-center`} style={{ height: '24rem' }}>
|
||||
<Spinner size={'base'} />
|
||||
</div>
|
||||
</AdminContentBlock>
|
||||
);
|
||||
}
|
||||
|
||||
return (
|
||||
<AdminContentBlock title={'User - ' + user.id}>
|
||||
<div css={tw`w-full flex flex-row items-center mb-4`}>
|
||||
<div css={tw`flex flex-col flex-shrink`} style={{ minWidth: '0' }}>
|
||||
<h2 css={tw`text-2xl text-neutral-50 font-header font-medium`}>{user.email}</h2>
|
||||
<p css={tw`text-base text-neutral-400 whitespace-nowrap overflow-ellipsis overflow-hidden`}>
|
||||
{user.uuid}
|
||||
</p>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<FlashMessageRender byKey={'user'} css={tw`mb-4`} />
|
||||
|
||||
<SubNavigation>
|
||||
<SubNavigationLink to={`/admin/users/${params.id}`} name={'About'}>
|
||||
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 20 20" fill="currentColor">
|
||||
<path
|
||||
clipRule="evenodd"
|
||||
fillRule="evenodd"
|
||||
d="M4 4a2 2 0 012-2h8a2 2 0 012 2v12a1 1 0 110 2h-3a1 1 0 01-1-1v-2a1 1 0 00-1-1H9a1 1 0 00-1 1v2a1 1 0 01-1 1H4a1 1 0 110-2V4zm3 1h2v2H7V5zm2 4H7v2h2V9zm2-4h2v2h-2V5zm2 4h-2v2h2V9z"
|
||||
/>
|
||||
</svg>
|
||||
</SubNavigationLink>
|
||||
|
||||
<SubNavigationLink to={`/admin/users/${params.id}/servers`} name={'Servers'}>
|
||||
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 20 20" fill="currentColor">
|
||||
<path
|
||||
clipRule="evenodd"
|
||||
fillRule="evenodd"
|
||||
d="M2 5a2 2 0 012-2h12a2 2 0 012 2v10a2 2 0 01-2 2H4a2 2 0 01-2-2V5zm3.293 1.293a1 1 0 011.414 0l3 3a1 1 0 010 1.414l-3 3a1 1 0 01-1.414-1.414L7.586 10 5.293 7.707a1 1 0 010-1.414zM11 12a1 1 0 100 2h3a1 1 0 100-2h-3z"
|
||||
/>
|
||||
</svg>
|
||||
</SubNavigationLink>
|
||||
</SubNavigation>
|
||||
|
||||
<Routes>
|
||||
<Route path="" element={<UserAboutContainer />} />
|
||||
<Route path="servers" element={<UserServers />} />
|
||||
</Routes>
|
||||
</AdminContentBlock>
|
||||
);
|
||||
};
|
||||
|
||||
export default () => {
|
||||
return (
|
||||
<Context.Provider>
|
||||
<UserRouter />
|
||||
</Context.Provider>
|
||||
);
|
||||
};
|
10
resources/scripts/components/admin/users/UserServers.tsx
Normal file
10
resources/scripts/components/admin/users/UserServers.tsx
Normal file
|
@ -0,0 +1,10 @@
|
|||
import ServersTable from '@/components/admin/servers/ServersTable';
|
||||
import { Context } from '@/components/admin/users/UserRouter';
|
||||
|
||||
function UserServers() {
|
||||
const user = Context.useStoreState(state => state.user);
|
||||
|
||||
return <ServersTable filters={{ owner_id: user?.id?.toString?.() }} />;
|
||||
}
|
||||
|
||||
export default UserServers;
|
79
resources/scripts/components/admin/users/UserTableRow.tsx
Normal file
79
resources/scripts/components/admin/users/UserTableRow.tsx
Normal file
|
@ -0,0 +1,79 @@
|
|||
import { BanIcon, DotsVerticalIcon, LockOpenIcon, PencilIcon, SupportIcon, TrashIcon } from '@heroicons/react/solid';
|
||||
import { useState } from 'react';
|
||||
|
||||
import Checkbox from '@/components/elements/inputs/Checkbox';
|
||||
import { Dropdown } from '@/components/elements/dropdown';
|
||||
import { Dialog } from '@/components/elements/dialog';
|
||||
import { Button } from '@/components/elements/button';
|
||||
import { User } from '@definitions/admin';
|
||||
|
||||
interface Props {
|
||||
user: User;
|
||||
selected?: boolean;
|
||||
onRowChange: (user: User, selected: boolean) => void;
|
||||
}
|
||||
|
||||
const UserTableRow = ({ user, selected, onRowChange }: Props) => {
|
||||
const [visible, setVisible] = useState(false);
|
||||
|
||||
return (
|
||||
<>
|
||||
<Dialog title={'Delete account'} visible={visible} onDismissed={() => setVisible(false)}>
|
||||
<Dialog.Icon type={'danger'} />
|
||||
This account will be permanently deleted.
|
||||
<Dialog.Buttons>
|
||||
<Button.Text onClick={() => setVisible(false)}>Cancel</Button.Text>
|
||||
<Button.Danger>Delete</Button.Danger>
|
||||
</Dialog.Buttons>
|
||||
</Dialog>
|
||||
<tr>
|
||||
<td className={'whitespace-nowrap'}>
|
||||
<div className={'flex justify-end items-center w-8'}>
|
||||
<Checkbox checked={selected} onChange={e => onRowChange(user, e.currentTarget.checked)} />
|
||||
</div>
|
||||
</td>
|
||||
<td className={'pl-6 py-4 whitespace-nowrap'}>
|
||||
<div className={'flex items-center'}>
|
||||
<div className={'w-10 h-10'}>
|
||||
<img src={user.avatarUrl} className={'w-10 h-10 rounded-full'} alt={'User avatar'} />
|
||||
</div>
|
||||
<div className={'ml-4'}>
|
||||
<p className={'font-medium'}>{user.email}</p>
|
||||
<p className={'text-sm text-neutral-400'}>{user.uuid}</p>
|
||||
</div>
|
||||
</div>
|
||||
</td>
|
||||
<td className={'pl-2 py-4 whitespace-nowrap'}>
|
||||
{user.isUsingTwoFactor && (
|
||||
<span
|
||||
className={
|
||||
'bg-green-100 uppercase text-green-700 font-semibold text-xs px-2 py-0.5 rounded'
|
||||
}
|
||||
>
|
||||
2-FA Enabled
|
||||
</span>
|
||||
)}
|
||||
</td>
|
||||
<td className={'px-6 py-4 whitespace-nowrap'}>
|
||||
<Dropdown>
|
||||
<Dropdown.Button className={'px-2'}>
|
||||
<DotsVerticalIcon />
|
||||
</Dropdown.Button>
|
||||
<Dropdown.Item icon={<PencilIcon />}>Edit</Dropdown.Item>
|
||||
<Dropdown.Item icon={<SupportIcon />}>Reset Password</Dropdown.Item>
|
||||
<Dropdown.Item icon={<LockOpenIcon />} disabled={!user.isUsingTwoFactor}>
|
||||
Disable 2-FA
|
||||
</Dropdown.Item>
|
||||
<Dropdown.Item icon={<BanIcon />}>Suspend</Dropdown.Item>
|
||||
<Dropdown.Gap />
|
||||
<Dropdown.Item icon={<TrashIcon />} onClick={() => setVisible(true)} danger>
|
||||
Delete Account
|
||||
</Dropdown.Item>
|
||||
</Dropdown>
|
||||
</td>
|
||||
</tr>
|
||||
</>
|
||||
);
|
||||
};
|
||||
|
||||
export default UserTableRow;
|
119
resources/scripts/components/admin/users/UsersContainer.tsx
Normal file
119
resources/scripts/components/admin/users/UsersContainer.tsx
Normal file
|
@ -0,0 +1,119 @@
|
|||
import { LockOpenIcon, PlusIcon, SupportIcon, TrashIcon } from '@heroicons/react/solid';
|
||||
import { Fragment, useEffect, useState } from 'react';
|
||||
|
||||
import { useGetUsers } from '@/api/admin/users';
|
||||
import type { UUID } from '@/api/definitions';
|
||||
import { Transition } from '@/components/elements/transitions';
|
||||
import { Button } from '@/components/elements/button/index';
|
||||
import Checkbox from '@/components/elements/inputs/Checkbox';
|
||||
import InputField from '@/components/elements/inputs/InputField';
|
||||
import UserTableRow from '@/components/admin/users/UserTableRow';
|
||||
import TFootPaginated from '@/components/elements/table/TFootPaginated';
|
||||
import type { User } from '@definitions/admin';
|
||||
import extractSearchFilters from '@/helpers/extractSearchFilters';
|
||||
import useDebouncedState from '@/plugins/useDebouncedState';
|
||||
|
||||
const filters = ['id', 'uuid', 'external_id', 'username', 'email'] as const;
|
||||
|
||||
const UsersContainer = () => {
|
||||
const [search, setSearch] = useDebouncedState('', 500);
|
||||
const [selected, setSelected] = useState<UUID[]>([]);
|
||||
const { data: users } = useGetUsers(
|
||||
extractSearchFilters(search, filters, {
|
||||
splitUnmatched: true,
|
||||
returnUnmatched: true,
|
||||
}),
|
||||
);
|
||||
|
||||
useEffect(() => {
|
||||
document.title = 'Admin | Users';
|
||||
}, []);
|
||||
|
||||
const onRowChange = (user: User, checked: boolean) => {
|
||||
setSelected(state => {
|
||||
return checked ? [...state, user.uuid] : selected.filter(uuid => uuid !== user.uuid);
|
||||
});
|
||||
};
|
||||
|
||||
const selectAllChecked = users && users.items.length > 0 && selected.length > 0;
|
||||
const onSelectAll = () =>
|
||||
setSelected(state => (state.length > 0 ? [] : users?.items.map(({ uuid }) => uuid) || []));
|
||||
|
||||
return (
|
||||
<div>
|
||||
<div className={'flex justify-end mb-4'}>
|
||||
<Button className={'shadow focus:ring-offset-2 focus:ring-offset-neutral-800'}>
|
||||
Add User <PlusIcon className={'ml-2 w-5 h-5'} />
|
||||
</Button>
|
||||
</div>
|
||||
<div className={'relative flex items-center rounded-t bg-neutral-700 px-4 py-2'}>
|
||||
<div className={'mr-6'}>
|
||||
<Checkbox
|
||||
checked={selectAllChecked}
|
||||
disabled={!users?.items.length}
|
||||
indeterminate={selected.length !== users?.items.length}
|
||||
onChange={onSelectAll}
|
||||
/>
|
||||
</div>
|
||||
<div className={'flex-1'}>
|
||||
<InputField
|
||||
type={'text'}
|
||||
name={'filter'}
|
||||
placeholder={'Begin typing to filter...'}
|
||||
className={'w-56 focus:w-96'}
|
||||
onChange={e => setSearch(e.currentTarget.value)}
|
||||
/>
|
||||
</div>
|
||||
<Transition.Fade as={Fragment} show={selected.length > 0} duration={'duration-75'}>
|
||||
<div
|
||||
className={
|
||||
'absolute rounded-t bg-neutral-700 w-full h-full top-0 left-0 flex items-center justify-end space-x-4 px-4'
|
||||
}
|
||||
>
|
||||
<div className={'flex-1'}>
|
||||
<Checkbox
|
||||
checked={selectAllChecked}
|
||||
indeterminate={selected.length !== users?.items.length}
|
||||
onChange={onSelectAll}
|
||||
/>
|
||||
</div>
|
||||
<Button.Text square>
|
||||
<SupportIcon className={'w-4 h-4'} />
|
||||
</Button.Text>
|
||||
<Button.Text square>
|
||||
<LockOpenIcon className={'w-4 h-4'} />
|
||||
</Button.Text>
|
||||
<Button.Text square>
|
||||
<TrashIcon className={'w-4 h-4'} />
|
||||
</Button.Text>
|
||||
</div>
|
||||
</Transition.Fade>
|
||||
</div>
|
||||
<table className={'min-w-full rounded bg-neutral-700'}>
|
||||
<thead className={'bg-neutral-900'}>
|
||||
<tr>
|
||||
<th scope={'col'} className={'w-8'} />
|
||||
<th scope={'col'} className={'text-left px-6 py-2 w-full'}>
|
||||
Email
|
||||
</th>
|
||||
<th scope={'col'} />
|
||||
<th scope={'col'} />
|
||||
</tr>
|
||||
</thead>
|
||||
<tbody>
|
||||
{users?.items.map(user => (
|
||||
<UserTableRow
|
||||
key={user.uuid}
|
||||
user={user}
|
||||
selected={selected.includes(user.uuid)}
|
||||
onRowChange={onRowChange}
|
||||
/>
|
||||
))}
|
||||
</tbody>
|
||||
{users && <TFootPaginated span={4} pagination={users.pagination} />}
|
||||
</table>
|
||||
</div>
|
||||
);
|
||||
};
|
||||
|
||||
export default UsersContainer;
|
Loading…
Add table
Add a link
Reference in a new issue