jqh 5 роки тому
батько
коміт
c214fd883a
2 змінених файлів з 29 додано та 20 видалено
  1. 5 2
      src/Controllers/MenuController.php
  2. 24 18
      src/Traits/HasFormResponse.php

+ 5 - 2
src/Controllers/MenuController.php

@@ -144,10 +144,13 @@ class MenuController extends AdminController
             $form->display('updated_at', trans('admin.updated_at'));
         })->saved(function (Form $form, $result) {
             if ($result) {
-                return $form->location(__('admin.save_succeeded'), 'auth/menu');
+                return $form->location('auth/menu', __('admin.save_succeeded'));
             }
 
-            return $form->location(__('admin.nothing_updated'), 'auth/menu', false);
+            return $form->location('auth/menu', [
+                'message' => __('admin.nothing_updated'),
+                'status'  => false,
+            ]);
         });
     }
 

+ 24 - 18
src/Traits/HasFormResponse.php

@@ -15,16 +15,24 @@ trait HasFormResponse
      * @param $message
      * @param null $redirect
      * @param bool $status
+     * @param array $options
      *
      * @return bool|\Illuminate\Http\JsonResponse
      */
-    public function ajaxResponse(?string $message, ?string $redirect = null, bool $status = true)
-    {
+    public function ajaxResponse(
+        ?string $message,
+        ?string $redirect = null,
+        bool $status = true,
+        array $options = []
+    ) {
         if ($this->isAjaxRequest()) {
+            $location = $options['location'] ?? false;
+            $urlKey = $location ? 'location' : 'redirect';
+
             return response()->json([
                 'status'   => $status,
                 'message'  => $message,
-                'redirect' => $redirect ? admin_url($redirect) : '',
+                $urlKey    => $redirect ? admin_url($redirect) : '',
             ]);
         }
 
@@ -40,21 +48,14 @@ trait HasFormResponse
      *
      * @return \Illuminate\Http\JsonResponse|\Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector
      */
-    public function location(?string $message, ?string $url = null, bool $status = true)
+    public function location($url = null, $options = [])
     {
-        if ($this->isAjaxRequest()) {
-            return response()->json([
-                'status'   => $status,
-                'message'  => $message,
-                'location' => $url ? admin_url($url) : false,
-            ]);
-        }
-
-        if ($message) {
-            admin_toastr($message, $status ? 'success' : 'error');
+        if (is_string($options)) {
+            $options = ['message' => $options];
         }
+        $options['location'] = true;
 
-        return $url ? redirect(admin_url($url)) : redirect()->refresh();
+        return $this->redirect($url, $options);
     }
 
     /**
@@ -136,13 +137,18 @@ trait HasFormResponse
     /**
      * Get redirect response.
      *
-     * @param string       $url
+     * @param string|array $url
      * @param array|string $options
      *
      * @return \Illuminate\Http\JsonResponse|\Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector
      */
-    public function redirect(?string $url, $options = null)
+    public function redirect($url = null, $options = null)
     {
+        if (is_array($url)) {
+            $options = $url;
+            $url = null;
+        }
+
         if (is_string($options)) {
             $message = $options;
             $options = [];
@@ -155,7 +161,7 @@ trait HasFormResponse
         if ($this->isAjaxRequest()) {
             $message = $message ?: trans('admin.save_succeeded');
 
-            return $this->ajaxResponse($message, $url, $status);
+            return $this->ajaxResponse($message, $url, $status, $options);
         }
 
         $statusCode = (int) ($options['status_code'] ?? 302);