From b515ae3e67ebceac7ce6c151006e86d86c3e7ee3 Mon Sep 17 00:00:00 2001 From: Vyacheslav1557 Date: Tue, 25 Feb 2025 23:04:08 +0500 Subject: [PATCH] feat(user): add CreateUser button&page --- src/app/users/new/page.tsx | 1 + src/plain-pages/new-user/index.ts | 1 + src/plain-pages/new-user/page.tsx | 19 +++++++ src/plain-pages/new-user/ui.tsx | 85 +++++++++++++++++++++++++++++++ src/plain-pages/users/ui.tsx | 4 ++ src/shared/api/ms-auth.ts | 27 +++++++++- 6 files changed, 136 insertions(+), 1 deletion(-) create mode 100644 src/app/users/new/page.tsx create mode 100644 src/plain-pages/new-user/index.ts create mode 100644 src/plain-pages/new-user/page.tsx create mode 100644 src/plain-pages/new-user/ui.tsx diff --git a/src/app/users/new/page.tsx b/src/app/users/new/page.tsx new file mode 100644 index 0000000..d4a97f0 --- /dev/null +++ b/src/app/users/new/page.tsx @@ -0,0 +1 @@ +export {NewUserPage as default, generateMetadata} from "@/plain-pages/new-user"; \ No newline at end of file diff --git a/src/plain-pages/new-user/index.ts b/src/plain-pages/new-user/index.ts new file mode 100644 index 0000000..e62e814 --- /dev/null +++ b/src/plain-pages/new-user/index.ts @@ -0,0 +1 @@ +export {NewUserPage, generateMetadata} from "./page"; \ No newline at end of file diff --git a/src/plain-pages/new-user/page.tsx b/src/plain-pages/new-user/page.tsx new file mode 100644 index 0000000..7c276db --- /dev/null +++ b/src/plain-pages/new-user/page.tsx @@ -0,0 +1,19 @@ +"use server"; + +import {ClientPage} from "./ui"; +import {Metadata} from "next"; + +const generateMetadata = async (): Promise => { + return { + title: 'Добавить пользователя', + description: '', + }; +} + +const Page = async () => { + return ( + + ) +} + +export {Page as NewUserPage, generateMetadata}; \ No newline at end of file diff --git a/src/plain-pages/new-user/ui.tsx b/src/plain-pages/new-user/ui.tsx new file mode 100644 index 0000000..1c278e6 --- /dev/null +++ b/src/plain-pages/new-user/ui.tsx @@ -0,0 +1,85 @@ +"use client"; + +import {AppShell, AppShellHeader, AppShellMain, Button, PasswordInput, Stack, TextInput, Title} from "@mantine/core"; +import {Header} from "@/components/header"; +import React from "react"; +import {useForm} from "@mantine/form"; +import {useRouter} from "next/navigation"; +import {useMutation} from "@tanstack/react-query"; +import {CreateUser, Credentials} from "@/shared/api"; +import {CreateUserResponse} from "../../../proto/user/v1/api"; + +const Page = () => { + const router = useRouter(); + + const form = useForm({ + initialValues: { + username: "", + password: "" + }, + }); + + const mutation = useMutation({ + mutationFn: async (credentials: Credentials) => { + return await CreateUser(credentials) + }, + onSuccess: async (data: CreateUserResponse) => { + await router.push(`/users/${data.id}`) + }, + onError: (error) => { + form.clearErrors(); + + form.setFieldError("username", "Что-то пошло не так. Попробуйте позже.") + }, + retry: false + }); + + const onSubmit = (event) => { + event.preventDefault() + mutation.mutate(form.getValues()) + } + + return ( + + +
+ + +
+ + Добавить пользователя + + + + + + +
+
+ + ); +}; + +export {Page as ClientPage}; diff --git a/src/plain-pages/users/ui.tsx b/src/plain-pages/users/ui.tsx index 3eaf65b..7da7981 100644 --- a/src/plain-pages/users/ui.tsx +++ b/src/plain-pages/users/ui.tsx @@ -6,6 +6,7 @@ import { AppShellAside, AppShellHeader, AppShellMain, + Button, Pagination, Stack, Table, @@ -99,6 +100,9 @@ const ClientPage = (props: Props) => { + diff --git a/src/shared/api/ms-auth.ts b/src/shared/api/ms-auth.ts index b363e38..7d1333f 100644 --- a/src/shared/api/ms-auth.ts +++ b/src/shared/api/ms-auth.ts @@ -5,7 +5,7 @@ import {decode} from "jsonwebtoken"; import {cookies} from "next/headers"; import {authApi} from "./config"; -type Credentials = { +export type Credentials = { username: string, password: string } @@ -163,6 +163,7 @@ export const GetMe = async () => { if (session === undefined) { throw new Error("Session id not found"); + // redirect("/login"); } const options: AxiosRequestConfig = { @@ -173,6 +174,10 @@ export const GetMe = async () => { const token = new JWTWithPermissions(decode(session.value) as JWT); + // if (token.exp < Date.now() / 1000) { + // redirect("/login"); + // } + const response = await authApi.getUser(token.user_id, options); return response.data; @@ -195,5 +200,25 @@ export const GetUsers = async (page: number, pageSize: number) => { const response = await authApi.listUsers(page, pageSize, options); + return response.data; +} + +export const CreateUser = async (credentials: Credentials) => { + const cookieStore = await cookies(); + + const session = cookieStore.get(CookieName); + + if (session === undefined) { + throw new Error("Session id not found"); + } + + const options: AxiosRequestConfig = { + headers: { + 'Authorization': "Bearer " + session.value + } + }; + + const response = await authApi.createUser(credentials, options); + return response.data; } \ No newline at end of file