From 1c3460ffebec7cb4b8e26e2b9deb9a9927c5b960 Mon Sep 17 00:00:00 2001 From: Maxime Lalo Date: Thu, 27 Jul 2023 11:53:04 +0200 Subject: [PATCH] :sparkles: Sending votes working --- package-lock.json | 42 +++++------ package.json | 2 +- .../SuperAdmin/LiveVotes/LiveVotes.ts | 44 ++++++++++++ src/front/Components/Layouts/Login/index.tsx | 2 +- .../Layouts/Users/UserInformations/index.tsx | 72 +++++++++++-------- 5 files changed, 110 insertions(+), 52 deletions(-) create mode 100644 src/front/Api/LeCoffreApi/SuperAdmin/LiveVotes/LiveVotes.ts diff --git a/package-lock.json b/package-lock.json index 4632a1db..fbfd6fde 100644 --- a/package-lock.json +++ b/package-lock.json @@ -22,7 +22,7 @@ "eslint-config-next": "13.2.4", "form-data": "^4.0.0", "jwt-decode": "^3.1.2", - "le-coffre-resources": "git@github.com:smart-chain-fr/leCoffre-resources.git#v2.58", + "le-coffre-resources": "git@github.com:smart-chain-fr/leCoffre-resources.git#v2.66", "next": "13.2.4", "prettier": "^2.8.7", "react": "18.2.0", @@ -328,9 +328,9 @@ } }, "node_modules/@eslint-community/regexpp": { - "version": "4.6.1", - "resolved": "https://registry.npmjs.org/@eslint-community/regexpp/-/regexpp-4.6.1.tgz", - "integrity": "sha512-O7x6dMstWLn2ktjcoiNLDkAGG2EjveHL+Vvc+n0fXumkJYAcSqcVYKtwDU+hDZ0uDUsnUagSYaZrOLAYE8un1A==", + "version": "4.6.2", + "resolved": "https://registry.npmjs.org/@eslint-community/regexpp/-/regexpp-4.6.2.tgz", + "integrity": "sha512-pPTNuaAG3QMH+buKyBIGJs3g/S5y0caxw0ygM3YyE6yJFySwiGGSzA+mM3KJ8QQvzeLh3blwgSonkFjgQdxzMw==", "engines": { "node": "^12.0.0 || ^14.0.0 || >=16.0.0" } @@ -441,22 +441,22 @@ } }, "node_modules/@mui/core-downloads-tracker": { - "version": "5.14.1", - "resolved": "https://registry.npmjs.org/@mui/core-downloads-tracker/-/core-downloads-tracker-5.14.1.tgz", - "integrity": "sha512-mIa1WmDmNr1LoupV1Rbxt9bTFKMbIn10RHG1bnZ/FJCkAYpuU/D4n+R+ttiycgcZNngU++zyh/OQeJblzbQPzg==", + "version": "5.14.2", + "resolved": "https://registry.npmjs.org/@mui/core-downloads-tracker/-/core-downloads-tracker-5.14.2.tgz", + "integrity": "sha512-x+c/MgDL1t/IIy5lDbMlrDouFG5DYZbl3DP4dbbuhlpPFBnE9glYwmJEee/orVHQpOPwLxCAIWQs+2DKSaBVWQ==", "funding": { "type": "opencollective", "url": "https://opencollective.com/mui" } }, "node_modules/@mui/material": { - "version": "5.14.1", - "resolved": "https://registry.npmjs.org/@mui/material/-/material-5.14.1.tgz", - "integrity": "sha512-WtsgYuageTunLfxH3Ri+o1RuQTFImtRHxMcVNyD0Hhd2/znjW6KODNz0XfjvLRnNCAynBxZNiflcoIBW40h9PQ==", + "version": "5.14.2", + "resolved": "https://registry.npmjs.org/@mui/material/-/material-5.14.2.tgz", + "integrity": "sha512-TgNR4/YRL11RifsnMWNhITNCkGJYVz20SCvVJBBoU5Y/KhUNSSJxjDpEB8VrnY+sUsV0NigLCkHZJglfsiS3Pw==", "dependencies": { "@babel/runtime": "^7.22.6", "@mui/base": "5.0.0-beta.8", - "@mui/core-downloads-tracker": "^5.14.1", + "@mui/core-downloads-tracker": "^5.14.2", "@mui/system": "^5.14.1", "@mui/types": "^7.2.4", "@mui/utils": "^5.14.1", @@ -2386,9 +2386,9 @@ } }, "node_modules/execa": { - "version": "7.1.1", - "resolved": "https://registry.npmjs.org/execa/-/execa-7.1.1.tgz", - "integrity": "sha512-wH0eMf/UXckdUYnO21+HDztteVv05rq2GXksxT4fCGeHkBhw1DROXh40wcjMcRqDOWE7iPJ4n3M7e2+YFP+76Q==", + "version": "7.2.0", + "resolved": "https://registry.npmjs.org/execa/-/execa-7.2.0.tgz", + "integrity": "sha512-UduyVP7TLB5IcAQl+OzLyLcS/l32W/GLg+AhHJ+ow40FOk2U3SAllPwR44v4vmdFwIWqpdwxxpQbF1n5ta9seA==", "dependencies": { "cross-spawn": "^7.0.3", "get-stream": "^6.0.1", @@ -3340,7 +3340,7 @@ } }, "node_modules/le-coffre-resources": { - "resolved": "git+ssh://git@github.com/smart-chain-fr/leCoffre-resources.git#5c506e8b49401240b8decee77f9b7e34694f490a", + "resolved": "git+ssh://git@github.com/smart-chain-fr/leCoffre-resources.git#e7916d516fe434c93ef13e765d8d63a7ce3c56b2", "license": "MIT", "dependencies": { "class-transformer": "^0.5.1", @@ -3361,9 +3361,9 @@ } }, "node_modules/libphonenumber-js": { - "version": "1.10.37", - "resolved": "https://registry.npmjs.org/libphonenumber-js/-/libphonenumber-js-1.10.37.tgz", - "integrity": "sha512-Z10PCaOCiAxbUxLyR31DNeeNugSVP6iv/m7UrSKS5JHziEMApJtgku4e9Q69pzzSC9LnQiM09sqsGf2ticZnMw==" + "version": "1.10.38", + "resolved": "https://registry.npmjs.org/libphonenumber-js/-/libphonenumber-js-1.10.38.tgz", + "integrity": "sha512-4NjVXVUmpZ9Zsqq6FXa2+MKI+KAI3tOqA0pxXgXGluhpj4ge5didmbWJpMBqGB3AVGv1SnEtKdGTbxjSEG1kCQ==" }, "node_modules/lines-and-columns": { "version": "1.2.4", @@ -4847,9 +4847,9 @@ } }, "node_modules/tslib": { - "version": "2.6.0", - "resolved": "https://registry.npmjs.org/tslib/-/tslib-2.6.0.tgz", - "integrity": "sha512-7At1WUettjcSRHXCyYtTselblcHl9PJFFVKiCAy/bY97+BPZXSQ2wbq0P9s8tK2G7dFQfNnlJnPAiArVBVBsfA==" + "version": "2.6.1", + "resolved": "https://registry.npmjs.org/tslib/-/tslib-2.6.1.tgz", + "integrity": "sha512-t0hLfiEKfMUoqhG+U1oid7Pva4bbDPHYfJNiB7BiIjRkj1pyC++4N3huJfqY6aRH6VTB0rvtzQwjM4K6qpfOig==" }, "node_modules/tsutils": { "version": "3.21.0", diff --git a/package.json b/package.json index 44aac912..a2f6f2f1 100644 --- a/package.json +++ b/package.json @@ -24,7 +24,7 @@ "eslint-config-next": "13.2.4", "form-data": "^4.0.0", "jwt-decode": "^3.1.2", - "le-coffre-resources": "git@github.com:smart-chain-fr/leCoffre-resources.git#v2.58", + "le-coffre-resources": "git@github.com:smart-chain-fr/leCoffre-resources.git#v2.66", "next": "13.2.4", "prettier": "^2.8.7", "react": "18.2.0", diff --git a/src/front/Api/LeCoffreApi/SuperAdmin/LiveVotes/LiveVotes.ts b/src/front/Api/LeCoffreApi/SuperAdmin/LiveVotes/LiveVotes.ts new file mode 100644 index 00000000..dfbb54e1 --- /dev/null +++ b/src/front/Api/LeCoffreApi/SuperAdmin/LiveVotes/LiveVotes.ts @@ -0,0 +1,44 @@ +import { Appointment, Vote } from "le-coffre-resources/dist/SuperAdmin"; + +import BaseSuperAdmin from "../BaseSuperAdmin"; + +// TODO Type get query params -> Where + inclue + orderby +export interface IGetLiveVotessparams { + where?: {}; + include?: {}; + select?: {}; +} + +export type IPostLiveVotesParams = { + appointment: Appointment; +}; + +export default class LiveVotes extends BaseSuperAdmin { + private static instance: LiveVotes; + private readonly baseURl = this.namespaceUrl.concat("/live-votes"); + + private constructor() { + super(); + } + + public static getInstance() { + if (!this.instance) { + return new this(); + } else { + return this.instance; + } + } + + /** + * @description : Create a LiveVotes + */ + public async post(body: IPostLiveVotesParams): Promise { + const url = new URL(this.baseURl); + try { + return await this.postRequest(url, body); + } 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 b4c10d64..977c4138 100644 --- a/src/front/Components/Layouts/Login/index.tsx +++ b/src/front/Components/Layouts/Login/index.tsx @@ -18,7 +18,7 @@ export default function Login() { const redirectUserOnConnection = useCallback(() => { async function getUser() { try { - await UserStore.instance.connect(process.env["NEXT_PUBLIC_ADMIN_ID"] as string); + await UserStore.instance.connect("jelkvelknvlkn"); await JwtService.getInstance().checkJwt(); router.push(Module.getInstance().get().modules.pages.Folder.props.path); } catch (e) { diff --git a/src/front/Components/Layouts/Users/UserInformations/index.tsx b/src/front/Components/Layouts/Users/UserInformations/index.tsx index 2abaca93..dbb3f4b2 100644 --- a/src/front/Components/Layouts/Users/UserInformations/index.tsx +++ b/src/front/Components/Layouts/Users/UserInformations/index.tsx @@ -1,13 +1,14 @@ import WarningIcon from "@Assets/images/warning.png"; import OfficeRoles from "@Front/Api/LeCoffreApi/Admin/OfficeRoles/OfficeRoles"; +import LiveVotes from "@Front/Api/LeCoffreApi/SuperAdmin/LiveVotes/LiveVotes"; import Users from "@Front/Api/LeCoffreApi/SuperAdmin/Users/Users"; import SelectField, { IOption } from "@Front/Components/DesignSystem/Form/SelectField"; import Confirm from "@Front/Components/DesignSystem/Modal/Confirm"; import Switch from "@Front/Components/DesignSystem/Switch"; import Typography, { ITypo, ITypoColor } from "@Front/Components/DesignSystem/Typography"; import DefaultUserDashboard from "@Front/Components/LayoutTemplates/DefaultUserDashboard"; -import Toasts from "@Front/Stores/Toasts"; -import User from "le-coffre-resources/dist/Notary"; +import User, { Appointment, Vote } from "le-coffre-resources/dist/SuperAdmin"; +import { EAppointmentStatus, EVote } from "le-coffre-resources/dist/SuperAdmin/Appointment"; import Image from "next/image"; import { useRouter } from "next/router"; import { useCallback, useEffect, useState } from "react"; @@ -30,6 +31,13 @@ export default function UserInformations(props: IProps) { const [isAdminChecked, setIsAdminChecked] = useState(false); const [isAdminModalOpened, setIsAdminModalOpened] = useState(false); + const [currentAppointment, setCurrentAppointment] = useState(null); + + useEffect(() => { + if (!userSelected) return; + setCurrentAppointment(userSelected?.appointment?.find((appointment) => appointment.status === EAppointmentStatus.OPEN) ?? null); + }, [userSelected]); + /** Functions for the admin modal */ const openAdminModal = () => { setIsAdminModalOpened(true); @@ -74,21 +82,21 @@ export default function UserInformations(props: IProps) { const handleSuperAdminModalAccepted = useCallback(async () => { if (!userSelected) return; - if (superAdminModalType === "add") { - Toasts.getInstance().open({ - title: "Vote attribué", - text: "Vous avez voté pour attribuer le titre de Super Admin à " + userSelected.contact?.first_name, - }); - // add super admin - } else { - Toasts.getInstance().open({ - title: "Vote attribué", - text: "Vous avez voté pour supprimer le titre de Super Admin à " + userSelected.contact?.first_name, - }); - // remove super admin - } + + let vote = Vote.hydrate({ + appointment: + currentAppointment ?? + Appointment.hydrate({ + targeted_user: User.hydrate({ + uid: userSelected.uid, + }), + choice: superAdminModalType === "add" ? EVote.NOMINATE : EVote.DISMISS, + }), + }); + + await LiveVotes.getInstance().post(vote); setIsSuperAdminModalOpened(false); - }, [userSelected, superAdminModalType]); + }, [userSelected, currentAppointment, superAdminModalType]); /** Reset switch state when userSelect change */ useEffect(() => { @@ -107,10 +115,13 @@ export default function UserInformations(props: IProps) { office_role: true, office_membership: true, role: true, + appointment: true, + votes: true, }, }); if (!user) return; + console.log(user); const roles = await OfficeRoles.getInstance().get(); if (!roles) return; setAvailableRoles(roles.map((role) => ({ value: role.uid, label: role.name }))); @@ -180,22 +191,25 @@ export default function UserInformations(props: IProps) {
-
-
- warning -
-
-
- 1/3 + {currentAppointment && ( +
+
+ warning
-
- - Vous avez voté pour attribuer le titre de Super Admin. Il manque 2 votes pour que le - collaborateur se voit attribuer le titre. - +
+
+ 1/3 +
+
+ + {currentAppointment.choice === EVote.NOMINATE + ? `Vous avez voté pour attribuer le titre de Super Admin. Il manque 2 votes pour que le collaborateur se voit attribuer le titre.` + : `Vous avez voté pour retirer le titre de Super Admin. Il manque 2 votes pour que le collaborateur se voit retirer le titre.`} + +
-
+ )}