chg: [ui] Added support of redirections via Ajax responses

pull/93/head
Sami Mokaddem 2021-11-08 15:54:37 +01:00
parent 50737543a9
commit d045f1f4d5
No known key found for this signature in database
GPG Key ID: 164C473F627A06FA
2 changed files with 21 additions and 8 deletions

View File

@ -567,7 +567,6 @@ class CRUDComponent extends Component
foreach ($ids as $id) {
$data = $this->Table->get($id);
$success = $this->Table->delete($data);
$success = true;
if ($success) {
$bulkSuccesses++;
}
@ -585,7 +584,11 @@ class CRUDComponent extends Component
Inflector::pluralize($this->ObjectAlias)
)
);
$this->setResponseForController('delete', $bulkSuccesses, $message, $data);
$additionalData = [];
if ($bulkSuccesses > 0) {
$additionalData['redirect'] = Router::url(['controller' => $this->Controller->getName(), 'action' => 'index']);
}
$this->setResponseForController('delete', $bulkSuccesses, $message, $data, null, $additionalData);
}
$this->Controller->set('metaGroup', 'ContactDB');
$this->Controller->set('scope', 'users');
@ -738,13 +741,16 @@ class CRUDComponent extends Component
$this->Controller->render('/genericTemplates/tag');
}
public function setResponseForController($action, $success, $message, $data = [], $errors = null)
public function setResponseForController($action, $success, $message, $data = [], $errors = null, $additionalData = [])
{
if ($success) {
if ($this->Controller->ParamHandler->isRest()) {
$this->Controller->restResponsePayload = $this->RestResponse->viewData($data, 'json');
} elseif ($this->Controller->ParamHandler->isAjax()) {
$this->Controller->ajaxResponsePayload = $this->RestResponse->ajaxSuccessResponse($this->ObjectAlias, $action, $data, $message);
if ($additionalData['redirect']) { // If a redirection occors, we need to make sure the flash message gets displayed
$this->Controller->Flash->success($message);
}
$this->Controller->ajaxResponsePayload = $this->RestResponse->ajaxSuccessResponse($this->ObjectAlias, $action, $data, $message, $additionalData);
} else {
$this->Controller->Flash->success($message);
$this->Controller->redirect($this->Controller->referer());
@ -753,6 +759,9 @@ class CRUDComponent extends Component
if ($this->Controller->ParamHandler->isRest()) {
$this->Controller->restResponsePayload = $this->RestResponse->viewData($data, 'json');
} elseif ($this->Controller->ParamHandler->isAjax()) {
if ($additionalData['redirect']) { // If a redirection occors, we need to make sure the flash message gets displayed
$this->Controller->Flash->error($message);
}
$this->Controller->ajaxResponsePayload = $this->RestResponse->ajaxFailResponse($this->ObjectAlias, $action, $data, $message, !is_null($errors) ? $errors : $data->getErrors());
} else {
$this->Controller->Flash->error($message);

View File

@ -185,10 +185,14 @@ class UIFactory {
submissionReloaderModal(url, reloadUrl, $reloadedElement, $statusNode=null) {
const successCallback = function ([data, modalObject]) {
UI.reload(reloadUrl, $reloadedElement, $statusNode)
if (data.additionalData !== undefined && data.additionalData.displayOnSuccess !== undefined) {
UI.modal({
rawHtml: data.additionalData.displayOnSuccess
})
if (data.additionalData !== undefined) {
if (data.additionalData.displayOnSuccess !== undefined) {
UI.modal({
rawHtml: data.additionalData.displayOnSuccess
})
} else if (data.additionalData.redirect !== undefined) {
window.location = data.additionalData.redirect
}
}
}
return UI.submissionModal(url, successCallback)