From 098d42d41929285cb0918021d7f7e8ec848ad780 Mon Sep 17 00:00:00 2001 From: Maxime Lalo Date: Thu, 4 Apr 2024 14:34:23 +0200 Subject: [PATCH] :sparkles: multiple email lines working --- .../SubscriptionInvite/classes.module.scss | 15 ++++ .../Subscription/SubscriptionInvite/index.tsx | 74 +++++++++++++++++-- 2 files changed, 81 insertions(+), 8 deletions(-) diff --git a/src/front/Components/Layouts/Subscription/SubscriptionInvite/classes.module.scss b/src/front/Components/Layouts/Subscription/SubscriptionInvite/classes.module.scss index 14c07ee3..35366fac 100644 --- a/src/front/Components/Layouts/Subscription/SubscriptionInvite/classes.module.scss +++ b/src/front/Components/Layouts/Subscription/SubscriptionInvite/classes.module.scss @@ -14,6 +14,21 @@ flex-direction: column; gap: 32px; + .input-container { + display: flex; + gap: 16px; + justify-content: flex-start; + align-items: center; + > span { + width: 100%; + flex: 1; + } + } + .add-line-container { + display: flex; + justify-content: flex-start; + } + .button-container { display: flex; justify-content: center; diff --git a/src/front/Components/Layouts/Subscription/SubscriptionInvite/index.tsx b/src/front/Components/Layouts/Subscription/SubscriptionInvite/index.tsx index 36a76f9b..b36bd706 100644 --- a/src/front/Components/Layouts/Subscription/SubscriptionInvite/index.tsx +++ b/src/front/Components/Layouts/Subscription/SubscriptionInvite/index.tsx @@ -3,17 +3,40 @@ import classes from "./classes.module.scss"; import DefaultTemplate from "@Front/Components/LayoutTemplates/DefaultTemplate"; import { useRouter } from "next/router"; import TextField from "@Front/Components/DesignSystem/Form/TextField"; -import Button from "@Front/Components/DesignSystem/Button"; +import Button, { EButtonVariant } from "@Front/Components/DesignSystem/Button"; import Form from "@Front/Components/DesignSystem/Form"; import Subscriptions from "@Front/Api/LeCoffreApi/Admin/Subscriptions/Subscriptions"; +import { useCallback, useState } from "react"; +import { TrashIcon } from "@heroicons/react/24/outline"; +import PlusIcon from "@Assets/Icons/plus.svg"; export enum EForfeitType { "standard", "unlimited", } +type EmailLine = { + element: JSX.Element; + id: number; +}; + export default function SubscriptionInvite() { const router = useRouter(); const nbOfCollaborators = parseInt(router.query["nbOfCollaborators"] as string); + const [incrementalId, setIncrementalId] = useState(isNaN(nbOfCollaborators) ? 0 : nbOfCollaborators); + + const getInitialLines = () => { + const linesToGet = isNaN(nbOfCollaborators) ? 1 : nbOfCollaborators; + const lines: EmailLine[] = []; + for (let i = 0; i < linesToGet; i++) { + lines.push({ + element: , + id: i, + }); + } + return lines; + }; + + const [lines, setLines] = useState(getInitialLines()); const sendInvitations = async (e: React.FormEvent | null) => { if (!e) return; @@ -27,15 +50,32 @@ export default function SubscriptionInvite() { emails.push(element.value); } }); - const body = { - emails, - }; + try { - await Subscriptions.getInstance().post(body); + await Subscriptions.getInstance().post({ + emails, + }); } catch (e) { console.error(e); } }; + + const addLine = useCallback(() => { + const newLine: EmailLine = { + element: ( + + ), + id: incrementalId + 1, + }; + setIncrementalId(incrementalId + 1); + setLines((prev) => [...prev, newLine]); + }, [incrementalId, lines]); + + const deleteLine = (e: React.MouseEvent) => { + const lineId = parseInt(e.currentTarget.getAttribute("data-line") as string); + setLines((prev) => prev.filter((line) => line.id !== lineId)); + }; + return (
@@ -49,9 +89,27 @@ export default function SubscriptionInvite() { )}
- {Array.from({ length: nbOfCollaborators }).map((_, index) => { - return ; - })} + {lines.map((line, index) => ( +
+ {line.element} + {!nbOfCollaborators && ( + + )} +
+ ))} + {isNaN(nbOfCollaborators) && ( +
+ +
+ )}