Skip to content

Commit

Permalink
refactor: Replace all $.ajax and XHR request with window.fetch
Browse files Browse the repository at this point in the history
  • Loading branch information
ambroisemaupate committed Sep 19, 2023
1 parent 3e84fb8 commit 0c6579e
Show file tree
Hide file tree
Showing 36 changed files with 906 additions and 878 deletions.
8 changes: 8 additions & 0 deletions lib/RoadizRozierBundle/config/routing/nodes.yml
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,14 @@ nodesTreePage:
requirements:
nodeId : "[0-9]+"
translationId : "[0-9]+"
nodesMainTreePage:
path: /tree/main/{translationId}
defaults:
_controller: Themes\Rozier\Controllers\Nodes\NodesTreesController::treeAction
nodeId : null
translationId : null
requirements:
translationId : "[0-9]+"
nodesBulkDeletePage:
path: /bulk-delete
defaults:
Expand Down
2 changes: 1 addition & 1 deletion lib/Rozier/src/.babelrc
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
{
"presets": ["es2015", "stage-0"],
"presets": ["env", "stage-0"],
"plugins": ["transform-runtime", "lodash"]
}
1 change: 0 additions & 1 deletion lib/Rozier/src/.editorconfig
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
# TheatreTheme editor config for contributors
# Root is false as your theme is inside Roadiz filetree
# http://editorconfig.org/
root = false

[*]
indent_style = space
Expand Down
2 changes: 1 addition & 1 deletion lib/Rozier/src/.eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ module.exports = {
},
// https://github.com/standard/standard/blob/master/docs/RULES-en.md
//extends: 'standard',
extends: ['prettier', 'plugin:prettier/recommended', 'plugin:vue/essential'],
extends: ['prettier', 'plugin:prettier/recommended', 'plugin:vue/base'],
// required to lint *.vue files
plugins: ['html', 'prettier'],
// add your custom rules here
Expand Down
8 changes: 6 additions & 2 deletions lib/Rozier/src/Controllers/Nodes/NodesSourcesController.php
Original file line number Diff line number Diff line change
Expand Up @@ -110,12 +110,16 @@ public function editSourceAction(Request $request, int $nodeId, int $translation
]
);
$form->handleRequest($request);
$isJsonRequest =
$request->isXmlHttpRequest() ||
\in_array('application/json', $request->getAcceptableContentTypes())
;

