Feat: Refactor assignment filter to modal

This commit is contained in:
Björn Fromme
2023-10-24 11:40:40 +02:00
parent 5e35af9665
commit cb06bcd636
9 changed files with 119 additions and 89 deletions
@@ -24,12 +24,7 @@ class IndexController extends AbstractController
#[IsGranted('ROLE_ADMINISTRATIVE')]
public function index(Request $request): Response
{
$this->filterHandler->setNamespace('filter:assignment:admin');
$filterForm = $this->filterHandler->getForm();
$filterDto = $this->filterHandler->handleRequest($filterForm, $request);
if (true === $filterDto->isReset()) {
return $this->redirectToRoute('app_admin_assignment_index');
}
$filterDto = $this->filterHandler->getFilterSettings();
$query = $this
->assignmentRepository
@@ -48,7 +43,6 @@ class IndexController extends AbstractController
return $this->render('admin/assignment/index.html.twig', [
'pagination' => $pagination,
'filterForm' => $filterForm,
'filterDto' => $filterDto,
]);
}
@@ -0,0 +1,59 @@
<?php
namespace App\Controller\Common;
use App\Controller\Traits\ReturnUrlTrait;
use App\Form\AssignmentFilterType;
use App\Model\AjaxModalResponseDto;
use App\Repository\AssignmentRepository;
use App\Service\Common\AssignmentFilterHandler;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Component\Security\Http\Attribute\IsGranted;
class AssignmentFilterController extends AbstractController
{
use ReturnUrlTrait;
public function __construct(
private readonly AssignmentFilterHandler $filterHandler,
private readonly AssignmentRepository $assignmentRepository
) {
}
#[Route('/common/assignment/filter', name: 'app_common_assignment_filter')]
#[IsGranted('ROLE_USER')]
public function index(Request $request): JsonResponse
{
$response = new AjaxModalResponseDto();
$formAction = $this->generateUrl('app_common_assignment_filter', ['r' => $request->query->get('r')]);
$formData = $this->filterHandler->getFilterSettings();
$filterOptions = $this->assignmentRepository->getFilterOptions();
$formOptions = [
'action' => $formAction,
'ajax_submit' => true,
'min_date' => $filterOptions['minDate'],
'max_date' => $filterOptions['maxDate'],
'job_profiles' => $filterOptions['jobProfiles'],
'destinations' => $filterOptions['destinations'],
];
$form = $this->createForm(AssignmentFilterType::class, $formData, $formOptions);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$this->filterHandler->handleRequest($form);
$returnUrl = $this->getReturnUrl($request, 'app_admin_assignment_index');
$response->setCloseAndRedirect($returnUrl);
} else {
$response->setContent($this->renderView('common/assignment_filter.html.twig', [
'filterForm' => $form,
'filterDto' => $form->getData(),
]));
}
return $this->json($response);
}
}
+1 -7
View File
@@ -34,12 +34,7 @@ class IndexController extends AbstractController
// Validate teamer data to show missing data right away
$errors = $this->validator->validate($teamer, null, ['profile_preflight']);
$this->filterHandler->setNamespace('filter:assignment:teamer');
$filterForm = $this->filterHandler->getForm();
$filterDto = $this->filterHandler->handleRequest($filterForm, $request);
if (true === $filterDto->isReset()) {
return $this->redirectToRoute('app_teamer_index');
}
$filterDto = $this->filterHandler->getFilterSettings();
$query = $this
->assignmentRepository
@@ -60,7 +55,6 @@ class IndexController extends AbstractController
'teamer' => $teamer,
'errors' => $errors,
'pagination' => $pagination,
'filterForm' => $filterForm,
'filterDto' => $filterDto,
]);
}
+8 -36
View File
@@ -2,15 +2,11 @@
namespace App\Service\Common;
use App\Entity\Assignment;
use App\Entity\Destination;
use App\Entity\JobProfile;
use App\Form\AssignmentFilterType;
use App\Model\AssignmentFilterDto;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Component\Form\FormFactoryInterface;
use Symfony\Component\Form\FormInterface;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\RequestStack;
use Symfony\Component\HttpFoundation\Session\SessionInterface;
@@ -20,8 +16,7 @@ class AssignmentFilterHandler
public function __construct(
private readonly RequestStack $requestStack,
private readonly EntityManagerInterface $entityManager,
private readonly FormFactoryInterface $formFactory,
private readonly EntityManagerInterface $entityManager
) {
}
@@ -32,44 +27,21 @@ class AssignmentFilterHandler
return $this;
}
public function getForm(): FormInterface
public function handleRequest(FormInterface $form): AssignmentFilterDto
{
$filterDto = $this->getFilterSettings();
$assignmentRepository = $this
->entityManager
->getRepository(Assignment::class)
;
$filterOptions = $assignmentRepository->getFilterOptions();
return $this->formFactory->create(AssignmentFilterType::class, $filterDto, [
'min_date' => $filterOptions['minDate'],
'max_date' => $filterOptions['maxDate'],
'job_profiles' => $filterOptions['jobProfiles'],
'destinations' => $filterOptions['destinations'],
]);
}
public function handleRequest(
FormInterface $form,
Request $request
): AssignmentFilterDto {
$form->handleRequest($request);
$filterDto = $form->getData();
if ($form->isSubmitted() && $form->isValid()) {
if ($form->has('reset') && $form->get('reset')->isClicked()) {
$filterDto = new AssignmentFilterDto(true);
$this->getSession()->remove($this->namespace);
} elseif ($form->has('apply') && $form->get('apply')->isClicked()) {
$this->saveFilterSettings($filterDto);
}
if ($form->get('reset')->isClicked()) {
$filterDto = new AssignmentFilterDto(true);
$this->getSession()->remove($this->namespace);
} elseif ($form->get('apply')->isClicked()) {
$this->saveFilterSettings($filterDto);
}
return $filterDto;
}
private function getFilterSettings(): AssignmentFilterDto
public function getFilterSettings(): AssignmentFilterDto
{
if (null === $data = $this->getSession()->get($this->namespace)) {
return new AssignmentFilterDto();