From e84e852f7087565460676cc3b71589f3385a3670 Mon Sep 17 00:00:00 2001
From: Alice <alice.poggioli@hotmail.fr>
Date: Fri, 15 Nov 2019 09:36:05 +0100
Subject: [PATCH] WIP: add a sib-like component.

---
 src/includes/head.pug                     |  1 +
 src/includes/mentor/components/header.pug |  2 +-
 src/includes/mentor/resources/detail.pug  |  4 +-
 src/scripts/coopstarter.js                | 45 +++++++++++++++++++++++
 4 files changed, 50 insertions(+), 2 deletions(-)

diff --git a/src/includes/head.pug b/src/includes/head.pug
index 907cc5fc..5568626e 100644
--- a/src/includes/head.pug
+++ b/src/includes/head.pug
@@ -6,6 +6,7 @@ head
     script(type="module" src="https://unpkg.com/@startinblox/router")
     script(type="module" src="https://unpkg.com/@startinblox/oidc")
     script(type="module" src="https://unpkg.com/@startinblox/component-conversation")
+    script(type="module" src="../../sib-like/sib-like.js")
     script(src="https://kit.fontawesome.com/48014d2af3.js")
     script(src="https://code.jquery.com/jquery-3.3.1.min.js" integrity="sha256-FgpCb/KJQlLNfOu91ta32o/NMZxltwRo8QtmkMRdAu8=" crossorigin="anonymous")
     script(src="/scripts/coopstarter.js")
diff --git a/src/includes/mentor/components/header.pug b/src/includes/mentor/components/header.pug
index 3e7b4a46..7ee39240 100644
--- a/src/includes/mentor/components/header.pug
+++ b/src/includes/mentor/components/header.pug
@@ -18,7 +18,7 @@
             )
             
             .dropdownWrapper
-                sib-display#mentor-account-picture.dropdownLabel(
+                sib-display#user-account-picture.dropdownLabel(
                     bind-user
                     fields='account.picture',
                     widget-account.picture='cs-account-picture'
diff --git a/src/includes/mentor/resources/detail.pug b/src/includes/mentor/resources/detail.pug
index cf20f6a7..b87da116 100644
--- a/src/includes/mentor/resources/detail.pug
+++ b/src/includes/mentor/resources/detail.pug
@@ -120,5 +120,7 @@ include ../../components/widgets
   )
 
 
-
+sib-like(
+  bind-resources,
+)
   
\ No newline at end of file
diff --git a/src/scripts/coopstarter.js b/src/scripts/coopstarter.js
index 0312cf45..e72e1e96 100644
--- a/src/scripts/coopstarter.js
+++ b/src/scripts/coopstarter.js
@@ -343,6 +343,24 @@ function initFileUpload(){
   }
 }
 
+/**
+ * Manage the like sender hidden form
+ * @param {*} event 
+ * @param {*} userWhoSubmitLike 
+ * @param {*} formSenderLike 
+ */
+function fillSenderLike(userWhoSubmitLike, formSenderLike, currentResource) {
+  console.log("userWhoSubmitLike", userWhoSubmitLike)
+  console.log("formSenderLike", formSenderLike)
+  formSenderLike.querySelector(
+    "input[name='sender']"
+  ).value = JSON.stringify({ "@id": userWhoSubmitLike });
+  formSenderLike.querySelector(
+    "input[name='receiver_object_id']"
+  ).value = JSON.stringify({ "@id": currentResource });
+  receiver_object_id
+}
+
 /**
  * For entrepreneur dashboard only :
  * As we cannot have multiple imbricated filtering with the native sib-display, we manage it manually.
@@ -545,6 +563,8 @@ jQuery(document).ready(function($) {
   linkDatasetToField(userAccountDataSrc, "improvement-dialog-form", "reviewer");
   linkDatasetToField(userAccountDataSrc, "refusal-dialog-form", "reviewer");
   linkDatasetToField(userAccountDataSrc, "change_status_request", "reviewer");
+  // linkDatasetToField(userAccountDataSrc, "add-like", "sender");
+  // linkDatasetToField(userAccountDataSrc, "add-dislike", "sender");
 
   //On form submission, we sometime have to refresh a list.
   refreshList("resource-creation-form", "resources-history");
@@ -600,6 +620,31 @@ jQuery(document).ready(function($) {
       var userWhoSubmitReport =  userAccountDataSrc.dataset.src
       var formBrokenLink = document.getElementById("report-broken-link-mentor");
       fillReportBrokenLinkForm(event, userWhoSubmitReport, formBrokenLink);
+      //fillSenderLike(userWhoSubmitReport, formSenderLike, currentResource);
+      document.querySelector('sib-auth').getUser().then(data=> {
+        console.log("Le user : ", data )
+        let targetForm = document.getElementById('add-like');
+        targetForm.addEventListener("populate", event => {
+          let resourceField = targetForm.querySelector(`input[name="sender"]`);
+          let test = targetForm.querySelector(`input[name="receiver_object_id"]`)
+          let sibLike= document.querySelector('sib-like')
+          test.value = JSON.stringify({ "@id": sibLike.dataset.src })
+          test.setAttribute(
+              "value",
+              JSON.stringify({ "@id": sibLike.dataset.src })
+            );
+          if (resourceField) {
+            resourceField.value = JSON.stringify(data);
+            resourceField.setAttribute(
+              "value",
+              JSON.stringify(data)
+            );
+            console.log("l'id_receveer : ", test.value )
+          }
+        });
+        
+      })
+      
     });
 
     //Init the form file behaviour
-- 
GitLab