import React, { useState } from "react";
import AdminLayout from "./Layout/AdminLayout";
import { FaPlus, FaTrash } from "react-icons/fa6";
import Pagination from "../../Components/Pagination";
import { FaEdit } from "react-icons/fa";
import { IoBookOutline, IoInformationCircleSharp } from "react-icons/io5";
import { Link } from "@inertiajs/react";
import GenerateUrl from "../../Utils/GenerateUrl";
import { debounce } from "../../Utils/Debounce";
import useSWR, { mutate } from "swr";
import { fetcher } from "../../Utils/Fetcher";
import CustomModal from "../../Components/CustomModal";
import NoDataTable from "../../Components/NoDataTable";
import Swal from "sweetalert2";
import HitApi from "../../Utils/HitApi";
import DeleteData from "../../Utils/DeleteData";
import { PiRankingFill } from "react-icons/pi";
const MateriDetail = (props) => {
const [page, setPage] = useState(1);
const [search, setSearch] = useState("");
const URL = GenerateUrl(
"/api/v1/materi",
`id=${props.id}`,
`page=${page}`,
`search=${encodeURIComponent(search)}`
);
const { data, error, isLoading } = useSWR(URL, fetcher);
const handleSearch = debounce((term) => {
setSearch(term);
}, 500);
const handleChangeSearch = (e) => {
const { value } = e.target;
setPage(1);
handleSearch(value);
};
const [showModal, setShowModal] = useState(false);
const [showRankModal, setShowRankModal] = useState(false);
const [form, setForm] = useState({
name: "",
category: "materi",
option: "tambah",
});
const clearForm = () => {
setForm({
name: "",
category: "materi",
option: "tambah",
});
};
const [ranking, setRanking] = useState([]);
const myState = {
showModal,
setShowModal,
URL,
form,
setForm,
clearForm,
props,
ranking,
setRanking,
showRankModal,
setShowRankModal,
};
return (
{data && (
<>
{/* head */}
|
Name |
Kategori |
Created At |
Aksi |
{data.result.data.map((item, index) => (
{data.result.from + index}
|
{item.name} |
{item.kategori} |
{new Date(
item.created_at
).toLocaleString()}
|
{item.kategori ==
"kuis" && (
)}
|
))}
>
)}
);
};
const RankingModal = ({ state }) => {
const { ranking, setRanking } = state;
return (
{ranking.length == 0 ? (
Tidak ada
) : (
No |
Nama |
Score |
Tanggal |
{ranking.map((item, index) => (
{index + 1} |
{item.user.name}
{item.user.email}
|
{item.skor} |
{new Date(
item.created_at
).toLocaleString()}
|
))}
)}
);
};
const Modal = ({ state }) => {
const { form, setForm, clearForm, props } = state;
// handle change form
const handleChange = (e) => {
const { name, value } = e.target;
setForm((prev) => ({ ...prev, [name]: value }));
};
const handleSubmit = (e) => {
e.preventDefault();
Swal.fire({
title: "Konfirmasi",
text:
form.option == "tambah"
? "Apakah anda yakin ingin menambahkan data?"
: "Apakah anda yakin ingin mengubah data?",
icon: "warning",
showCancelButton: true,
confirmButtonColor: "#3085d6",
cancelButtonColor: "#d33",
confirmButtonText: "Ya",
}).then((result) => {
if (result.isConfirmed) {
Swal.fire({
title: "Loading",
html: '
', // add html attribute if you want or remove
allowOutsideClick: false,
showConfirmButton: false,
});
HitApi({
url: "/api/v1/materi",
method: form.option == "tambah" ? "POST" : "PUT",
body: { ...form, id_module: props.id },
onSuccess: () => {
Swal.fire(
"Berhasil",
form.option == "tambah"
? "Data berhasil ditambahkan"
: "Data berhasil diubah",
"success"
);
clearForm();
state.setShowModal(false);
mutate(state.URL);
},
onError: () => {
// Swal.fire("Gagal", "Data gagal ditambahkan", "error");
},
});
}
});
};
return (
);
};
export default MateriDetail;