From c0327f9607cd362013d80d7ac7654c3d983f1bce Mon Sep 17 00:00:00 2001 From: Mathis Date: Wed, 22 May 2024 17:03:14 +0200 Subject: [PATCH] Refactored register services and enhanced user profile functionality A new RequestService was introduced to standardize the HTTP requests, and the register service was refactored to use it. Improvements were made to the handling of user profile data along with error handling and display. New components were created for displaying the user profile, and changes to existing components were made to accommodate the service and data handling updates. --- .idea/workspace.xml | 25 ++++++++--- src/app/profile/page.tsx | 76 ++++++++++++++++++++++++++------ src/components/profile.tsx | 26 +++++++++++ src/components/register-form.tsx | 13 ++++-- src/services/account.service.ts | 39 ++++++++++++++++ src/services/register.service.ts | 22 --------- src/services/request.service.ts | 71 +++++++++++++++++++++++++++++ src/types/userdata.type.ts | 16 +++++++ 8 files changed, 243 insertions(+), 45 deletions(-) create mode 100644 src/components/profile.tsx create mode 100644 src/services/account.service.ts delete mode 100644 src/services/register.service.ts create mode 100644 src/services/request.service.ts create mode 100644 src/types/userdata.type.ts diff --git a/.idea/workspace.xml b/.idea/workspace.xml index 4b9d338..6dac8e2 100644 --- a/.idea/workspace.xml +++ b/.idea/workspace.xml @@ -5,8 +5,14 @@ + + + + - + + + @@ -120,6 +134,7 @@ - \ No newline at end of file diff --git a/src/app/profile/page.tsx b/src/app/profile/page.tsx index 6304530..59e9e1c 100644 --- a/src/app/profile/page.tsx +++ b/src/app/profile/page.tsx @@ -1,19 +1,67 @@ -import React from "react"; +'use client' +import React, {useEffect, useState} from "react"; import {Button} from "@/components/ui/button"; -import {KeyRound, Rss, UserRoundPlus} from "lucide-react"; +import Link from "next/link"; +import {AccountService} from "@/services/account.service"; +import {IUserDataProfile} from "@/types/userdata.type"; +import {TriangleAlert} from "lucide-react"; +import {Profile} from "@/components/profile"; export default function ProfilePage() { - return ( -
-
-

OnlyDevs

-

A social network for you the nerdy developer !

-
- - - + const token = localStorage.getItem('token') || false + if (!token) { + return ( +
+

You should be logged to see the page.

+
+ +
-
-
- ); + + ); + } + + const [userData, setUserData] = useState(); + const [httpError, setHttpError] = useState() + useEffect(() => { + const fetchData = async () => { + try { + const response = await AccountService.getUserData(); + if (typeof response === 'string') { + setHttpError(response) + return + } + setUserData(response); + } catch (error) { + console.error(error); + } + }; + fetchData(); + }, []); + + if (httpError) { + return ( +
+
+ +

{`${httpError}`}

+
+
+ ); + } + + if (userData?.email) { + return ( +
+ +
+ ); + } + return ( +
+

Loading...

+
+ ); } \ No newline at end of file diff --git a/src/components/profile.tsx b/src/components/profile.tsx new file mode 100644 index 0000000..3f13c5b --- /dev/null +++ b/src/components/profile.tsx @@ -0,0 +1,26 @@ +'use client' +import {IUserDataProfile} from "@/types/userdata.type"; + +export function Profile({asAdmin, userData}: {asAdmin?: boolean, userData: IUserDataProfile}) { + + return ( +
+
+ +
+
+
+
+

+ PP +

+
+
+ Title +
+
+
+
+
+ ) +} \ No newline at end of file diff --git a/src/components/register-form.tsx b/src/components/register-form.tsx index 6444055..abfc7ba 100644 --- a/src/components/register-form.tsx +++ b/src/components/register-form.tsx @@ -11,7 +11,7 @@ import toast, {ToastBar} from "react-hot-toast"; import {ToastBox, toastType} from "@/components/ui/toast-box"; import {zodResolver} from "@hookform/resolvers/zod" import {z} from 'zod' -import doRegister from "@/services/register.service"; +import {AccountService} from "@/services/account.service"; const Schema = z.object({ username: @@ -67,7 +67,6 @@ function ErrorLabel({message}: {message: string}) {

{message}

) - } export function RegisterForm() { @@ -84,9 +83,15 @@ export function RegisterForm() { const onSubmit: SubmitHandler = async (data) => { console.log('ping'); - const result = await doRegister(data) + const result = await AccountService.register(data) console.log(result); - //if (result.status === 'BAD') + if (!result.success) { + toast.custom() + return + } toast.custom( { + return { + headers: { + 'content-type': 'application/json', + Authorization: `Bearer ${localStorage.getItem('token')}`, + }, + validateStatus: function (status: number) { + return status < 500; // Resolve only if the status code is less than 500 + } + } + } + }, + standard: { + json: () => { + return { + headers: { + 'content-type': 'application/json' + }, + validateStatus: function (status: number) { + return status < 500; // Resolve only if the status code is less than 500 + } + } + } + } +} + +async function doAuthenticatedJsonPostReq(url:string, body:object): Promise> { + return await axios.post(url, body, AxiosConfigs.authenticated.json()) +} +async function doAuthenticatedGetReq(url: string): Promise> { + return await axios.get(url, AxiosConfigs.authenticated.json()) +} + +async function doAuthenticatedPatchReq(url: string, body: object): Promise> { + return await axios.patch(url, body, AxiosConfigs.authenticated.json()) +} + +async function doAuthenticatedDelReq(url: string): Promise> { + return await axios.delete(url, AxiosConfigs.authenticated.json()) +} + +//TODO form/multipart req + + +async function doJsonPostReq(url:string, body: object): Promise> { + return await axios.post(url, body, AxiosConfigs.standard.json()) +} +async function doJsonGetReq(url: string): Promise> { + return await axios.get(url, AxiosConfigs.standard.json()); +} + +const RequestService = { + authenticated: { + post: {json: doAuthenticatedJsonPostReq}, + patch: {json: doAuthenticatedPatchReq}, + delete: {json: doAuthenticatedDelReq}, + get: {json: doAuthenticatedGetReq} + }, + standard: { + post: {json: doJsonPostReq}, + get: {json: doJsonGetReq} + } +} + +export default RequestService; \ No newline at end of file diff --git a/src/types/userdata.type.ts b/src/types/userdata.type.ts new file mode 100644 index 0000000..445514c --- /dev/null +++ b/src/types/userdata.type.ts @@ -0,0 +1,16 @@ +export interface IUserDataProfile { + id: string; + username: string; + email: string; + displayName: string; + isAdmin: boolean; + followers: FollowsAndFollowers[]; + following: FollowsAndFollowers[]; +} + +interface FollowsAndFollowers { + id: string, + source_id: string, + target_id: string, + iat?: number +} \ No newline at end of file