mirror of
https://github.com/documenso/documenso.git
synced 2025-11-13 16:23:06 +10:00
chore: implement pr feedback
This commit is contained in:
@ -3,21 +3,23 @@
|
|||||||
import { useRouter } from 'next/navigation';
|
import { useRouter } from 'next/navigation';
|
||||||
|
|
||||||
import { zodResolver } from '@hookform/resolvers/zod';
|
import { zodResolver } from '@hookform/resolvers/zod';
|
||||||
import { Loader } from 'lucide-react';
|
import { useForm } from 'react-hook-form';
|
||||||
import { Controller, useForm } from 'react-hook-form';
|
|
||||||
|
|
||||||
import { trpc } from '@documenso/trpc/react';
|
import { trpc } from '@documenso/trpc/react';
|
||||||
import { Button } from '@documenso/ui/primitives/button';
|
import { Button } from '@documenso/ui/primitives/button';
|
||||||
import { Combobox } from '@documenso/ui/primitives/combobox';
|
import { Combobox } from '@documenso/ui/primitives/combobox';
|
||||||
|
import {
|
||||||
|
Form,
|
||||||
|
FormControl,
|
||||||
|
FormField,
|
||||||
|
FormItem,
|
||||||
|
FormLabel,
|
||||||
|
FormMessage,
|
||||||
|
} from '@documenso/ui/primitives/form/form';
|
||||||
import { Input } from '@documenso/ui/primitives/input';
|
import { Input } from '@documenso/ui/primitives/input';
|
||||||
import { Label } from '@documenso/ui/primitives/label';
|
|
||||||
import { useToast } from '@documenso/ui/primitives/use-toast';
|
import { useToast } from '@documenso/ui/primitives/use-toast';
|
||||||
|
|
||||||
import { FormErrorMessage } from '../../../../../components/form/form-error-message';
|
import { TUserFormSchema, ZUserFormSchema } from '~/providers/admin-user-profile-update.types';
|
||||||
import {
|
|
||||||
TUserFormSchema,
|
|
||||||
ZUserFormSchema,
|
|
||||||
} from '../../../../../providers/admin-user-profile-update.types';
|
|
||||||
|
|
||||||
export default function UserPage({ params }: { params: { id: number } }) {
|
export default function UserPage({ params }: { params: { id: number } }) {
|
||||||
const { toast } = useToast();
|
const { toast } = useToast();
|
||||||
@ -34,21 +36,11 @@ export default function UserPage({ params }: { params: { id: number } }) {
|
|||||||
|
|
||||||
const user = result.data;
|
const user = result.data;
|
||||||
|
|
||||||
const roles = user?.roles;
|
const roles = user?.roles ?? [];
|
||||||
let rolesArr: string[] = [];
|
|
||||||
|
|
||||||
if (roles) {
|
|
||||||
rolesArr = Object.values(roles);
|
|
||||||
}
|
|
||||||
|
|
||||||
const { mutateAsync: updateUserMutation } = trpc.admin.updateUser.useMutation();
|
const { mutateAsync: updateUserMutation } = trpc.admin.updateUser.useMutation();
|
||||||
|
|
||||||
const {
|
const form = useForm<TUserFormSchema>({
|
||||||
register,
|
|
||||||
control,
|
|
||||||
handleSubmit,
|
|
||||||
formState: { errors, isSubmitting },
|
|
||||||
} = useForm<TUserFormSchema>({
|
|
||||||
resolver: zodResolver(ZUserFormSchema),
|
resolver: zodResolver(ZUserFormSchema),
|
||||||
values: {
|
values: {
|
||||||
name: user?.name ?? '',
|
name: user?.name ?? '',
|
||||||
@ -85,42 +77,69 @@ export default function UserPage({ params }: { params: { id: number } }) {
|
|||||||
return (
|
return (
|
||||||
<div>
|
<div>
|
||||||
<h2 className="text-4xl font-semibold">Manage {user?.name}'s profile</h2>
|
<h2 className="text-4xl font-semibold">Manage {user?.name}'s profile</h2>
|
||||||
<form className="mt-6 flex w-full flex-col gap-y-4" onSubmit={handleSubmit(onSubmit)}>
|
<Form {...form}>
|
||||||
<div>
|
<form onSubmit={form.handleSubmit(onSubmit)}>
|
||||||
<Label htmlFor="name" className="text-muted-foreground">
|
<fieldset className="mt-6 flex w-full flex-col gap-y-4">
|
||||||
Name
|
<FormField
|
||||||
</Label>
|
control={form.control}
|
||||||
<Input defaultValue={user?.name ?? ''} type="text" {...register('name')} />
|
name="name"
|
||||||
<FormErrorMessage className="mt-1.5" error={errors.name} />
|
render={({ field }) => (
|
||||||
</div>
|
<FormItem>
|
||||||
<div>
|
<FormLabel required className="text-muted-foreground">
|
||||||
<Label htmlFor="email" className="text-muted-foreground">
|
Name
|
||||||
Email
|
</FormLabel>
|
||||||
</Label>
|
<FormControl>
|
||||||
<Input defaultValue={user?.email} type="email" {...register('email')} />
|
<Input type="text" {...field} value={field.value ?? ''} />
|
||||||
<FormErrorMessage className="mt-1.5" error={errors.email} />
|
</FormControl>
|
||||||
</div>
|
<FormMessage />
|
||||||
<div className="flex flex-col gap-2">
|
</FormItem>
|
||||||
<Label htmlFor="roles" className="text-muted-foreground">
|
)}
|
||||||
User roles
|
/>
|
||||||
</Label>
|
<FormField
|
||||||
<Controller
|
control={form.control}
|
||||||
control={control}
|
name="email"
|
||||||
name="roles"
|
render={({ field }) => (
|
||||||
render={({ field: { onChange } }) => (
|
<FormItem>
|
||||||
<Combobox listValues={rolesArr} onChange={(values: string[]) => onChange(values)} />
|
<FormLabel required className="text-muted-foreground">
|
||||||
)}
|
Email
|
||||||
/>
|
</FormLabel>
|
||||||
<FormErrorMessage className="mt-1.5" error={errors.roles} />
|
<FormControl>
|
||||||
</div>
|
<Input type="text" {...field} />
|
||||||
|
</FormControl>
|
||||||
|
<FormMessage />
|
||||||
|
</FormItem>
|
||||||
|
)}
|
||||||
|
/>
|
||||||
|
|
||||||
<div className="mt-4">
|
<FormField
|
||||||
<Button type="submit" disabled={isSubmitting}>
|
control={form.control}
|
||||||
{isSubmitting && <Loader className="mr-2 h-5 w-5 animate-spin" />}
|
name="roles"
|
||||||
Update user
|
render={({ field: { onChange } }) => (
|
||||||
</Button>
|
<FormItem>
|
||||||
</div>
|
<fieldset className="flex flex-col gap-2">
|
||||||
</form>
|
<FormLabel required className="text-muted-foreground">
|
||||||
|
Roles
|
||||||
|
</FormLabel>
|
||||||
|
<FormControl>
|
||||||
|
<Combobox
|
||||||
|
listValues={roles}
|
||||||
|
onChange={(values: string[]) => onChange(values)}
|
||||||
|
/>
|
||||||
|
</FormControl>
|
||||||
|
<FormMessage />
|
||||||
|
</fieldset>
|
||||||
|
</FormItem>
|
||||||
|
)}
|
||||||
|
/>
|
||||||
|
|
||||||
|
<div className="mt-4">
|
||||||
|
<Button type="submit" loading={form.formState.isSubmitting}>
|
||||||
|
Update user
|
||||||
|
</Button>
|
||||||
|
</div>
|
||||||
|
</fieldset>
|
||||||
|
</form>
|
||||||
|
</Form>
|
||||||
</div>
|
</div>
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|||||||
@ -15,7 +15,7 @@ export const updateUser = async ({ id, name, email, roles }: UpdateUserOptions)
|
|||||||
},
|
},
|
||||||
});
|
});
|
||||||
|
|
||||||
const updatedUser = await prisma.user.update({
|
return await prisma.user.update({
|
||||||
where: {
|
where: {
|
||||||
id,
|
id,
|
||||||
},
|
},
|
||||||
@ -25,5 +25,4 @@ export const updateUser = async ({ id, name, email, roles }: UpdateUserOptions)
|
|||||||
roles,
|
roles,
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
return updatedUser;
|
|
||||||
};
|
};
|
||||||
|
|||||||
@ -2,7 +2,7 @@ import { Prisma } from '@prisma/client';
|
|||||||
|
|
||||||
import { prisma } from '@documenso/prisma';
|
import { prisma } from '@documenso/prisma';
|
||||||
|
|
||||||
type getAllUsersProps = {
|
type GetAllUsersProps = {
|
||||||
username: string;
|
username: string;
|
||||||
email: string;
|
email: string;
|
||||||
page: number;
|
page: number;
|
||||||
@ -14,7 +14,7 @@ export const findUsers = async ({
|
|||||||
email = '',
|
email = '',
|
||||||
page = 1,
|
page = 1,
|
||||||
perPage = 10,
|
perPage = 10,
|
||||||
}: getAllUsersProps) => {
|
}: GetAllUsersProps) => {
|
||||||
const whereClause = Prisma.validator<Prisma.UserWhereInput>()({
|
const whereClause = Prisma.validator<Prisma.UserWhereInput>()({
|
||||||
OR: [
|
OR: [
|
||||||
{
|
{
|
||||||
|
|||||||
@ -1,24 +1,14 @@
|
|||||||
import { TRPCError } from '@trpc/server';
|
import { TRPCError } from '@trpc/server';
|
||||||
|
|
||||||
import { isAdmin } from '@documenso/lib/next-auth/guards/is-admin';
|
|
||||||
import { updateUser } from '@documenso/lib/server-only/admin/update-user';
|
import { updateUser } from '@documenso/lib/server-only/admin/update-user';
|
||||||
|
|
||||||
import { authenticatedProcedure, router } from '../trpc';
|
import { adminProcedure, router } from '../trpc';
|
||||||
import { ZUpdateProfileMutationByAdminSchema } from './schema';
|
import { ZUpdateProfileMutationByAdminSchema } from './schema';
|
||||||
|
|
||||||
export const adminRouter = router({
|
export const adminRouter = router({
|
||||||
updateUser: authenticatedProcedure
|
updateUser: adminProcedure
|
||||||
.input(ZUpdateProfileMutationByAdminSchema)
|
.input(ZUpdateProfileMutationByAdminSchema)
|
||||||
.mutation(async ({ input, ctx }) => {
|
.mutation(async ({ input }) => {
|
||||||
const isUserAdmin = isAdmin(ctx.user);
|
|
||||||
|
|
||||||
if (!isUserAdmin) {
|
|
||||||
throw new TRPCError({
|
|
||||||
code: 'UNAUTHORIZED',
|
|
||||||
message: 'Not authorized to perform this action.',
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
const { id, name, email, roles } = input;
|
const { id, name, email, roles } = input;
|
||||||
|
|
||||||
try {
|
try {
|
||||||
|
|||||||
@ -1,13 +1,12 @@
|
|||||||
import { TRPCError } from '@trpc/server';
|
import { TRPCError } from '@trpc/server';
|
||||||
|
|
||||||
import { isAdmin } from '@documenso/lib/next-auth/guards/is-admin';
|
|
||||||
import { forgotPassword } from '@documenso/lib/server-only/user/forgot-password';
|
import { forgotPassword } from '@documenso/lib/server-only/user/forgot-password';
|
||||||
import { getUserById } from '@documenso/lib/server-only/user/get-user-by-id';
|
import { getUserById } from '@documenso/lib/server-only/user/get-user-by-id';
|
||||||
import { resetPassword } from '@documenso/lib/server-only/user/reset-password';
|
import { resetPassword } from '@documenso/lib/server-only/user/reset-password';
|
||||||
import { updatePassword } from '@documenso/lib/server-only/user/update-password';
|
import { updatePassword } from '@documenso/lib/server-only/user/update-password';
|
||||||
import { updateProfile } from '@documenso/lib/server-only/user/update-profile';
|
import { updateProfile } from '@documenso/lib/server-only/user/update-profile';
|
||||||
|
|
||||||
import { authenticatedProcedure, procedure, router } from '../trpc';
|
import { adminProcedure, authenticatedProcedure, procedure, router } from '../trpc';
|
||||||
import {
|
import {
|
||||||
ZForgotPasswordFormSchema,
|
ZForgotPasswordFormSchema,
|
||||||
ZResetPasswordFormSchema,
|
ZResetPasswordFormSchema,
|
||||||
@ -17,29 +16,18 @@ import {
|
|||||||
} from './schema';
|
} from './schema';
|
||||||
|
|
||||||
export const profileRouter = router({
|
export const profileRouter = router({
|
||||||
getUser: authenticatedProcedure
|
getUser: adminProcedure.input(ZRetrieveUserByIdQuerySchema).query(async ({ input }) => {
|
||||||
.input(ZRetrieveUserByIdQuerySchema)
|
try {
|
||||||
.query(async ({ input, ctx }) => {
|
const { id } = input;
|
||||||
const isUserAdmin = isAdmin(ctx.user);
|
|
||||||
|
|
||||||
if (!isUserAdmin) {
|
return await getUserById({ id });
|
||||||
throw new TRPCError({
|
} catch (err) {
|
||||||
code: 'UNAUTHORIZED',
|
throw new TRPCError({
|
||||||
message: 'Not authorized to perform this action.',
|
code: 'BAD_REQUEST',
|
||||||
});
|
message: 'We were unable to retrieve the specified account. Please try again.',
|
||||||
}
|
});
|
||||||
|
}
|
||||||
try {
|
}),
|
||||||
const { id } = input;
|
|
||||||
|
|
||||||
return await getUserById({ id });
|
|
||||||
} catch (err) {
|
|
||||||
throw new TRPCError({
|
|
||||||
code: 'BAD_REQUEST',
|
|
||||||
message: 'We were unable to retrieve the specified account. Please try again.',
|
|
||||||
});
|
|
||||||
}
|
|
||||||
}),
|
|
||||||
|
|
||||||
updateProfile: authenticatedProcedure
|
updateProfile: authenticatedProcedure
|
||||||
.input(ZUpdateProfileMutationSchema)
|
.input(ZUpdateProfileMutationSchema)
|
||||||
|
|||||||
@ -1,6 +1,8 @@
|
|||||||
import { TRPCError, initTRPC } from '@trpc/server';
|
import { TRPCError, initTRPC } from '@trpc/server';
|
||||||
import SuperJSON from 'superjson';
|
import SuperJSON from 'superjson';
|
||||||
|
|
||||||
|
import { isAdmin } from '@documenso/lib/next-auth/guards/is-admin';
|
||||||
|
|
||||||
import { TrpcContext } from './context';
|
import { TrpcContext } from './context';
|
||||||
|
|
||||||
const t = initTRPC.context<TrpcContext>().create({
|
const t = initTRPC.context<TrpcContext>().create({
|
||||||
@ -28,9 +30,37 @@ export const authenticatedMiddleware = t.middleware(async ({ ctx, next }) => {
|
|||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
export const adminMiddleware = t.middleware(async ({ ctx, next }) => {
|
||||||
|
if (!ctx.session || !ctx.user) {
|
||||||
|
throw new TRPCError({
|
||||||
|
code: 'UNAUTHORIZED',
|
||||||
|
message: 'You must be logged in to perform this action.',
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
const isUserAdmin = isAdmin(ctx.user);
|
||||||
|
|
||||||
|
if (!isUserAdmin) {
|
||||||
|
throw new TRPCError({
|
||||||
|
code: 'UNAUTHORIZED',
|
||||||
|
message: 'Not authorized to perform this action.',
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
return await next({
|
||||||
|
ctx: {
|
||||||
|
...ctx,
|
||||||
|
|
||||||
|
user: ctx.user,
|
||||||
|
session: ctx.session,
|
||||||
|
},
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Routers and Procedures
|
* Routers and Procedures
|
||||||
*/
|
*/
|
||||||
export const router = t.router;
|
export const router = t.router;
|
||||||
export const procedure = t.procedure;
|
export const procedure = t.procedure;
|
||||||
export const authenticatedProcedure = t.procedure.use(authenticatedMiddleware);
|
export const authenticatedProcedure = t.procedure.use(authenticatedMiddleware);
|
||||||
|
export const adminProcedure = t.procedure.use(adminMiddleware);
|
||||||
|
|||||||
@ -44,40 +44,38 @@ const Combobox = ({ listValues, onChange }: ComboboxProps) => {
|
|||||||
};
|
};
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<>
|
<Popover open={open} onOpenChange={setOpen}>
|
||||||
<Popover open={open} onOpenChange={setOpen}>
|
<PopoverTrigger asChild>
|
||||||
<PopoverTrigger asChild>
|
<Button
|
||||||
<Button
|
variant="outline"
|
||||||
variant="outline"
|
role="combobox"
|
||||||
role="combobox"
|
aria-expanded={open}
|
||||||
aria-expanded={open}
|
className="w-[200px] justify-between"
|
||||||
className="w-[200px] justify-between"
|
>
|
||||||
>
|
{selectedValues.length > 0 ? selectedValues.join(', ') : 'Select values...'}
|
||||||
{selectedValues.length > 0 ? selectedValues.join(', ') : 'Select values...'}
|
<ChevronsUpDown className="ml-2 h-4 w-4 shrink-0 opacity-50" />
|
||||||
<ChevronsUpDown className="ml-2 h-4 w-4 shrink-0 opacity-50" />
|
</Button>
|
||||||
</Button>
|
</PopoverTrigger>
|
||||||
</PopoverTrigger>
|
<PopoverContent className="w-[200px] p-0">
|
||||||
<PopoverContent className="w-[200px] p-0">
|
<Command>
|
||||||
<Command>
|
<CommandInput placeholder={selectedValues.join(', ')} />
|
||||||
<CommandInput placeholder={selectedValues.join(', ')} />
|
<CommandEmpty>No value found.</CommandEmpty>
|
||||||
<CommandEmpty>No value found.</CommandEmpty>
|
<CommandGroup>
|
||||||
<CommandGroup>
|
{allRoles.map((value: string, i: number) => (
|
||||||
{allRoles.map((value: string, i: number) => (
|
<CommandItem key={i} onSelect={() => handleSelect(value)}>
|
||||||
<CommandItem key={i} onSelect={() => handleSelect(value)}>
|
<Check
|
||||||
<Check
|
className={cn(
|
||||||
className={cn(
|
'mr-2 h-4 w-4',
|
||||||
'mr-2 h-4 w-4',
|
selectedValues.includes(value) ? 'opacity-100' : 'opacity-0',
|
||||||
selectedValues.includes(value) ? 'opacity-100' : 'opacity-0',
|
)}
|
||||||
)}
|
/>
|
||||||
/>
|
{value}
|
||||||
{value}
|
</CommandItem>
|
||||||
</CommandItem>
|
))}
|
||||||
))}
|
</CommandGroup>
|
||||||
</CommandGroup>
|
</Command>
|
||||||
</Command>
|
</PopoverContent>
|
||||||
</PopoverContent>
|
</Popover>
|
||||||
</Popover>
|
|
||||||
</>
|
|
||||||
);
|
);
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user