diff --git a/src/app/(dashboard)/admin/layout.tsx b/src/app/(dashboard)/admin/layout.tsx deleted file mode 100644 index 44c611b..0000000 --- a/src/app/(dashboard)/admin/layout.tsx +++ /dev/null @@ -1,13 +0,0 @@ -import type { ReactNode } from "react" - -import { requireRole } from "@/services/auth.service" - -export default async function AdminLayout({ - children, -}: { - children: ReactNode -}) { - await requireRole("ADMIN") - - return children -} diff --git a/src/app/(dashboard)/admin/users/[userId]/edit/page.tsx b/src/app/(dashboard)/admin/users/[userId]/edit/page.tsx deleted file mode 100644 index 3048447..0000000 --- a/src/app/(dashboard)/admin/users/[userId]/edit/page.tsx +++ /dev/null @@ -1,22 +0,0 @@ -import { redirect } from "next/navigation" - -import prisma from "@/lib/prisma" - -export default async function EditUserPage({ - params, -}: { - params: Promise<{ userId: string }> -}) { - const { userId } = await params - - const person = await prisma.person.findFirst({ - where: { userId }, - select: { id: true }, - }) - - if (!person) { - redirect("/people") - } - - redirect(`/people/${person.id}/edit`) -} diff --git a/src/app/(dashboard)/admin/users/_components/edit.user.form.tsx b/src/app/(dashboard)/admin/users/_components/edit.user.form.tsx deleted file mode 100644 index 312221d..0000000 --- a/src/app/(dashboard)/admin/users/_components/edit.user.form.tsx +++ /dev/null @@ -1,154 +0,0 @@ -"use client" - -import { zodResolver } from "@hookform/resolvers/zod" -import { useRouter } from "next/navigation" -import { useMemo } from "react" -import type { UseFormRegisterReturn } from "react-hook-form" -import { useForm } from "react-hook-form" -import { toast } from "sonner" -import { updateUserAction } from "@/actions/user.actions" -import { - SubmitButton, - type SubmitButtonCopy, -} from "@/components/forms/submitButton" -import { - buildUpdateUserSchema, - type UpdateUserFormType, - type UserSchemaCopy, -} from "@/schemas/user.schema" -import type { UserWithoutPassword } from "@/services/user.service" - -import type { UserFormCopy, UserRoleCopy } from "./user.copy" - -export default function EditUserForm({ - formCopy, - schemaCopy, - roleLabels, - submitButtonCopy, - user, -}: { - formCopy: UserFormCopy - schemaCopy: UserSchemaCopy - roleLabels: UserRoleCopy - submitButtonCopy: SubmitButtonCopy - user: UserWithoutPassword -}) { - const router = useRouter() - const schema = useMemo(() => buildUpdateUserSchema(schemaCopy), [schemaCopy]) - const { - register, - handleSubmit, - setError, - formState: { errors, isSubmitting, isSubmitSuccessful }, - } = useForm({ - resolver: zodResolver(schema), - defaultValues: { - id: user.id, - name: user.name, - email: user.email, - role: user.role, - isActive: user.isActive, - }, - }) - - const onSubmit = async (formData: UpdateUserFormType) => { - const response = await updateUserAction(formData) - - if (response?.errors) { - Object.entries(response.errors).forEach(([fieldName, messages]) => { - messages.forEach((message: string) => { - setError(fieldName as keyof UpdateUserFormType, { - type: "server", - message, - }) - toast.error(message) - }) - }) - return - } - - if (response?.success) { - toast.success(response.message) - router.push("/admin/users") - } - } - - return ( -
- - - -
- - -
- - - {formCopy.updateSubmit} - - - ) -} - -function UserTextInput({ - error, - id, - label, - placeholder, - register, - type = "text", -}: { - error?: string - id: string - label: string - placeholder: string - register: UseFormRegisterReturn - type?: string -}) { - return ( -
- - - {error &&

{error}

} -
- ) -} diff --git a/src/app/(dashboard)/admin/users/_components/new.user.form.tsx b/src/app/(dashboard)/admin/users/_components/new.user.form.tsx deleted file mode 100644 index 251eef5..0000000 --- a/src/app/(dashboard)/admin/users/_components/new.user.form.tsx +++ /dev/null @@ -1,246 +0,0 @@ -"use client" - -import { zodResolver } from "@hookform/resolvers/zod" -import { useRouter } from "next/navigation" -import { useMemo } from "react" -import type { UseFormRegisterReturn } from "react-hook-form" -import { useForm } from "react-hook-form" -import { toast } from "sonner" -import { createPersonUserAction } from "@/actions/person.actions" -import { - SubmitButton, - type SubmitButtonCopy, -} from "@/components/forms/submitButton" -import { PERSON_DEPARTMENTS } from "@/lib/constants" -import { - buildUnifiedCreateSchema, - type UnifiedCreateFormType, - type UnifiedSchemaCopy, -} from "@/schemas/user.schema" - -import { - formatPersonDepartment, - type PersonDepartmentCopy, - type PersonFallbackCopy, - type UserFormCopy, - type UserRoleCopy, -} from "./user.copy" - -export default function NewUserForm({ - formCopy, - schemaCopy, - roleLabels, - departmentCopy, - fallbackCopy, - submitButtonCopy, -}: { - formCopy: UserFormCopy - schemaCopy: UnifiedSchemaCopy - roleLabels: UserRoleCopy - departmentCopy: PersonDepartmentCopy - fallbackCopy: PersonFallbackCopy - submitButtonCopy: SubmitButtonCopy -}) { - const router = useRouter() - const schema = useMemo( - () => buildUnifiedCreateSchema(schemaCopy), - [schemaCopy], - ) - const { - register, - handleSubmit, - watch, - setError, - formState: { errors, isSubmitting, isSubmitSuccessful }, - } = useForm({ - resolver: zodResolver(schema), - defaultValues: { - role: "STAFF", - isActive: true, - }, - }) - - const selectedRole = watch("role") - const showPassword = selectedRole !== "NO_USER" - - const onSubmit = async (formData: UnifiedCreateFormType) => { - const response = await createPersonUserAction(formData) - - if (response?.errors) { - Object.entries(response.errors).forEach(([fieldName, messages]) => { - messages.forEach((message: string) => { - setError(fieldName as keyof UnifiedCreateFormType, { - type: "server", - message, - }) - toast.error(message) - }) - }) - return - } - - if (response?.success) { - toast.success(response.message) - router.push("/admin/users") - } - } - - return ( -
- - - - - - - {showPassword && ( - - )} - - {formCopy.createSubmit} - - - ) -} - -function UserTextInput({ - error, - id, - label, - placeholder, - register, - type = "text", -}: { - error?: string - id: string - label: string - placeholder: string - register: UseFormRegisterReturn - type?: string -}) { - return ( -
- - - {error &&

{error}

} -
- ) -} - -function RoleSelect({ - register, - roleLabel, - roleLabels, -}: { - register: UseFormRegisterReturn - roleLabel: string - roleLabels: UserRoleCopy -}) { - return ( -
- - -
- ) -} - -function DepartmentSelect({ - error, - formCopy, - departmentCopy, - fallbackCopy, - register, -}: { - error?: string - formCopy: UserFormCopy - departmentCopy: PersonDepartmentCopy - fallbackCopy: PersonFallbackCopy - register: UseFormRegisterReturn -}) { - return ( -
- - - {error &&

{error}

} -
- ) -} diff --git a/src/app/(dashboard)/admin/users/_components/reset.user.password.form.tsx b/src/app/(dashboard)/admin/users/_components/reset.user.password.form.tsx deleted file mode 100644 index ff90b5a..0000000 --- a/src/app/(dashboard)/admin/users/_components/reset.user.password.form.tsx +++ /dev/null @@ -1,97 +0,0 @@ -"use client" - -import { zodResolver } from "@hookform/resolvers/zod" -import { useMemo } from "react" -import { useForm } from "react-hook-form" -import { toast } from "sonner" -import { resetUserPasswordAction } from "@/actions/user.actions" -import { - SubmitButton, - type SubmitButtonCopy, -} from "@/components/forms/submitButton" -import { - buildResetUserPasswordSchema, - type ResetUserPasswordFormType, - type UserSchemaCopy, -} from "@/schemas/user.schema" - -import type { UserResetPasswordCopy } from "./user.copy" - -export default function ResetUserPasswordForm({ - formCopy, - schemaCopy, - submitButtonCopy, - userId, -}: { - formCopy: UserResetPasswordCopy - schemaCopy: UserSchemaCopy - submitButtonCopy: SubmitButtonCopy - userId: string -}) { - const schema = useMemo( - () => buildResetUserPasswordSchema(schemaCopy), - [schemaCopy], - ) - const { - register, - handleSubmit, - reset, - setError, - formState: { errors, isSubmitting, isSubmitSuccessful }, - } = useForm({ - resolver: zodResolver(schema), - defaultValues: { - id: userId, - }, - }) - - const onSubmit = async (formData: ResetUserPasswordFormType) => { - const response = await resetUserPasswordAction(formData) - - if (response?.errors) { - Object.entries(response.errors).forEach(([fieldName, messages]) => { - messages.forEach((message: string) => { - setError(fieldName as keyof ResetUserPasswordFormType, { - type: "server", - message, - }) - toast.error(message) - }) - }) - return - } - - if (response?.success) { - toast.success(response.message) - reset({ id: userId, password: "" }) - } - } - - return ( -
- -
- - - {errors.password && ( -

{errors.password.message}

- )} -
- - {formCopy.submit} - -
- ) -} diff --git a/src/app/(dashboard)/admin/users/_components/user.copy.ts b/src/app/(dashboard)/admin/users/_components/user.copy.ts deleted file mode 100644 index b4a28c6..0000000 --- a/src/app/(dashboard)/admin/users/_components/user.copy.ts +++ /dev/null @@ -1,35 +0,0 @@ -import type { Dictionary } from "@/i18n/dictionaries" - -export type UserFormCopy = Dictionary["admin"]["users"]["form"] -export type UserRoleCopy = Dictionary["admin"]["users"]["roles"] -export type UserStatusCopy = Dictionary["admin"]["users"]["status"] -export type UserFallbackCopy = Dictionary["admin"]["users"]["fallback"] -export type UserResetPasswordCopy = - Dictionary["admin"]["users"]["resetPassword"] -export type PersonDepartmentCopy = - Dictionary["inventory"]["people"]["departments"] -export type PersonFallbackCopy = Dictionary["inventory"]["people"]["fallback"] - -export function formatUserRole( - role: string, - roleCopy: UserRoleCopy, - fallbackCopy: UserFallbackCopy, -): string { - return role in roleCopy - ? roleCopy[role as keyof UserRoleCopy] - : fallbackCopy.unknownRole -} - -export function formatPersonDepartment( - department: string | null | undefined, - departmentCopy: PersonDepartmentCopy, - fallbackCopy: PersonFallbackCopy, -): string { - if (!department) { - return fallbackCopy.unknownDepartment - } - - return department in departmentCopy - ? departmentCopy[department as keyof PersonDepartmentCopy] - : fallbackCopy.unknownDepartment -} diff --git a/src/app/(dashboard)/admin/users/new/page.tsx b/src/app/(dashboard)/admin/users/new/page.tsx deleted file mode 100644 index 676a555..0000000 --- a/src/app/(dashboard)/admin/users/new/page.tsx +++ /dev/null @@ -1,5 +0,0 @@ -import { redirect } from "next/navigation" - -export default function NewUserPage() { - redirect("/people/new") -} diff --git a/src/app/(dashboard)/admin/users/page.tsx b/src/app/(dashboard)/admin/users/page.tsx deleted file mode 100644 index 69a304f..0000000 --- a/src/app/(dashboard)/admin/users/page.tsx +++ /dev/null @@ -1,5 +0,0 @@ -import { redirect } from "next/navigation" - -export default function UsersPage() { - redirect("/people") -} diff --git a/src/proxy.ts b/src/proxy.ts index 27c8f5b..174eb78 100644 --- a/src/proxy.ts +++ b/src/proxy.ts @@ -20,7 +20,7 @@ export default auth((req) => { return NextResponse.redirect(newUrl) } - if (!isAdmin(session) && pathname.startsWith("/admin")) { + if (!isAdmin(session) && pathname.startsWith("/people")) { return NextResponse.redirect(new URL("/forbidden", req.nextUrl.origin)) }