feat: slicing cms faq

main-dlh
shola 2025-12-05 20:04:29 +07:00
parent 7981812499
commit 3e8d407cb6
No known key found for this signature in database
GPG Key ID: FA9358FFDCCD05D9
3 changed files with 396 additions and 3 deletions

View File

@ -0,0 +1,73 @@
using Microsoft.AspNetCore.Mvc;
namespace BpsRwApp.Controllers
{
[Route("[controller]/[action]")]
public class FaqController : AppControllerBase
{
public IActionResult Index()
{
return View();
}
[HttpGet]
public IActionResult Table()
{
var data = new[]
{
new {
id = 1,
pertanyaan = "Apa tujuan dari BPS-RW?",
answer = "BPS-RW bertujuan untuk meningkatkan kesadaran masyarakat akan pentingnya pengelolaan sampah yang baik dan benar."
},
new {
id = 2,
pertanyaan = "Di mana saya bisa mendaftar?",
answer = "Anda bisa mendaftar melalui situs web resmi BPS-RW atau datang langsung ke kantor kelurahan setempat."
},
new {
id = 3,
pertanyaan = "Siapa yang harus saya hubungi untuk melaporkan pembuangan sampah ilegal?",
answer = "Anda dapat menghubungi layanan kebersihan kota atau melalui aplikasi aduan warga."
}
};
var result = data.Select((x, index) => new
{
no = index + 1,
x.id,
x.pertanyaan,
x.answer,
aksi = $@"
<div class='flex gap-2'>
<button data-id='{x.id}' class='btn bg-white text-primary-500 btn-sm border-primary-500 rounded-full btn-edit-faq'>Edit</button>
<button data-id='{x.id}' class='btn bg-white text-gray-800 btn-sm border-gray-800 rounded-full btn-detail-faq'>Detail</button>
<button data-id='{x.id}' class='btn bg-white text-red-500 btn-sm border-red-500 rounded-full btn-delete-faq'>Delete</button>
</div>"
});
return Json(new { data = result });
}
[HttpPost]
public IActionResult Create(string pertanyaan, string jawaban)
{
// create data
return Json(new { success = true, message = "FAQ berhasil ditambahkan" });
}
[HttpPost]
public IActionResult Update(int id, string pertanyaan, string jawaban)
{
// update data
return Json(new { success = true, message = "FAQ berhasil diupdate" });
}
[HttpPost]
public IActionResult Delete(int id)
{
// delete data
return Json(new { success = true, message = "FAQ berhasil dihapus" });
}
}
}

View File