if ($form->isSubmitted()) {
if ($form->isValid() && !$this->isReadOnly) {
$this->onPostUpdate($source, $request);

if (!$request->isXmlHttpRequest()) {
if (!$isJsonRequest) {
return $this->getPostUpdateRedirection($source);
}

Expand Down Expand Up @@ -168,7 +172,7 @@ public function editSourceAction(Request $request, int $nodeId, int $translation
/*
* Handle errors when Ajax POST requests
*/
if ($request->isXmlHttpRequest()) {
if ($isJsonRequest) {
$errors = $this->formErrorSerializer->getErrorsAsArray($form);
return new JsonResponse([
'status' => 'fail',
Expand Down
11 changes: 9 additions & 2 deletions lib/Rozier/src/Controllers/SettingsController.php
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,13 @@ protected function commonSettingList(Request $request, SettingGroup $settingGrou
$this->assignation['filters'] = $listManager->getAssignation();
$settings = $listManager->getEntities();
$this->assignation['settings'] = [];
$isJson =
$request->isXmlHttpRequest() ||
$request->getRequestFormat('html') === 'json' ||
\in_array(
'application/json',
$request->getAcceptableContentTypes()
);

/** @var Setting $setting */
foreach ($settings as $setting) {
Expand All @@ -135,7 +142,7 @@ protected function commonSettingList(Request $request, SettingGroup $settingGrou
);
$this->publishConfirmMessage($request, $msg, $setting);

if ($request->isXmlHttpRequest() || $request->getRequestFormat('html') === 'json') {
if ($isJson) {
return new JsonResponse([
'status' => 'success',
'message' => $msg,
Expand All @@ -162,7 +169,7 @@ protected function commonSettingList(Request $request, SettingGroup $settingGrou
/*
* Do not publish any message, it may lead to flushing invalid form
*/
if ($request->isXmlHttpRequest() || $request->getRequestFormat('html') === 'json') {
if ($isJson) {
return new JsonResponse([
'status' => 'failed',
'errors' => $errors,
Expand Down
17 changes: 14 additions & 3 deletions lib/Rozier/src/Controllers/Tags/TagsController.php
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,10 @@ public function editTranslatedAction(Request $request, int $tagId, ?int $transla
'disabled' => $this->isReadOnly,
]);
$form->handleRequest($request);
$isJsonRequest =
$request->isXmlHttpRequest() ||
\in_array('application/json', $request->getAcceptableContentTypes())
;

if ($form->isSubmitted()) {
if ($form->isValid()) {
Expand Down Expand Up @@ -196,19 +200,26 @@ public function editTranslatedAction(Request $request, int $tagId, ?int $transla
/*
* Force redirect to avoid resending form when refreshing page
*/
return $this->getPostUpdateRedirection($tagTranslation);
if (!$isJsonRequest) {
return $this->getPostUpdateRedirection($tagTranslation);
}

return new JsonResponse([
'status' => 'success',
'errors' => [],
], Response::HTTP_PARTIAL_CONTENT);
}

/*
* Handle errors when Ajax POST requests
*/
if ($request->isXmlHttpRequest()) {
if ($isJsonRequest) {
$errors = $this->getErrorsAsArray($form);
return new JsonResponse([
'status' => 'fail',
'errors' => $errors,
'message' => $this->getTranslator()->trans('form_has_errors.check_you_fields'),
], JsonResponse::HTTP_BAD_REQUEST);
], Response::HTTP_BAD_REQUEST);
}
}
/** @var TranslationRepository $translationRepository */
Expand Down
69 changes: 34 additions & 35 deletions lib/Rozier/src/Resources/app/Lazyload.js
Original file line number Diff line number Diff line change
Expand Up @@ -153,55 +153,54 @@ export default class Lazyload {
window.cancelAnimationFrame(this.currentTimeout)
}

this.currentTimeout = window.requestAnimationFrame(() => {
this.currentTimeout = window.requestAnimationFrame(async () => {
/*
* Trigger event on window to notify open
* widgets to close.
*/
let pageChangeEvent = new CustomEvent('pagechange')
window.dispatchEvent(pageChangeEvent)

this.currentRequest = $.ajax({
url: location.href,
type: 'get',
dataType: 'html',
cache: false,
data: state.headerData,
beforeSend: (xhr) => {
xhr.setRequestHeader('X-Partial', true)
},
})
.done((data) => {
this.applyContent(data)
this.canvasLoader.hide()
let pageLoadEvent = new CustomEvent('pageload', { detail: data })
window.dispatchEvent(pageLoadEvent)
try {
const response = await fetch(location.href, {
method: 'GET',
headers: {
'X-Partial': true,
Accept: 'text/html',
},
})
.fail((data) => {
if (typeof data.responseText !== 'undefined') {
try {
let exception = JSON.parse(data.responseText)
window.UIkit.notify({
message: exception.message,
status: 'danger',
timeout: 3000,
pos: 'top-center',
})
} catch (e) {
// No valid JsonResponse, need to refresh page
window.location.href = location.href
}
} else {
if (!response.ok) {
throw response
}
const data = await response.text()
this.applyContent(data)
let pageLoadEvent = new CustomEvent('pageload', { detail: data })
window.dispatchEvent(pageLoadEvent)
} catch (response) {
const data = await response.text()
if (data) {
try {
let exception = JSON.parse(data)
window.UIkit.notify({
message: window.Rozier.messages.forbiddenPage,
message: exception.message,
status: 'danger',
timeout: 3000,
pos: 'top-center',
})
} catch (e) {
// No valid JsonResponse, need to refresh page
window.location.href = location.href
}

this.canvasLoader.hide()
})
} else {
window.UIkit.notify({
message: window.Rozier.messages.forbiddenPage,
status: 'danger',
timeout: 3000,
pos: 'top-center',
})
}
}
this.canvasLoader.hide()
})
}

Expand Down
Loading

0 comments on commit 0c6579e

Please sign in to comment.