Commit 8debcdc3 authored by Mat's avatar Mat
Browse files

Offre/ajaxDisplay, formulaire javascript pour basculer le statut des offres en préparation

parent ac2223b0
......@@ -4,6 +4,7 @@ namespace APIBundle\Controller;
use APIBundle\Form\OffreAPIType;
use APIBundle\Form\OffreStatusAPIType;
use PotageBundle\Entity\Offre;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
......@@ -70,4 +71,33 @@ class OffreAPIController extends MasterAPIController
return $this->api($offres);
}
/**
* @param Request $request
* @param $id
* @return \Symfony\Component\HttpFoundation\JsonResponse
* @throws \Doctrine\ORM\NonUniqueResultException
*/
public function updateStatusAction(Request $request, $id)
{
$em = $this->getDoctrine()->getManager();
$offre = $em->getRepository('PotageBundle:Offre')
->findOneForUpdate($id);
$form = $this->createForm(OffreStatusAPIType::class, $offre);
$form->handleRequest($request);
if ($offre === null) {
return $this->api('Not found', Response::HTTP_NOT_FOUND);
}
if ($form->isSubmitted() && $form->isValid())
{
$em->flush();
return $this->api($offre);
}
return $this->api($form, Response::HTTP_BAD_REQUEST);
}
}
<?php
namespace APIBundle\Form;
use PotageBundle\Form\Offre\OffreStatusType;
use Symfony\Component\OptionsResolver\OptionsResolver;
class OffreStatusAPIType extends OffreStatusType
{
/**
* @param OptionsResolver $resolver
*/
public function configureOptions(OptionsResolver $resolver)
{
parent::configureOptions($resolver);
$resolver->setDefault('csrf_protection', false);
// TODO temporaire, le temps de régler les validations au niveau des Asserts et des FormType
$resolver->setDefault('attr', array('novalidate' => true));
}
}
......@@ -66,3 +66,11 @@ api_offre_read_by_status:
_controller: APIBundle:OffreAPI:readByStatus
methods: [ GET ]
api_offre_update_status:
path: /offre/{id}
requirements:
id: \d+
defaults:
_controller: APIBundle:OffreAPI:updateStatus
methods: [ POST ]
<?php
namespace PotageBundle\Form\Offre;
use PotageBundle\Entity\Offre;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
use Symfony\Component\Form\Extension\Core\Type\SubmitType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;
class OffreStatusType extends AbstractType
{
/**
* @param FormBuilderInterface $builder
* @param array $options
*/
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder->add('status', ChoiceType::class, array(
'label' => 'Statut',
'choices' => array(
'En préparation' => 'draft',
'En cours' => 'current',
'Terminé' => 'closed',
//'Poubelle' => 'trash',
),
'label_attr' => array('class' => 'col-form-label'),
'attr' => array(
'class' => 'form-control form-control-sm',
),
));
$builder->add('sauver', SubmitType::class, array(
'label' => 'Changer',
'attr' => array('class' => 'btn btn-dark mb-2')
));
}
/**
* @param OptionsResolver $resolver
*/
public function configureOptions(OptionsResolver $resolver)
{
parent::configureOptions($resolver);
$resolver->setDefault('data_class', Offre::class);
$resolver->setDefault('attr', array(
'class' => 'formulaire formulaire_offre_status',
));
}
/**
* @return null|string
*/
public function getBlockPrefix()
{
return 'offre_status';
}
}
......@@ -40,4 +40,21 @@ class OffreRepository extends \Doctrine\ORM\EntityRepository
return $qb->getQuery()->getResult();
}
/**
* @param $id
* @return mixed
* @throws \Doctrine\ORM\NonUniqueResultException
*/
public function findOneForUpdate($id)
{
$qb = $this->createQueryBuilder('o')
->where('o.id = :id')
->andWhere('o.status = :status ')
->setParameters(array(
':id'=> $id,
':status' => 'draft'
));
return $qb->getQuery()->getOneOrNullResult();
}
}
......@@ -49,14 +49,6 @@
<div class="row">
<aside class="col-3">
<h3>Titre</h3>
<ul>
<li>
item 1
</li>
<li>
item 2
</li>
</ul>
</aside>
{% endblock %}
......@@ -104,6 +96,7 @@
let form = document.querySelector('form[name=offre]');
let titleForm = document.querySelector('#form-offre');
/*
* Recharge la table
*/
......@@ -143,14 +136,49 @@
let td5 = document.createElement('td');
let listStatus = {
'draft': 'préparation',
'current': 'en cours',
'closed': 'terminé',
'trash': 'corbeille'
'draft': 'En préparation',
'current': 'En cours',
'closed': 'Terminé',
//'trash': 'poubelle'
};
for(let statut in listStatus) {
for (let statut in listStatus) {
if (statut === json[i].status) {
td5.textContent = listStatus[statut];
if (json[i].status === 'draft')
{
/*
* Changement de statut
*/
let editForm = displayEditForm();
editForm.addEventListener('change', function(e)
{
sleep(500).then(() => {
let ask = confirm("L'offre contient XX légumes.\n" +
"Attention, une fois celle-ci publiée,\n" +
"vous ne pourrez plus revenir en arrière !\n" +
"Êtes-vous sûr de vouloir changer son statut ?"
);
if (ask === true)
{
AJAX('POST',
Routing.generate('api_offre_update_status', {'id': json[i].id }),
function() {
reloadTables();
},
editForm
);
}
else {
reloadTableOffre('draft');
}
});
});
td5.appendChild(editForm);
}
else {
td5.textContent = listStatus[statut];
}
}}
tr.appendChild(td5);
......@@ -159,6 +187,44 @@
});
}
/*
* Affiche le formulaire d'édition
*/
function displayEditForm()
{
let editForm = document.createElement('form');
editForm.setAttribute('name', 'offre_status');
editForm.setAttribute('method', 'post');
editForm.setAttribute('novalidate', '1');
let divForm = document.createElement('div');
divForm.setAttribute('id', 'offre_status');
divForm.setAttribute('novalidate', 'novalidate');
editForm.appendChild(divForm);
let selectForm = document.createElement('select');
selectForm.setAttribute('id', 'offre_status_status');
selectForm.setAttribute('name', 'offre_status[status]');
selectForm.classList.add('form-control', 'form-control-sm');
divForm.appendChild(selectForm);
let optionValue = {
'draft': 'En préparation',
'current': 'Publier',
//'trash': 'Poubelle'
};
for (let value in optionValue)
{
let optionForm = document.createElement('option');
optionForm.setAttribute('value', value);
optionForm.textContent = optionValue[value];
selectForm.appendChild(optionForm);
}
return editForm;
}
/*
* Recharge les tables
*/
......@@ -172,10 +238,6 @@
reloadTables();
/*
* Ajouter une nouvelle offre
*/
......@@ -184,12 +246,11 @@
createBtn.addEventListener('click', function()
{
titleForm.textContent = 'Nouvelle offre';
//form.querySelector('#offre_ended_at').value = null;
//form.querySelector('#offre_status').value = 'draft';
submitURL = Routing.generate('api_offre_create');
});
});
/*
* Soumission du formulaire
*/
......@@ -203,6 +264,5 @@
});
</script>
{% endblock %}
......@@ -39,6 +39,11 @@ function decompte(seconds, callback)
}
decompteLoop();
}
function sleep (time) {
return new Promise((resolve) => setTimeout(resolve, time));
}
/*
* F O N C T I O N S A J A X
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment