From 3bde377caab078264352269f2d3ca2cb3b57eb13 Mon Sep 17 00:00:00 2001
From: Anders <anders164a@gmail.com>
Date: Mon, 17 Aug 2020 10:43:52 +0200
Subject: [PATCH] v0.10.0b - Fixed Location update AJAX Added - You are now
 able to delete a location

---
 .../Http/Controllers/LocationController.php   | 17 ++++--
 .../views/admin/locations/create.blade.php    |  4 --
 .../views/admin/locations/edit.blade.php      | 55 ++++++++++++++++++-
 skolehjem/routes/web.php                      |  1 +
 4 files changed, 68 insertions(+), 9 deletions(-)

diff --git a/skolehjem/app/Http/Controllers/LocationController.php b/skolehjem/app/Http/Controllers/LocationController.php
index 12152b3..f94753e 100644
--- a/skolehjem/app/Http/Controllers/LocationController.php
+++ b/skolehjem/app/Http/Controllers/LocationController.php
@@ -61,7 +61,7 @@ class LocationController extends Controller
 
         // If there already is a washing machine with that name, then don't add it
         if (count($locations) > 0)
-            return redirect()->route("locations.store")->with('NameExists', '<p><b>Der findes allerede en lokation med det navn!</b></p>');
+            return redirect()->route("locations.index");
         else { // Else - Add it
             $location->save();
             $locations = Location::query()->paginate($request->input("limit", 20));
@@ -111,7 +111,7 @@ class LocationController extends Controller
 
         // If there already is a washing machine with that name, then don't change it
         if (count($allMachines) > 0)
-            return redirect()->route("locations.store")->with('NameExists', '<p><b>Der findes allerede en lokation med det navn!</b></p>');
+            return redirect()->route("locations.index");
         else { // Else - Change the name
             $location->update($data);
             $location->save();
@@ -124,12 +124,15 @@ class LocationController extends Controller
     /**
      * Remove the specified resource from storage.
      *
-     * @param  \App\Location  $location
+     * @param  int  $id
      * @return \Illuminate\Http\Response
      */
-    public function destroy(Location $location)
+    public function destroy($id)
     {
+        $locations = Location::find($id);
+        $locations->delete();
 
+        return redirect()->route("locations.index");
     }
 
     public function nameCheck(Request $request){
@@ -139,4 +142,10 @@ class LocationController extends Controller
         }
     }
 
+    public function nameCheckUpdate(Request $request){
+        $locations = Location::query()->where('name', 'LIKE',$request->nameCheck)->where('id', '!=', $request->id)->get();
+        if(count($locations) > 0 && $request->nameCheck !== NULL){
+            return 1;
+        }
+    }
 }
diff --git a/skolehjem/resources/views/admin/locations/create.blade.php b/skolehjem/resources/views/admin/locations/create.blade.php
index e6d2361..1bf1044 100644
--- a/skolehjem/resources/views/admin/locations/create.blade.php
+++ b/skolehjem/resources/views/admin/locations/create.blade.php
@@ -30,8 +30,6 @@
             );
         });
 
-
-
         //Dont delete me, if the user reloads the pages with text in the text-box this code runs.
         $(document).ready(function () {
             $value = $('#name').val();
@@ -83,7 +81,5 @@
                 }
             });
         })
-
-
     </script>
 @endsection
diff --git a/skolehjem/resources/views/admin/locations/edit.blade.php b/skolehjem/resources/views/admin/locations/edit.blade.php
index 39ee8c8..e4ecfb1 100644
--- a/skolehjem/resources/views/admin/locations/edit.blade.php
+++ b/skolehjem/resources/views/admin/locations/edit.blade.php
@@ -15,8 +15,9 @@
         @csrf
         @method("put")
         <label for="name">Lokationsnavn:</label>
+        <label hidden id="error" for="errormesseages">Lokation findes allerede</label>
         <input type="text" name="name" id="name" value="{{ $location->name }}" required>
-        <input type="submit" class="btn btn-dark text-white" value="Rediger">
+        <input type="submit" id="disable" class="btn btn-dark text-white" value="Rediger">
     </form>
 @endsection
 @section("scripts")
@@ -29,5 +30,57 @@
                 }
             );
         });
+
+        //Dont delete me, if the user reloads the pages with text in the text-box this code runs.
+        $(document).ready(function () {
+            $value = $('#name').val();
+            $.ajax({
+                type: 'get',
+                url: '{{route('locations.nameCheckUpdate')}}',
+                data: {'nameCheck':$value, 'id':{{$location->id}}},
+                success:function (data) {
+                    console.log(data);
+                    if(data){
+                        $("#error").show(100);
+                        $("#error").css('color', 'red');
+                        $("#disable").prop('disabled', true);
+                        $('#disable').css('cursor','not-allowed');
+                    }else{
+                        $("#error").hide();
+                        $("#disable").prop('disabled', false);
+                        $('#disable').css('cursor','pointer');
+                    }
+
+                },
+                error:function (data) {
+                    console.log(data);
+                }
+            });
+        });
+
+        $('#name').on('keyup', function () {
+            $value = $(this).val();
+            $.ajax({
+                type: 'get',
+                url: '{{route('locations.nameCheckUpdate')}}',
+                data: {'nameCheck':$value, 'id':{{$location->id}}},
+                success:function (data) {
+                    if(data){
+                        $("#error").show(100);
+                        $("#error").css('color', 'red');
+                        $("#disable").prop('disabled', true);
+                        $('#disable').css('cursor','not-allowed');
+                    }else{
+                        $("#error").hide();
+                        $("#disable").prop('disabled', false);
+                        $('#disable').css('cursor','pointer');
+                    }
+
+                },
+                error:function (data) {
+                    console.log(data);
+                }
+            });
+        })
     </script>
 @endsection
diff --git a/skolehjem/routes/web.php b/skolehjem/routes/web.php
index 42cff39..6bd705f 100644
--- a/skolehjem/routes/web.php
+++ b/skolehjem/routes/web.php
@@ -72,6 +72,7 @@ Route::get("/roleCheckUpdate", "RolesController@nameCheckUpdate")->name("roles.n
 Route::get("/guidesCheckUpdate", "GuideController@nameCheckUpdate")->name("guides.nameCheckUpdate");
 Route::get("/newsCheckUpdate", "NewsController@nameCheckUpdate")->name("news.nameCheckUpdate");
 Route::get("/menuplanCheckUpdate", "MenuPlanController@nameCheckUpdate")->name("menuplans.nameCheckUpdate");
+Route::get("/locationsCheckUpdate", "LocationController@nameCheckUpdate")->name("locations.nameCheckUpdate");