Parcourir la source

On récupère la bonne url pour les Ajax

Sangfroid il y a 2 mois
Parent
commit
e0c6e3356b

+ 5 - 2
assets/controllers/easymde_controller.js

@@ -2,8 +2,11 @@ import { Controller } from '@hotwired/stimulus';
 
 export default class extends Controller {
     connect() {
+        const textarea = document.querySelector('textarea')
+        const basePath = textarea.dataset.url
+
         const easyMde = new EasyMDE({
-            element: document.querySelector('textarea'),
+            element: textarea,
             spellChecker: false,
             toolbar: [
                 'bold', 'italic', 'heading-2', 'heading-3', '|',
@@ -14,7 +17,7 @@ export default class extends Controller {
                 'guide'
             ],
             uploadImage:true,
-            imageUploadEndpoint: '/upload-image',
+            imageUploadEndpoint: basePath + '/upload-image',
             imagePathAbsolute: false,
             imageMaxSize: 1024 * 1024 * 100,
             imagePreviewInEditor: true,

+ 4 - 13
assets/controllers/slugger_controller.js

@@ -4,23 +4,14 @@ export default class extends Controller {
     static targets = ['title', 'slug', 'bouton']
 
     async open() {
-        if (this.slugTarget.hasAttribute('readonly')) {
-            sessionStorage.setItem('oldSlugValue', this.slugTarget.value)
-            this.slugTarget.removeAttribute('readonly');
-            this.slugTarget.value = await this.getSlug(this.titleTarget.value);
+        const basePath = this.slugTarget.dataset.url
+            this.slugTarget.value = await this.getSlug(this.titleTarget.value, basePath);
             this.slugTarget.focus();
-            this.boutonTarget.innerText = 'Annuler la modification';
-        } else {
-            this.slugTarget.value = sessionStorage.getItem('oldSlugValue')
-            sessionStorage.removeItem('oldSlugValue')
-            this.slugTarget.setAttribute('readonly', true);
-            this.boutonTarget.innerText = 'Générer le slug';
-        }
     }
 
-    async getSlug(text) {
+    async getSlug(text, basePath) {
         // Envoyer le titre au serveur pour générer le slug
-        const response = await fetch('/generate-slug', {
+        const response = await fetch(basePath + '/generate-slug', {
             method: 'POST',
             headers: {
                 'Content-Type': 'application/json',

+ 2 - 0
src/Entity/Article.php

@@ -6,6 +6,7 @@ use App\Repository\ArticleRepository;
 use Doctrine\DBAL\Types\Types;
 use Doctrine\ORM\Mapping as ORM;
 use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity;
+use Symfony\Component\Validator\Constraints as Assert;
 
 #[UniqueEntity('slug')]
 #[ORM\Entity(repositoryClass: ArticleRepository::class)]
@@ -33,6 +34,7 @@ class Article
     private ?string $state = null;
 
     #[ORM\Column(length: 100, unique: true, nullable: false)]
+    #[Assert\NotBlank()]
     private ?string $slug = null;
 
     public function __construct(User $author)

+ 10 - 2
src/Form/ArticleType.php

@@ -4,6 +4,7 @@ namespace App\Form;
 
 use App\Entity\Article;
 use App\Entity\User;
+use App\Service\BaseUrl;
 use Symfony\Bridge\Doctrine\Form\Type\EntityType;
 use Symfony\Component\Form\AbstractType;
 use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
@@ -14,6 +15,11 @@ use Symfony\Component\OptionsResolver\OptionsResolver;
 
 class ArticleType extends AbstractType
 {
+    public function __construct(protected BaseUrl $baseUrl)
+    {
+        
+    }
+
     public function buildForm(FormBuilderInterface $builder, array $options): void
     {
         $builder
@@ -24,14 +30,16 @@ class ArticleType extends AbstractType
             ])
             ->add('slug', TextType::class, [
                 'attr'  => [
-                    'readonly'  => true,
-                    'data-slugger-target'  => 'slug'
+                    //'readonly'  => true,
+                    'data-slugger-target'  => 'slug',
+                    'data-url' => $this->baseUrl->getBasePath()
                 ],
             ])
             ->add('content', TextareaType::class, [
                 'required'  => false,
                 'attr'  =>  [
                     'data-controller'   => 'easymde',
+                    'data-url' => $this->baseUrl->getBasePath()
                 ]
             ])
             ->add('publicationDate', null, [

+ 8 - 1
src/Form/PageType.php

@@ -3,6 +3,7 @@
 namespace App\Form;
 
 use App\Entity\Page;
+use App\Service\BaseUrl;
 use Symfony\Component\Form\AbstractType;
 use Symfony\Component\Form\Extension\Core\Type\TextareaType;
 use Symfony\Component\Form\FormBuilderInterface;
@@ -10,13 +11,19 @@ use Symfony\Component\OptionsResolver\OptionsResolver;
 
 class PageType extends AbstractType
 {
+    public function __construct(protected BaseUrl $baseUrl)
+    {
+        
+    }
+
     public function buildForm(FormBuilderInterface $builder, array $options): void
     {
         $builder
             ->add('title')
             ->add('content', TextareaType::class, [
                 'attr'  => [
-                    'data-controller'   => 'easymde'
+                    'data-controller'   => 'easymde',
+                    'data-url' => $this->baseUrl->getBasePath()
                 ]
             ])
         ;

+ 22 - 0
src/Service/BaseUrl.php

@@ -0,0 +1,22 @@
+<?php
+
+namespace App\Service;
+
+use Symfony\Component\HttpFoundation\RequestStack;
+
+class BaseUrl
+{
+    public function __construct(protected readonly RequestStack $requestStack)
+    {
+        
+    }
+
+    public function getBasePath(): string
+    {
+        $request = $this->requestStack->getCurrentRequest();
+
+        $basePath = $request->getBasePath();
+
+        return $basePath;
+    }
+}