From 489cdd968540236093b8fcce163b04fdf10b2b9e Mon Sep 17 00:00:00 2001 From: Vins Date: Wed, 9 Aug 2023 11:39:12 +0200 Subject: [PATCH] Page select folders unmocked --- src/front/Api/Auth/franceConnect/Customer.ts | 38 +++++++++++++ .../Api/LeCoffreApi/Customer/Files/Files.ts | 2 +- .../LeCoffreApi/Customer/Folders/Folders.ts | 57 +++++++++++++++++++ src/front/Components/Layouts/Login/index.tsx | 19 +++++++ .../Components/Layouts/SelectFolder/index.tsx | 22 ++++++- src/front/Services/JwtService/JwtService.ts | 4 +- src/front/Stores/UserStore.ts | 18 ++++++ 7 files changed, 155 insertions(+), 5 deletions(-) create mode 100644 src/front/Api/Auth/franceConnect/Customer.ts create mode 100644 src/front/Api/LeCoffreApi/Customer/Folders/Folders.ts diff --git a/src/front/Api/Auth/franceConnect/Customer.ts b/src/front/Api/Auth/franceConnect/Customer.ts new file mode 100644 index 00000000..b7c60ba5 --- /dev/null +++ b/src/front/Api/Auth/franceConnect/Customer.ts @@ -0,0 +1,38 @@ +import BaseApiService from "@Front/Api/BaseApiService"; + +export default class Customer extends BaseApiService { + private static instance: Customer; + private readonly baseURl = this.getBaseUrl().concat("/france-connect/customer"); + + private constructor() { + super(); + } + + public static getInstance() { + if (!this.instance) { + return new Customer(); + } else { + return this.instance; + } + } + + public async login(email: string) { + const url = new URL(this.baseURl.concat("/login/").concat(email)); + try { + return await this.postRequest(url); + } catch (err) { + this.onError(err); + return Promise.reject(err); + } + } + + public async refreshToken(refreshToken: string): Promise<{ accessToken: string }> { + const url = new URL(this.baseURl.concat("/refresh-token")); + try { + return await this.postRequest(url, {}, refreshToken); + } catch (err) { + this.onError(err); + return Promise.reject(err); + } + } +} diff --git a/src/front/Api/LeCoffreApi/Customer/Files/Files.ts b/src/front/Api/LeCoffreApi/Customer/Files/Files.ts index 4cf6ebd4..6ae12cf0 100644 --- a/src/front/Api/LeCoffreApi/Customer/Files/Files.ts +++ b/src/front/Api/LeCoffreApi/Customer/Files/Files.ts @@ -1,4 +1,4 @@ -import { File } from "le-coffre-resources/dist/SuperAdmin"; +import { File } from "le-coffre-resources/dist/Customer"; import BaseCustomer from "../BaseCustomer"; // TODO Type get query params -> Where + inclue + orderby diff --git a/src/front/Api/LeCoffreApi/Customer/Folders/Folders.ts b/src/front/Api/LeCoffreApi/Customer/Folders/Folders.ts new file mode 100644 index 00000000..27bfd731 --- /dev/null +++ b/src/front/Api/LeCoffreApi/Customer/Folders/Folders.ts @@ -0,0 +1,57 @@ +import { type OfficeFolder } from "le-coffre-resources/dist/Customer"; + +import BaseCustomer from "../BaseCustomer"; + +// TODO Type get query params -> Where + inclue + orderby +export interface IGetFoldersParams { + q?: { + select?: {}; + where?: {}; + include?: {}; + }; +} + +export default class Folders extends BaseCustomer { + private static instance: Folders; + private readonly baseURl = this.namespaceUrl.concat("/folders"); + + private constructor() { + super(); + } + + public static getInstance() { + if (!this.instance) { + return new this(); + } else { + return this.instance; + } + } + + /** + * @description : Get all folders + */ + public async get(q: IGetFoldersParams): Promise { + const url = new URL(this.baseURl); + Object.entries(q).forEach(([key, value]) => url.searchParams.set(key, JSON.stringify(value))); + try { + return await this.getRequest(url); + } catch (err) { + this.onError(err); + return Promise.reject(err); + } + } + + /** + * @description : Get a folder by uid + */ + public async getByUid(uid: string, q?: any): Promise { + const url = new URL(this.baseURl.concat(`/${uid}`)); + if (q) Object.entries(q).forEach(([key, value]) => url.searchParams.set(key, JSON.stringify(value))); + try { + return await this.getRequest(url); + } catch (err) { + this.onError(err); + return Promise.reject(err); + } + } +} diff --git a/src/front/Components/Layouts/Login/index.tsx b/src/front/Components/Layouts/Login/index.tsx index 977c4138..7bbcf405 100644 --- a/src/front/Components/Layouts/Login/index.tsx +++ b/src/front/Components/Layouts/Login/index.tsx @@ -15,6 +15,7 @@ import LandingImage from "./landing-connect.jpeg"; export default function Login() { const router = useRouter(); + const redirectUserOnConnection = useCallback(() => { async function getUser() { try { @@ -28,6 +29,21 @@ export default function Login() { getUser(); }, [router]); + + const redirectCustomerOnConnection = useCallback(() => { + async function getUser() { + try { + await UserStore.instance.connectCustomer("antoine.bernard@outlook.com"); + await JwtService.getInstance().checkJwt(); + router.push(Module.getInstance().get().modules.pages.Folder.pages.Select.props.path); + } catch (e) { + console.error(e); + } + } + + getUser(); + }, [router]); + return (
@@ -38,6 +54,9 @@ export default function Login() { +
Vous n'arrivez pas à vous connecter ?
diff --git a/src/front/Components/Layouts/SelectFolder/index.tsx b/src/front/Components/Layouts/SelectFolder/index.tsx index 441461ef..7b5ebfd6 100644 --- a/src/front/Components/Layouts/SelectFolder/index.tsx +++ b/src/front/Components/Layouts/SelectFolder/index.tsx @@ -1,4 +1,4 @@ -import Folders from "@Front/Api/LeCoffreApi/SuperAdmin/Folders/Folders"; +import Folders from "@Front/Api/LeCoffreApi/Customer/Folders/Folders"; import BlockList, { IBlock } from "@Front/Components/DesignSystem/BlockList"; import Typography, { ITypo } from "@Front/Components/DesignSystem/Typography"; import DefaultDoubleSidePage from "@Front/Components/LayoutTemplates/DefaultDoubleSidePage"; @@ -8,6 +8,7 @@ import { useCallback, useEffect, useState } from "react"; import LandingImage from "../Login/landing-connect.jpeg"; import classes from "./classes.module.scss"; +import JwtService from "@Front/Services/JwtService/JwtService"; export default function SelectFolder() { const [folders, setFolders] = useState([]); @@ -15,8 +16,23 @@ export default function SelectFolder() { useEffect(() => { async function getFolders() { - const folders = await Folders.getInstance().get({}); - setFolders(folders.slice(0, 3)); + const jwt = JwtService.getInstance().decodeJwt(); + if (!jwt) return; + console.log(jwt); + + const folders = await Folders.getInstance().get({ + q: { + where: { + customers: { + some: { + uid: jwt.userId, + }, + }, + }, + }, + }); + setFolders(folders); + console.log(folders); } getFolders(); diff --git a/src/front/Services/JwtService/JwtService.ts b/src/front/Services/JwtService/JwtService.ts index 66db5c7c..f753ae6f 100644 --- a/src/front/Services/JwtService/JwtService.ts +++ b/src/front/Services/JwtService/JwtService.ts @@ -38,7 +38,9 @@ export default class JwtService { */ public async checkJwt() { const decodedToken = this.decodeJwt(); - if(!decodedToken) return; + console.log(decodedToken); + + if (!decodedToken) return; const now = Math.floor(Date.now() / 1000); diff --git a/src/front/Stores/UserStore.ts b/src/front/Stores/UserStore.ts index c4bf3a1a..06c70182 100644 --- a/src/front/Stores/UserStore.ts +++ b/src/front/Stores/UserStore.ts @@ -1,6 +1,7 @@ "use client"; import User from "@Front/Api/Auth/IdNot/User"; +import Customer from "@Front/Api/Auth/franceConnect/Customer"; import CookieService from "@Front/Services/CookieService/CookieService"; import EventEmitter from "@Front/Services/EventEmitter"; @@ -33,6 +34,23 @@ export default class UserStore { return true; } + public async connectCustomer(email: string) { + try { + //call connection function + const customer: any = await Customer.getInstance().login(email); + + //Save tokens in cookies + CookieService.getInstance().setCookie("leCoffreAccessToken", customer.accessToken); + CookieService.getInstance().setCookie("leCoffreRefreshToken", customer.refreshToken); + + this.event.emit("connection", this.accessToken); + } catch (error) { + console.error(error); + return false; + } + return true; + } + public async disconnect() { try { //Remove tokens from cookies