diff --git a/app/Http/Controllers/Admin/ManageLocationController.php b/app/Http/Controllers/Admin/ManageLocationController.php index fc752af..8d730d4 100644 --- a/app/Http/Controllers/Admin/ManageLocationController.php +++ b/app/Http/Controllers/Admin/ManageLocationController.php @@ -107,24 +107,30 @@ class ManageLocationController extends Controller Created at : 07 June 2024 Use : To update location. */ + public function update(Request $request) { - - try { DB::beginTransaction(); - + + // Check if the location name already exists, excluding the current location + if (ManageState::where('name', $request->location_name)->where('id', '!=', $request->id)->exists()) { + return response()->json(['success' => false, 'error' => 'Location name already exists', 'status' => 422]); + } + $location_data = ManageState::find($request->id); $location_data->name = $request->location_name; $location_data->save(); - + DB::commit(); - - return jsonResponseWithSuccessMessage(__('success.update_data')); + + return response()->json(['success' => true, 'message' => __('success.update_data'), 'status' => 200]); } catch (Exception $e) { DB::rollBack(); Log::error("Failed to update location: " . $e->getMessage()); - return jsonResponseWithErrorMessage(__('auth.something_went_wrong'), 500); + return response()->json(['success' => false, 'error' => __('auth.something_went_wrong'), 'status' => 500]); } } + + } diff --git a/public/assets/js/admin/manage_location/edit_location.js b/public/assets/js/admin/manage_location/edit_location.js index 6d727d3..e088cc9 100644 --- a/public/assets/js/admin/manage_location/edit_location.js +++ b/public/assets/js/admin/manage_location/edit_location.js @@ -6,6 +6,7 @@ $(document).on("click", ".edit_location_value", function (e) { }); + $('#edit_location').validate({ rules: { location_name: { @@ -21,7 +22,7 @@ $('#edit_location').validate({ submitHandler: function (form) { var formData = new FormData(form); $('#update_passport_btn').text('Please wait...'); - $('#update_passport_btn').attr('disabled', true); + $('#update_passport_btn').attr('disabled', true); let base_url = url_path; $.ajax({ url: base_url + '/update_location', @@ -30,13 +31,16 @@ $('#edit_location').validate({ processData: false, contentType: false, success: function(result) { - - if (result.status_code == 200) { + if (result.status === 200) { toastr.success('Location Updated successfully'); setTimeout(function() { window.location.href = base_url + "/manage_location"; }, 2000); - } else { + } + else if (result.status === 422 && result.error === 'Location name already exists') { + toastr.error('Location name already exists'); + } + else { toastr.error('Something Went Wrong'); setTimeout(function() { window.location.href = base_url + "/manage_location"; @@ -45,8 +49,11 @@ $('#edit_location').validate({ $('#update_passport_btn').attr('disabled', false); $('#update_passport_btn').text('Submit'); }, + error: function(xhr, status, error) { + toastr.error('Something Went Wrong'); + $('#update_passport_btn').attr('disabled', false); + $('#update_passport_btn').text('Submit'); + } }); - $('#update_passport_btn').attr('disabled', false); - $('#update_passport_btn').text('Submit'); } -}); \ No newline at end of file +});