@ -0,0 +1,314 @@
@{
ViewData["Title"] = "FAQ";
}
<!-- Toast Notification -->
<div id="successToast" class="fixed top-20 right-6 z-9999 hidden">
<div class="alert bg-green-50 border border-green-200 shadow-lg flex items-start gap-3 pr-12 min-w-[320px]">
<div class="shrink-0">
<div class="w-5 h-5 rounded-full bg-green-500 flex items-center justify-center">
<span class="icon text-white text-xs">check</span>
</div>
</div>
<div class="flex-1">
<h3 id="toastTitle" class="font-semibold text-gray-900">FAQ berhasil ditambah!</h3>
<p id="toastMessage" class="text-sm text-gray-600">Berhasil! FAQ baru telah berhasil ditambahkan.</p>
</div>
<button onclick="closeToast()" class="absolute top-3 right-3 text-gray-400 hover:text-gray-600">
<span class="icon text-lg">close</span>
</button>
</div>
</div>
<div class="breadcrumbs text-sm mb-4">
<ul>
<li class="text-gray-500"><a>CMS</a></li>
<li id="breadcrumb-active">FAQ</li>
</ul>
</div>
<!-- Header -->
<div class="flex items-center justify-between mb-6">
<h2 class="text-xl font-semibold text-gray-800">FAQ</h2>
<div class="flex items-center">
<button onclick="modal_tambah.showModal()" class="btn btn-primary rounded-full">
<span class="icon icon-fill">add</span>
Tambah
</button>
</div>
</div>
<!-- Table -->
<div class="card bg-white shadow-sm">
<div class="card-body p-0">
<table class="table-zebra table w-full" id="faq-table">
<thead>
<tr>
<th class="w-[5%]">No</th>
<th class="w-[35%]">Pertanyaan</th>
<th class="w-[45%]">Answer</th>
<th class="w-[15%]">Aksi</th>
</tr>
</thead>
<tbody></tbody>
</table>
</div>
</div>
<!-- Modal Tambah FAQ -->
<dialog id="modal_tambah" class="modal modal-bottom sm:modal-middle">
<div class="modal-box w-full sm:max-w-lg">
<button class="btn btn-sm btn-circle absolute right-2 top-2" onclick="modal_tambah.close()">✕</button>
<h3 class="text-lg font-bold mb-4">Tambah FAQ</h3>
<form id="form_tambah">
<div class="space-y-4">
<fieldset class="fieldset text-base">
<legend class="fieldset-legend">Pertanyaan <span class="text-red-500">*</span></legend>
<textarea name="Pertanyaan" class="textarea textarea-bordered w-full h-32"
placeholder="Masukkan pertanyaan" required></textarea>
</fieldset>
<fieldset class="fieldset text-base">
<legend class="fieldset-legend">Jawaban <span class="text-red-500">*</span></legend>
<textarea name="Jawaban" class="textarea textarea-bordered w-full h-32"
placeholder="Masukkan jawaban" required></textarea>
</fieldset>
</div>
<div class="modal-action mt-6">
<button type="button" class="btn rounded-full" onclick="resetFormTambah()">
<span class="icon">cancel</span>
Tutup
</button>
<button type="submit" class="btn bg-primary-500 text-white rounded-full hover:bg-primary-700">
<span class="icon">save</span>
Simpan
</button>
</div>
</form>
</div>
<form method="dialog" class="modal-backdrop">
<button onclick="resetFormTambah()">close</button>
</form>
</dialog>
<!-- Modal Edit FAQ -->
<dialog id="modal_edit" class="modal modal-bottom sm:modal-middle">
<div class="modal-box w-full sm:max-w-lg">
<button class="btn btn-sm btn-circle absolute right-2 top-2" onclick="modal_edit.close()">✕</button>
<h3 class="text-lg font-bold mb-4">Tambah FAQ</h3>
<form id="form_edit">
<input type="hidden" id="edit_id" name="Id">
<div class="space-y-4">
<fieldset class="fieldset text-base">
<legend class="fieldset-legend">Pertanyaan <span class="text-red-500">*</span></legend>
<textarea id="edit_pertanyaan" name="Pertanyaan" class="textarea textarea-bordered w-full h-32"
placeholder="Masukkan pertanyaan" required></textarea>
</fieldset>
<fieldset class="fieldset text-base">
<legend class="fieldset-legend">Jawaban <span class="text-red-500">*</span></legend>
<textarea id="edit_jawaban" name="Jawaban" class="textarea textarea-bordered w-full h-32"
placeholder="Masukkan jawaban" required></textarea>
</fieldset>
</div>
<div class="modal-action mt-6">
<button type="button" class="btn rounded-full" onclick="resetFormEdit()">
<span class="icon">cancel</span>
Tutup
</button>
<button type="submit" class="btn bg-primary-500 text-white rounded-full hover:bg-primary-700">
<span class="icon">save</span>
Simpan
</button>
</div>
</form>
</div>
<form method="dialog" class="modal-backdrop">
<button onclick="resetFormEdit()">close</button>
</form>
</dialog>
<!-- Modal Detail FAQ -->
<dialog id="modal_detail" class="modal modal-bottom sm:modal-middle">
<div class="modal-box w-full max-w-2xl">
<button class="btn btn-sm btn-circle absolute right-2 top-2" onclick="modal_detail.close()">✕</button>
<h3 class="text-lg font-bold mb-4">Detail FAQ</h3>
<div class="space-y-4">
<div>
<p class="text-sm text-gray-500">Pertanyaan</p>
<p class="font-semibold text-lg" id="detail_pertanyaan">-</p>
</div>
<div>
<p class="text-sm text-gray-500">Jawaban</p>
<p id="detail_jawaban">-</p>
</div>
</div>
<div class="modal-action mt-6">
<button type="button" class="btn rounded-full" onclick="modal_detail.close()">
<span class="icon">cancel</span>
Tutup
</button>
<button type="button" id="btn_edit_from_detail"
class="btn bg-primary-500 text-white rounded-full hover:bg-primary-700">
<span class="icon">edit</span>
Edit
</button>
</div>
</div>
<form method="dialog" class="modal-backdrop">
<button>close</button>
</form>
</dialog>
<script src="https://code.jquery.com/jquery-3.7.1.js"></script>
<script src="https://cdn.datatables.net/2.3.4/js/dataTables.js"></script>
<script src="/plugins/datatables/dataTables.tailwindcss.js"></script>
<script src="https://cdn.jsdelivr.net/npm/sweetalert2@11"></script>
<script type="text/javascript">
var table;
function showToast(title, message) {
$('#toastTitle').text(title);
$('#toastMessage').text(message);
$('#successToast').removeClass('hidden');
setTimeout(function () {
closeToast();
}, 3000);
}
function closeToast() {
$('#successToast').addClass('hidden');
}
function resetFormTambah() {
$('#form_tambah')[0].reset();
modal_tambah.close();
}
function resetFormEdit() {
$('#form_edit')[0].reset();
modal_edit.close();
}
$(document).ready(function () {
table = new DataTable('#faq-table', {
ajax: '@Url.Action("Table", "Faq")',
scrollX: true,
autoWidth: false,
initComplete: function () {
$('div.dt-scroll-body thead').css('visibility', 'collapse');
},
columnDefs: [
{ width: '5%', targets: 0 },
{ width: '35%', targets: 1 },
{ width: '45%', targets: 2 },
{ width: '15%', targets: 3 }
],
columns: [
{ data: 'no', orderable: false, searchable: false },
{
data: 'pertanyaan',
render: function (data) {
return `<div class="break-words whitespace-normal">${data}</div>`;
}
},
{
data: 'answer',
render: function (data) {
return `<div class="break-words whitespace-normal">${data}</div>`;
}
},
{ data: 'aksi', orderable: false, searchable: false },
]
});
// Form Tambah
$('#form_tambah').on('submit', function (e) {
e.preventDefault();
let formData = $(this).serialize();
$.ajax({
url: '@Url.Action("Create", "Faq")',
type: 'POST',
data: formData,
success: function (response) {
if (response.success) {
showToast('FAQ berhasil ditambah!', 'Berhasil! FAQ baru telah berhasil ditambahkan.');
modal_tambah.close();
$('#form_tambah')[0].reset();
table.ajax.reload();
}
},
error: function () {
showToast('Gagal!', 'Terjadi kesalahan saat menyimpan data.');
}
});
});
// Form Edit
$('#form_edit').on('submit', function (e) {
e.preventDefault();
let formData = $(this).serialize();
$.ajax({
url: '@Url.Action("Update", "Faq")',
type: 'POST',
data: formData,
success: function (response) {
if (response.success) {
showToast('FAQ berhasil diubah!', 'Berhasil! FAQ telah berhasil diubah.');
modal_edit.close();
table.ajax.reload();
}
},
error: function () {
showToast('Gagal!', 'Terjadi kesalahan saat mengupdate data.');
}
});
});
// Button Edit
table.on("click", ".btn-edit-faq", function (e) {
e.preventDefault();
let rowData = table.row($(this).closest('tr')).data();
// default data
$('#edit_id').val(rowData.id);
$('#edit_pertanyaan').val(rowData.pertanyaan);
$('#edit_jawaban').val(rowData.answer);
modal_edit.showModal();
});
// Button Detail
table.on("click", ".btn-detail-faq", function (e) {
e.preventDefault();
let rowData = table.row($(this).closest('tr')).data();
$('#detail_pertanyaan').text(rowData.pertanyaan);
$('#detail_jawaban').text(rowData.answer);
// Simpan data
$('#btn_edit_from_detail').data('rowData', rowData);
modal_detail.showModal();
});
// Edit from
$('#btn_edit_from_detail').on('click', function () {
let rowData = $(this).data('rowData');
modal_detail.close();
$('#edit_id').val(rowData.id);
$('#edit_pertanyaan').val(rowData.pertanyaan);
$('#edit_jawaban').val(rowData.answer);
modal_edit.showModal();
});
});
</script>

