fix redirect back data settings

This commit is contained in:
arifal
2025-03-12 21:39:52 +07:00
parent 4db457d7bd
commit e5db2294b4
7 changed files with 33 additions and 37 deletions

View File

@@ -18,34 +18,21 @@ class DataSettingController extends Controller
*/ */
public function index(IndexRequest $request) public function index(IndexRequest $request)
{ {
$menuId = $request->query('menu_id'); $menuId = $request->query('menu_id') ?? $request->input('menu_id');
$user = Auth::user(); $permissions = $this->permissions[$menuId]?? []; // Avoid undefined index error
$userId = $user->id; $creator = $permissions['allow_create'] ?? 0;
$updater = $permissions['allow_update'] ?? 0;
// Ambil role_id yang dimiliki user $destroyer = $permissions['allow_destroy'] ?? 0;
$roleIds = DB::table('user_role') return view("data-settings.index", compact('creator', 'updater', 'destroyer','menuId'));
->where('user_id', $userId)
->pluck('role_id');
// Ambil data akses berdasarkan role_id dan menu_id
$roleAccess = DB::table('role_menu')
->whereIn('role_id', $roleIds)
->where('menu_id', $menuId)
->first();
// Pastikan roleAccess tidak null sebelum mengakses properti
$creator = $roleAccess->allow_create ?? 0;
$updater = $roleAccess->allow_update ?? 0;
$destroyer = $roleAccess->allow_destroy ?? 0;
return view("data-settings.index", compact('creator', 'updater', 'destroyer'));
} }
/** /**
* Show the form for creating a new resource. * Show the form for creating a new resource.
*/ */
public function create() public function create(IndexRequest $request)
{ {
return view("data-settings.create"); $menuId = $request->query('menu_id') ?? $request->input('menu_id');
return view("data-settings.create", compact('menuId'));
} }
/** /**
@@ -78,14 +65,15 @@ class DataSettingController extends Controller
/** /**
* Show the form for editing the specified resource. * Show the form for editing the specified resource.
*/ */
public function edit(string $id) public function edit(IndexRequest $request,string $id)
{ {
try{ try{
$data = DataSetting::findOrFail($id); $data = DataSetting::findOrFail($id);
$menuId = $request->query('menu_id') ?? $request->input('menu_id');
if(empty($data)){ if(empty($data)){
return redirect()->route('data-settings.index')->with('error', 'Invalid id'); return redirect()->route('data-settings.index')->with('error', 'Invalid id');
} }
return view("data-settings.edit", compact("data")); return view("data-settings.edit", compact("data", 'menuId'));
}catch(Exception $ex){ }catch(Exception $ex){
return redirect()->route("data-settings.index")->with("error", "Invalid id"); return redirect()->route("data-settings.index")->with("error", "Invalid id");
} }

View File

@@ -1,6 +1,7 @@
document.addEventListener("DOMContentLoaded", function (e) { document.addEventListener("DOMContentLoaded", function (e) {
const toastNotification = document.getElementById("toastNotification"); const toastNotification = document.getElementById("toastNotification");
const toast = new bootstrap.Toast(toastNotification); const toast = new bootstrap.Toast(toastNotification);
let menuId = document.getElementById("menuId").value;
document document
.getElementById("btnCreateDataSettings") .getElementById("btnCreateDataSettings")
.addEventListener("click", async function () { .addEventListener("click", async function () {
@@ -37,7 +38,7 @@ document.addEventListener("DOMContentLoaded", function (e) {
result.data.message; result.data.message;
toast.show(); toast.show();
setTimeout(() => { setTimeout(() => {
window.location.href = "/data-settings"; window.location.href = `/data-settings?menu_id=${menuId}`;
}, 2000); }, 2000);
} else { } else {
let error = await response.json(); let error = await response.json();

View File

@@ -32,7 +32,8 @@ class DataSettings {
tableContainer.innerHTML = ""; tableContainer.innerHTML = "";
let canUpdate = tableContainer.getAttribute("data-updater") === "1"; let canUpdate = tableContainer.getAttribute("data-updater") === "1";
let canDelete = tableContainer.getAttribute("data-destroyer") === "1" let canDelete = tableContainer.getAttribute("data-destroyer") === "1";
let menuId = tableContainer.getAttribute("data-menuId");
// Create a new Grid.js instance only if it doesn't exist // Create a new Grid.js instance only if it doesn't exist
this.table = new Grid({ this.table = new Grid({
columns: [ columns: [
@@ -45,15 +46,15 @@ class DataSettings {
width: "120px", width: "120px",
formatter: function (cell) { formatter: function (cell) {
let buttons = ""; let buttons = "";
if (canUpdate) { if (canUpdate) {
buttons += ` buttons += `
<a href="/data-settings/${cell}/edit" class="btn btn-yellow btn-sm d-inline-flex align-items-center justify-content-center"> <a href="/data-settings/${cell}/edit?menu_id=${menuId}" class="btn btn-yellow btn-sm d-inline-flex align-items-center justify-content-center">
<i class='bx bx-edit'></i> <i class='bx bx-edit'></i>
</a> </a>
`; `;
} }
if (canDelete) { if (canDelete) {
buttons += ` buttons += `
<button class="btn btn-sm btn-red d-inline-flex align-items-center justify-content-center btn-delete-data-settings" data-id="${cell}"> <button class="btn btn-sm btn-red d-inline-flex align-items-center justify-content-center btn-delete-data-settings" data-id="${cell}">
@@ -61,12 +62,14 @@ class DataSettings {
</button> </button>
`; `;
} }
if (!canUpdate && !canDelete) { if (!canUpdate && !canDelete) {
buttons = `<span class="text-muted">No Privilege</span>`; buttons = `<span class="text-muted">No Privilege</span>`;
} }
return gridjs.html(`<div class="d-flex justify-content-center gap-2">${buttons}</div>`); return gridjs.html(
`<div class="d-flex justify-content-center gap-2">${buttons}</div>`
);
}, },
}, },
], ],

View File

@@ -6,6 +6,7 @@ document.addEventListener("DOMContentLoaded", function (e) {
let toast = new bootstrap.Toast( let toast = new bootstrap.Toast(
document.getElementById("toastNotification") document.getElementById("toastNotification")
); );
let menuId = document.getElementById("menuId").value;
submitButton.addEventListener("click", async function () { submitButton.addEventListener("click", async function () {
let submitButton = this; let submitButton = this;
@@ -36,7 +37,7 @@ document.addEventListener("DOMContentLoaded", function (e) {
toastMessage.innerText = result.data.message; toastMessage.innerText = result.data.message;
toast.show(); toast.show();
setTimeout(() => { setTimeout(() => {
window.location.href = "/data-settings"; window.location.href = `/data-settings?menu_id=${menuId}`;
}, 2000); }, 2000);
} else { } else {
let error = await response.json(); let error = await response.json();

View File

@@ -5,11 +5,12 @@
@include('layouts.partials/page-title', ['title' => 'Data Settings', 'subtitle' => 'Setting Dashboard']) @include('layouts.partials/page-title', ['title' => 'Data Settings', 'subtitle' => 'Setting Dashboard'])
<x-toast-notification /> <x-toast-notification />
<input type="hidden" id="menuId" value="{{ $menuId ?? 0 }}">
<div class="row d-flex justify-content-center"> <div class="row d-flex justify-content-center">
<div class="col-lg-6"> <div class="col-lg-6">
<div class="card"> <div class="card">
<div class="card-header d-flex justify-content-end"> <div class="card-header d-flex justify-content-end">
<a href="{{ route('data-settings.index') }}" class="btn btn-sm btn-secondary">Back</a> <a href="{{ route('data-settings.index', ['menu_id' => $menuId]) }}" class="btn btn-sm btn-secondary">Back</a>
</div> </div>
<div class="card-body"> <div class="card-body">
<form id="formDataSettings" action="{{ route('api.data-settings.store') }}" method="POST"> <form id="formDataSettings" action="{{ route('api.data-settings.store') }}" method="POST">

View File

@@ -5,11 +5,12 @@
@include('layouts.partials/page-title', ['title' => 'Data Settings', 'subtitle' => 'Setting Dashboard']) @include('layouts.partials/page-title', ['title' => 'Data Settings', 'subtitle' => 'Setting Dashboard'])
<x-toast-notification /> <x-toast-notification />
<input type="hidden" id="menuId" value="{{ $menuId ?? 0 }}">
<div class="row d-flex justify-content-center"> <div class="row d-flex justify-content-center">
<div class="col-lg-6"> <div class="col-lg-6">
<div class="card"> <div class="card">
<div class="card-header d-flex justify-content-end"> <div class="card-header d-flex justify-content-end">
<a href="{{ route('data-settings.index') }}" class="btn btn-sm btn-secondary">Back</a> <a href="{{ route('data-settings.index', ['menu_id' => $menuId]) }}" class="btn btn-sm btn-secondary">Back</a>
</div> </div>
<div class="card-body"> <div class="card-body">
<form id="formUpdateDataSettings" action="{{ route('api.data-settings.update', $data->id) }}" method="POST"> <form id="formUpdateDataSettings" action="{{ route('api.data-settings.update', $data->id) }}" method="POST">

View File

@@ -16,12 +16,13 @@
<div class="card-body"> <div class="card-body">
<div class="d-flex flex-wrap justify-content-end align-items-center mb-2"> <div class="d-flex flex-wrap justify-content-end align-items-center mb-2">
@if ($creator) @if ($creator)
<a href="{{ route('data-settings.create')}}" class="btn btn-success btn-sm d-block d-sm-inline w-auto">Create</a> <a href="{{ route('data-settings.create', ['menu_id' => $menuId])}}" class="btn btn-success btn-sm d-block d-sm-inline w-auto">Create</a>
@endif @endif
</div> </div>
<div id="table-data-settings" <div id="table-data-settings"
data-updater="{{ $updater }}" data-updater="{{ $updater }}"
data-destroyer="{{ $destroyer }}"> data-destroyer="{{ $destroyer }}"
data-menuId="{{ $menuId }}">
</div> </div>
</div> </div>
</div> </div>