Skip to content

Commit

Permalink
refactor: Send JSON form response after a fetch POST request
Browse files Browse the repository at this point in the history
  • Loading branch information
ambroisemaupate committed Sep 19, 2023
1 parent 0c6579e commit 75bb3d3
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ public function __construct(
}

/**
* Handles an access denied failure redirecting to home page
* Handles access denied failure redirecting to home page
*
* @param Request $request
* @param AccessDeniedException $accessDeniedException
Expand Down
12 changes: 10 additions & 2 deletions lib/Rozier/src/Controllers/Nodes/NodesAttributesController.php
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,14 @@ public function editAction(Request $request, int $nodeId, int $translationId): R
return $response;
}

$isJson =
$request->isXmlHttpRequest() ||
$request->getRequestFormat('html') === 'json' ||
\in_array(
'application/json',
$request->getAcceptableContentTypes()
);

$this->assignation['attribute_value_translation_forms'] = [];
$attributeValues = $node->getAttributeValues();
/** @var AttributeValue $attributeValue */
Expand Down Expand Up @@ -108,7 +116,7 @@ public function editAction(Request $request, int $nodeId, int $translationId): R
);
$this->publishConfirmMessage($request, $msg, $nodeSource);

if ($request->isXmlHttpRequest() || $request->getRequestFormat('html') === 'json') {
if ($isJson) {
return new JsonResponse([
'status' => 'success',
'message' => $msg,
Expand All @@ -123,7 +131,7 @@ public function editAction(Request $request, int $nodeId, int $translationId): R
/*
* Handle errors when Ajax POST requests
*/
if ($request->isXmlHttpRequest() || $request->getRequestFormat('html') === 'json') {
if ($isJson) {
return new JsonResponse([
'status' => 'fail',
'errors' => $errors,
Expand Down
6 changes: 5 additions & 1 deletion lib/Rozier/src/Controllers/Tags/TagsController.php
Original file line number Diff line number Diff line change
Expand Up @@ -380,6 +380,10 @@ public function editSettingsAction(Request $request, int $tagId): Response
]);

$form->handleRequest($request);
$isJsonRequest =
$request->isXmlHttpRequest() ||
\in_array('application/json', $request->getAcceptableContentTypes())
;

if ($form->isSubmitted()) {
if ($form->isValid()) {
Expand All @@ -403,7 +407,7 @@ public function editSettingsAction(Request $request, int $tagId): Response
/*
* Handle errors when Ajax POST requests
*/
if ($request->isXmlHttpRequest()) {
if ($isJsonRequest) {
$errors = $this->getErrorsAsArray($form);
return new JsonResponse([
'status' => 'fail',
Expand Down

0 comments on commit 75bb3d3

Please sign in to comment.