From edb6a7c1fda9d267084cd6f38ed60df449f33483 Mon Sep 17 00:00:00 2001
From: Zahdi <ZahdiMohcine20@gmail.com>
Date: Wed, 26 Mar 2025 04:31:15 +0100
Subject: [PATCH] [Progress] adding the invite a co author link generation

---
 src/Controller/InvitationController.php   |   9 +-
 src/Controller/RegistrationController.php |   4 +-
 templates/wishlist/index.html.twig        | 145 +++++++++++++---------
 3 files changed, 100 insertions(+), 58 deletions(-)

diff --git a/src/Controller/InvitationController.php b/src/Controller/InvitationController.php
index 36675c95..c85534b0 100644
--- a/src/Controller/InvitationController.php
+++ b/src/Controller/InvitationController.php
@@ -31,12 +31,19 @@ final class InvitationController extends AbstractController
         
         $user = $this->getUser() ; 
 
+        $data = json_decode($request->getContent(),true) ; 
+        $wishlist_id = $data["wishlist_id"] ?? null ; 
+
+        if (!$wishlist_id) {
+            return new JsonResponse(["error"=>"Missing wishlist_id"] , Response::HTTP_BAD_REQUEST) ; 
+        }
+
         if (!$user) {
             return $this->createAccessDeniedException('User is not connected');
         } 
 
         $invitation->setInviter($user);
-        $invitation->setWishlist($entityManager->find(Wishlist::class, $request->get(key: 'wishlist_id')));
+        $invitation->setWishlist($entityManager->find(Wishlist::class, $wishlist_id));
         $entityManager->persist($invitation);
         $entityManager->flush();
 
