浏览代码

Scripts changemet état en js pur

François Drouhard 1 年之前
父节点
当前提交
dff34e3a4f
共有 4 个文件被更改,包括 55 次插入16 次删除
  1. 3 1
      assets/app.js
  2. 40 0
      assets/js/switchEtat.js
  3. 3 0
      assets/js/switchViewVanilla.js
  4. 9 15
      src/Controller/VideothequePersonnelleController.php

+ 3 - 1
assets/app.js

@@ -19,13 +19,15 @@ import './js/filtre';
 //import './js/switchView';
 import './js/colorModeBootstrap'
 import { switchView } from './js/switchViewVanilla';
+import { switchEtat } from './js/switchEtat';
 
 const sv = switchView("#switchview");
+const se = switchEtat ('[data-fonction="switch"]');
 
 jQuery(function() {
     $('.add-another-collection-widget').addCollection();
 
-    $('[data-fonction="switch"]').switchEtat();
+    //$('[data-fonction="switch"]').switchEtat();
 
     $('[data-toggle="star-filter"]').filtreParNote();
 

+ 40 - 0
assets/js/switchEtat.js

@@ -0,0 +1,40 @@
+export const switchEtat = (selector) => {
+    const buttons = document.querySelectorAll(selector);
+    /** @type (Element) */
+    for (const button of buttons) {
+        button.addEventListener("click", (event) => {
+            event.preventDefault();
+            reqSwitch(button);
+        })
+    }
+
+    function reqSwitch(button) {
+        const icone = button.querySelector('i');
+        const id = button.dataset.content;
+        const state1 = button.dataset.iconeActif;
+        const state2 = button.dataset.iconeInactif;
+        const path = button.dataset.path;
+
+        const req = fetch(
+            path, {
+                method: "PATCH",
+                body: JSON.stringify({"id_film": id})
+            }
+        );
+
+        req
+            .then((response) => {
+                if (!response.ok) {
+                    throw new Error(`Erreur Http ${response.status}`);
+                }
+                return response.json();
+            })
+            .then((json) => {
+                if (icone.className === state1) {
+                    icone.className = state2;
+                } else {
+                    icone.className = state1;
+                }
+            })
+    }
+}

+ 3 - 0
assets/js/switchViewVanilla.js

@@ -1,5 +1,8 @@
 export function switchView(idSelector) {
     const button = document.querySelector(idSelector);
+    if (!button) {
+        return
+    }
     button.addEventListener("click", (event) => {
         event.preventDefault();
         change();

+ 9 - 15
src/Controller/VideothequePersonnelleController.php

@@ -14,7 +14,7 @@ use Symfony\Component\HttpFoundation\Response;
 
 class VideothequePersonnelleController extends AbstractController
 {
-	#[Route("/maliste/", name:"videothequepersonnelle_maliste")]
+	#[Route("/maliste", name:"videothequepersonnelle_maliste")]
 	public function maListeAction (Request $request, FilmRepository $repo, OptionsManager $options): Response
     {
         $films = $repo->findTousFavoritesByUser($this->getUser());
@@ -25,31 +25,25 @@ class VideothequePersonnelleController extends AbstractController
         ));
     }
 
-    #[Route("/maliste/modifieravoir/", name:"maliste_modifier_a_voir")]
+    #[Route("/maliste/modifieravoir", name:"maliste_modifier_a_voir", methods: ['PATCH'])]
     public function modifierFilmDansListeAction(Request $request, EntityManagerInterface $em, FilmRepository $repo, FilmManager $filmManager): Response
     {
         $result = null;
-        $film = $repo->find($request->request->get('id_film'));
-        if ($request->isXmlHttpRequest())
-        {
-            $result = $filmManager->inverseUserWantToView($film);
-            $em->flush();
-        }
+        $film = $repo->find($request->toArray()['id_film']);
+        $result = $filmManager->inverseUserWantToView($film);
+        $em->flush();
         /*$resultat = $this->get('serializer')->serialize($film, 'json');*/
         return new JsonResponse((object)['newState' => $result]);
     }
 
-    #[Route("/maliste/modifiervus/", name:"maliste_modifier_vus")]
+    #[Route("/maliste/modifiervus", name:"maliste_modifier_vus", methods: ['PATCH'])]
     public function modifierFilmVusAction(Request $request, FilmRepository $repo, EntityManagerInterface $em, FilmManager $filmManager): Response
     {
         $result = null;
 
-        $film = $repo->find($request->request->get('id_film'));
-        if ($request->isXmlHttpRequest())
-        {
-            $result = $filmManager->inverseUserWhoSeen($film);
-            $em->flush();
-        }
+        $film = $repo->find($request->toArray()['id_film']);
+        $result = $filmManager->inverseUserWhoSeen($film);
+        $em->flush();
         return new JsonResponse((object)['newState' => $result]);
     }