View File

@ -266,13 +266,13 @@
</details> </details>
</li> </li>
<li> <li>
<details @(new[] { "TentangBpsRw", "SliderUtama" }.Contains(controller) ? "open" : "")> <details @(new[] { "TentangBpsRw", "SliderUtama", "TentangRumahMemilah", "InformasiKontak", "Regulasi", "Faq", "Edukasi" }.Contains(controller) ? "open" : "")>
<summary>CMS</summary> <summary>CMS</summary>
<ul> <ul>
<li> <li>
<a asp-controller="SliderUtama" asp-action="Index" <a asp-controller="SliderUtama" asp-action="Index"
class="@(controller == "SliderUtama" ? "menu-active" : "")"> class="@(controller == "SliderUtama" ? "menu-active" : "")">
<span class="icon icon-fill">text_fields</span> <span class="icon icon-fill">photo_size_select_actual</span>
Slider Utama Slider Utama
</a> </a>
</li> </li>
@ -311,6 +311,12 @@
Panduan Panduan
</a> </a>
</li> </li>
<li>
<a asp-controller="Faq" asp-action="Index" class="@(controller == "Faq" ? "menu-active" : "")">
<span class="icon icon-fill">help</span>
FAQ
</a>
</li>
</ul> </ul>
</details> </details>
</li> </li>