diff --git a/src/Controller/RegistrationController.php b/src/Controller/RegistrationController.php
index 4610778b..def3e8a9 100644
--- a/src/Controller/RegistrationController.php
+++ b/src/Controller/RegistrationController.php
@@ -21,7 +21,7 @@ class RegistrationController extends AbstractController
         $form->handleRequest($request);
 
         if ($form->isSubmitted() ) {
-            
+            dump($form->isValid()) ;
             if ($form->isValid()){
                 // Hacher le mot de passe
                 $hashedPassword = $passwordHasher->hashPassword($user, plainPassword: $form->get('password')->getData());
@@ -36,10 +36,12 @@ class RegistrationController extends AbstractController
             }
 
             if (!$form->isValid()) {
+                dump("I am here ") ; 
                 $errors = [];
                 foreach ($form->getErrors(true) as $error) {
                     $errors[] = $error->getMessage();
                 }
+                dump($errors);
                 return $this->render('registration/register.html.twig', [
                     'registrationForm' => $form->createView(),
                     'formErrors' => $errors,
diff --git a/templates/wishlist/index.html.twig b/templates/wishlist/index.html.twig
index f775e2ad..62fa8599 100644
--- a/templates/wishlist/index.html.twig
+++ b/templates/wishlist/index.html.twig
@@ -4,33 +4,34 @@
 
 {% block body  %} 
 
-
 <style> 
-.modal {
-    display: none; /* Hide by default */
-    position: fixed;
+/* Popup box styling */
+.popup-box {
+    display: none; /* Hidden by default */
+    position: fixed; /* Fixed relative to the viewport */
     z-index: 1000;
-    left: 50%;
-    top: 50%;
-    transform: translate(-50%, -50%);
-    background-color: white;
-    padding: 20px;
-    border-radius: 8px;
-    box-shadow: 0px 0px 10px rgba(0, 0, 0, 0.2);
-}
-
-.modal-content {
-    text-align: center;
+    background-color: #fff;
+    padding: 15px;
+    border: 1px solid #ccc;
+    border-radius: 6px;
+    box-shadow: 0 2px 8px rgba(0, 0, 0, 0.15);
+    width: 220px;
 }
 
-.close {
+/* Close button styling */
+.popup-box .close {
     position: absolute;
-    top: 10px;
-    right: 20px;
-    font-size: 24px;
+    top: 5px;
+    right: 8px;
+    font-size: 18px;
     cursor: pointer;
 }
 
+/* Center content inside the popup */
+.popup-box .content {
+    text-align: center;
+    margin-top: 20px;
+}
 </style>
 
 <header>
@@ -44,7 +45,9 @@
 <div class="container">
     {% for wishlist in wishlists %}
     <div class="wishlist">
-        <h2>{{ wishlist.name }}</h2>
+        <a href="{{ path('app_wishlist_show', { 'id': wishlist.id }) }}">
+            {{ wishlist.name }}
+        </a>
         <div class="wishlist-items">
             {% for item in wishlist.items %}
             <div class="wishlist-item">📷</div>
@@ -61,76 +64,106 @@
     {% endfor %}
 </div>
 
-<!-- Modal Popup -->
-<div id="share-modal" class="modal">
-    <div class="modal-content">
-        <span class="close">&times;</span>
-        <h3>Share Wishlist</h3>
+<!-- Popup Box -->
+<div id="popup-box" class="popup-box">
+    <span class="close">&times;</span>
+    <h4>Share Wishlist</h4>
+    <div class="content">
         <button id="invite-co-worker">Invite a Co-Worker</button>
+        <br><br>
         <button id="share-wishes">Share Your Wishes!</button>
     </div>
 </div>
-
-
 <script>
 document.addEventListener("DOMContentLoaded", function () {
     const shareButtons = document.querySelectorAll(".share-btn");
-    const modal = document.getElementById("share-modal");
-    const closeBtn = document.querySelector(".close");
-    
-    let selectedWishlistId = null; // Store the selected wishlist ID
+    const popupBox = document.getElementById("popup-box");
+    const closeBtn = popupBox.querySelector(".close");
+    let selectedWishlistId = null;
 
+    // When a share button is clicked, position and show the popup.
     shareButtons.forEach(button => {
         button.addEventListener("click", function () {
             selectedWishlistId = this.dataset.wishlistId;
-            modal.style.display = "block";
+            // Reset invite button text and stored URL.
+            const inviteButton = document.getElementById("invite-co-worker");
+            inviteButton.textContent = "Invite a Co-Worker";
+            inviteButton.removeAttribute("data-invite-url");
+
+            const rect = this.getBoundingClientRect();
+            // Adjust popup position as needed.
+            popupBox.style.left = `${rect.left}px`;
+            popupBox.style.top = `${rect.top + rect.height + 8}px`;
+            popupBox.style.display = "block";
         });
     });
 
+    // Close popup when the close button is clicked.
     closeBtn.addEventListener("click", function () {
-        modal.style.display = "none";
+        popupBox.style.display = "none";
     });
 
+    // Optionally close the popup if clicking outside.
     window.addEventListener("click", function (event) {
-        if (event.target === modal) {
-            modal.style.display = "none";
+        if (!popupBox.contains(event.target) && !event.target.classList.contains("share-btn")) {
+            popupBox.style.display = "none";
         }
     });
 
-    // Handle Invite a Co-Worker Click
-    document.getElementById("invite-co-worker").addEventListener("click", async function () {
-        try {
-            const response = await fetch("{{path('app_invitation_new')}}", {
+    // Invite button click: first click fetches URL, second click copies it.
+    document.getElementById("invite-co-worker").addEventListener("click", function () {
+        const inviteButton = this;
+        // Check if URL is already generated.
+        if (inviteButton.hasAttribute("data-invite-url")) {
+            // URL exists, copy it.
+            const url = inviteButton.getAttribute("data-invite-url");
+            navigator.clipboard.writeText(url)
+                .then(() => {
+                    alert("URL was copied to clipboard successfully!");
+                    popupBox.style.display = "none";
+                })
+                .catch(error => {
+                    console.error("Clipboard write failed:", error);
+                    alert("Failed to copy URL to clipboard.");
+                });
+        } else {
+            // Fetch the invite URL.
+            fetch("{{ path('app_invitation_new') }}", {
                 method: "POST",
                 headers: {
                     "Content-Type": "application/json"
                 },
                 body: JSON.stringify({ wishlist_id: selectedWishlistId })
+            })
+            .then(response => {
+                if (!response.ok) {
+                    throw new Error("Network response was not ok");
+                }
+                return response.json();
+            })
+            .then(responseData => {
+                if (responseData.joint_creation_URL) {
+                    // Save URL in a data attribute and change button text.
+                    inviteButton.setAttribute("data-invite-url", responseData.joint_creation_URL);
+                    inviteButton.textContent = "Copy Invite URL";
+                    alert("Invite URL generated. Click the button again to copy it.");
+                } else {
+                    alert("Failed to generate invite link.");
+                }
+            })
+            .catch(error => {
+                console.error("Error:", error);
+                alert("Failed to send invitation.");
             });
-
-            if (!response.ok) {
-                throw new Error("Network response was not ok");
-            }
-
-            const responseData = await response.json();
-            navigator.clipboard.writeText(responseData.url).then(() => {
-                alert("URL was copied to clipboard successfully!");
-            }).catch(err => {
-                alert("Error copying URL to clipboard: " +  err);
-            });
-        } catch (error) {
-            console.error("Error:", error);
-            alert("Failed to send invitation.");
         }
     });
 
-    // Handle Share Your Wishes Click
+    // Share Your Wishes action placeholder.
     document.getElementById("share-wishes").addEventListener("click", function () {
         alert("Sharing your wishes! (Implement your sharing logic here)");
     });
 });
-
-
 </script>
 
+
 {% endblock %}
-- 
GitLab