From 6687d3566643f6eb5d36f283e1f9ead0b2d2c200 Mon Sep 17 00:00:00 2001
From: pfleu <fleutotp@gmail.com>
Date: Thu, 8 Jun 2023 09:18:57 +0200
Subject: [PATCH] Ajout Fos JS Routing. Suite reorder entry

---
 composer.json                              |  1 +
 config/bundles.php                         |  1 +
 config/routes/fos_js_routing.yaml          |  2 ++
 src/Controller/EntryController.php         | 11 +++++++++++
 templates/base.html.twig                   |  2 ++
 templates/entry/_entryAttributes.html.twig | 21 +++++++++++++++++++--
 6 files changed, 36 insertions(+), 2 deletions(-)
 create mode 100644 config/routes/fos_js_routing.yaml

diff --git a/composer.json b/composer.json
index 820066e..626d792 100644
--- a/composer.json
+++ b/composer.json
@@ -13,6 +13,7 @@
         "doctrine/doctrine-bundle": "^2.7",
         "doctrine/doctrine-migrations-bundle": "^3.2",
         "doctrine/orm": "^2.13",
+        "friendsofsymfony/jsrouting-bundle": "^2.8",
         "justinrainbow/json-schema": "^5.2",
         "league/oauth2-server-bundle": "^0.4.0",
         "nelmio/api-doc-bundle": "^4.10",
diff --git a/config/bundles.php b/config/bundles.php
index c457161..e810766 100644
--- a/config/bundles.php
+++ b/config/bundles.php
@@ -18,4 +18,5 @@ return [
     SymfonyCasts\Bundle\VerifyEmail\SymfonyCastsVerifyEmailBundle::class => ['all' => true],
     Vich\UploaderBundle\VichUploaderBundle::class => ['all' => true],
     SymfonyCasts\Bundle\ResetPassword\SymfonyCastsResetPasswordBundle::class => ['all' => true],
+    FOS\JsRoutingBundle\FOSJsRoutingBundle::class => ['all' => true],
 ];
diff --git a/config/routes/fos_js_routing.yaml b/config/routes/fos_js_routing.yaml
new file mode 100644
index 0000000..79d25e2
--- /dev/null
+++ b/config/routes/fos_js_routing.yaml
@@ -0,0 +1,2 @@
+fos_js_routing:
+    resource: "@FOSJsRoutingBundle/Resources/config/routing/routing-sf4.xml"
diff --git a/src/Controller/EntryController.php b/src/Controller/EntryController.php
index 8955781..f52b0c3 100644
--- a/src/Controller/EntryController.php
+++ b/src/Controller/EntryController.php
@@ -327,4 +327,15 @@ class EntryController extends AppBaseController
         }
     }
 
+    /**
+     * @Route("/{id}/reorder", name="app_entry_reorder", methods={"GET"}, options={"expose" = true})
+     */
+    public function reorder(Request $request, Entry $entry): Response
+    {
+        $serialized = $request->get('serialized');
+dump($serialized);die();
+
+        return $this->render('entry/show.html.twig', ['id' => $entry->getId()]);
+    }
+
 }
diff --git a/templates/base.html.twig b/templates/base.html.twig
index b92e34e..28ca96c 100644
--- a/templates/base.html.twig
+++ b/templates/base.html.twig
@@ -22,6 +22,8 @@
         <script src="https://cdn.jsdelivr.net/npm/select2@4.1.0-rc.0/dist/js/select2.min.js"></script>
         {% include "js_translations.html.twig" %}
         <script src="{{ asset('assets/js/jquery.mjs.nestedSortable.js') }}"></script>
+        <script src="{{ asset('bundles/fosjsrouting/js/router.min.js') }}"></script>
+        <script src="{{ path('fos_js_routing_js', { callback: 'fos.Router.setData' }) }}"></script>
         <script src="{{ asset('assets/js/app.js') }}"></script>
     {% endblock %}
 </head>
diff --git a/templates/entry/_entryAttributes.html.twig b/templates/entry/_entryAttributes.html.twig
index 95dd746..bd79683 100644
--- a/templates/entry/_entryAttributes.html.twig
+++ b/templates/entry/_entryAttributes.html.twig
@@ -110,8 +110,25 @@
         });
 
         $("#serialize").click(function(e) {
-            e.preventDefault();console.log($('ol.sortable').nestedSortable('serialize'));
-            // $("#output").text($('ol.sortable').nestedSortable('serialize'));
+            e.preventDefault();
+console.log($('ol.sortable').nestedSortable('serialize'));
+            var $overlay = $('#overlay').show();
+            var serialized = $('ol.sortable').nestedSortable('serialize');
+            var target = $('#entryAttributes');
+            var entryId = {{ entry.id }}
+            var url = Routing.generate('app_entry_reorder', {id: entryId, serialized: serialized});
+
+            $.ajax({
+                url: url,
+                method: "GET",
+                data: {serialized: serialized},
+                success: function(res) {
+                    target.replaceWith($(res).find('#entryAttributes'));
+                },
+                complete: function () {
+                    $overlay.hide();
+                }
+            });
         });
 
     });
-- 
GitLab