This commit is contained in:
MOCH. PASHA ARDYAN PUTRA
2025-07-07 15:08:43 +00:00
parent 0ee9f230d6
commit 3206db6010
5 changed files with 270 additions and 81 deletions

View File

@@ -13,26 +13,6 @@ const ProtectedRoute = ({ element }) => {
return token ? element : <Navigate to="/login" />; return token ? element : <Navigate to="/login" />;
}; };
// Komponen redirect berdasarkan sessionStorage
const HomeRedirect = () => {
const token = localStorage.getItem("token");
const hasOpen = sessionStorage.getItem("hasOpen");
if (!token) {
return <Navigate to="/login" />;
}
// Jika tidak ada sessionId (anggap sebagai session baru)
if (!hasOpen) {
sessionStorage.setItem("hasOpen", true);
return <Navigate to="/scan" />;
}
// Jika sudah ada sessionId
return <Navigate to="/dashboard" />;
};
function App() { function App() {
return ( return (
<div className="App"> <div className="App">
@@ -43,11 +23,11 @@ function App() {
path="/dashboard" path="/dashboard"
element={<ProtectedRoute element={<Dashboard />} />} element={<ProtectedRoute element={<Dashboard />} />}
/> />
<Route path="/" element={<ProtectedRoute element={<Dashboard />} />} />
<Route <Route
path="/profile" path="/profile"
element={<ProtectedRoute element={<Profile />} />} element={<ProtectedRoute element={<Profile />} />}
/> />
<Route path="/" element={<HomeRedirect />} />
<Route path="/:nik" element={<ShowImage />} /> <Route path="/:nik" element={<ShowImage />} />
</Routes> </Routes>
</div> </div>

View File

@@ -70,7 +70,7 @@ const Dashboard = () => {
verifyTokenAndFetchData(); verifyTokenAndFetchData();
}, []); }, []);
useEffect(() => { // Memisahkan fungsi fetchOfficers agar dapat dipanggil ulang
const fetchOfficers = async () => { const fetchOfficers = async () => {
const token = localStorage.getItem("token"); const token = localStorage.getItem("token");
try { try {
@@ -91,6 +91,7 @@ const Dashboard = () => {
} }
}; };
useEffect(() => {
if (user.role == "admin") { if (user.role == "admin") {
fetchOfficers(); fetchOfficers();
} }
@@ -133,6 +134,9 @@ const Dashboard = () => {
setUsername(""); setUsername("");
setPassword(""); setPassword("");
setErrorMessage(""); setErrorMessage("");
// Refresh daftar officer setelah berhasil menambahkan
await fetchOfficers();
} catch (error) { } catch (error) {
setErrorMessage(error.message || "Gagal menambahkan officer"); setErrorMessage(error.message || "Gagal menambahkan officer");
setSuccessMessage(""); setSuccessMessage("");
@@ -148,6 +152,7 @@ const Dashboard = () => {
document.addEventListener("mousedown", handleClickOutside); document.addEventListener("mousedown", handleClickOutside);
return () => document.removeEventListener("mousedown", handleClickOutside); return () => document.removeEventListener("mousedown", handleClickOutside);
}, []); }, []);
const handleDeleteOfficer = async (id) => { const handleDeleteOfficer = async (id) => {
const confirmDelete = window.confirm( const confirmDelete = window.confirm(
"Apakah Anda yakin ingin menghapus petugas ini?" "Apakah Anda yakin ingin menghapus petugas ini?"
@@ -268,11 +273,22 @@ const Dashboard = () => {
{user.role === "admin" && ( {user.role === "admin" && (
<div className={styles.formSection}> <div className={styles.formSection}>
<h2>Daftar Petugas</h2> <h2>Daftar Petugas</h2>
<ul className={styles.officerList}> <div className={styles.officerListContainer}>
{officers.map((officer) => ( <div className={styles.officerList}>
<li key={officer.id} className={styles.officerItem}> {officers.length > 0 ? (
👤 <strong>{officer.username}</strong> {" "} officers.map((officer) => (
<em>{officer.role}</em> <div key={officer.id} className={styles.officerItem}>
<div className={styles.officerInfo}>
<span className={styles.officerIcon}>👤</span>
<div className={styles.officerDetails}>
<strong className={styles.officerName}>
{officer.username}
</strong>
<span className={styles.officerRole}>
{officer.role}
</span>
</div>
</div>
<button <button
onClick={() => handleDeleteOfficer(officer.id)} onClick={() => handleDeleteOfficer(officer.id)}
className={styles.deleteButton} className={styles.deleteButton}
@@ -280,9 +296,16 @@ const Dashboard = () => {
> >
</button> </button>
</li> </div>
))} ))
</ul> ) : (
<div className={styles.emptyState}>
<span>📋</span>
<p>Belum ada petugas terdaftar</p>
</div>
)}
</div>
</div>
<hr className={styles.separator} /> <hr className={styles.separator} />
<h2>Tambah Petugas Baru</h2> <h2>Tambah Petugas Baru</h2>

View File

@@ -442,3 +442,159 @@ body {
grid-column: 1 / -1; grid-column: 1 / -1;
} }
} }
/* CSS untuk styling daftar petugas */
.officerListContainer {
background: #f8f9fa;
border: 1px solid #dee2e6;
border-radius: 8px;
padding: 16px;
margin-bottom: 20px;
box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
}
.officerList {
max-height: 300px;
overflow-y: auto;
padding: 0;
margin: 0;
list-style: none;
border-radius: 6px;
background: white;
border: 1px solid #e9ecef;
}
.officerItem {
display: flex;
align-items: center;
justify-content: space-between;
padding: 12px 16px;
border-bottom: 1px solid #e9ecef;
transition: background-color 0.2s ease;
}
.officerItem:last-child {
border-bottom: none;
}
.officerItem:hover {
background-color: #f8f9fa;
}
.officerInfo {
display: flex;
align-items: center;
gap: 12px;
flex: 1;
}
.officerIcon {
font-size: 20px;
width: 24px;
height: 24px;
display: flex;
align-items: center;
justify-content: center;
}
.officerDetails {
display: flex;
flex-direction: column;
gap: 2px;
}
.officerName {
font-weight: 600;
color: #2c3e50;
font-size: 14px;
}
.officerRole {
font-size: 12px;
color: #6c757d;
font-style: italic;
text-transform: capitalize;
}
.deleteButton {
background: none;
border: none;
cursor: pointer;
font-size: 12px;
padding: 4px 8px;
border-radius: 4px;
transition: background-color 0.2s ease;
opacity: 0.7;
}
.deleteButton:hover {
background-color: #fee;
opacity: 1;
}
.deleteButton:focus {
outline: 2px solid #dc3545;
outline-offset: 2px;
}
.emptyState {
text-align: center;
padding: 40px 20px;
color: #6c757d;
}
.emptyState span {
font-size: 32px;
display: block;
margin-bottom: 8px;
}
.emptyState p {
margin: 0;
font-size: 14px;
}
.separator {
border: none;
border-top: 1px solid #dee2e6;
margin: 24px 0;
}
/* Custom scrollbar untuk daftar petugas */
.officerList::-webkit-scrollbar {
width: 8px;
}
.officerList::-webkit-scrollbar-track {
background: #f1f1f1;
border-radius: 4px;
}
.officerList::-webkit-scrollbar-thumb {
background: #c1c1c1;
border-radius: 4px;
}
.officerList::-webkit-scrollbar-thumb:hover {
background: #a8a8a8;
}
/* Responsive design */
@media (max-width: 768px) {
.officerItem {
padding: 10px 12px;
}
.officerInfo {
gap: 8px;
}
.officerName {
font-size: 13px;
}
.officerRole {
font-size: 11px;
}
}

View File

@@ -1,4 +1,6 @@
import React, { useEffect, useRef, useState } from "react"; import React, { useEffect, useRef, useState } from "react";
import styless from "./Dashboard.module.css";
import { useNavigate } from "react-router-dom"; import { useNavigate } from "react-router-dom";
import Modal from "./Modal"; import Modal from "./Modal";
@@ -7,6 +9,8 @@ import PaginatedFormEditable from "./PaginatedFormEditable";
const STORAGE_KEY = "camera_canvas_gallery"; const STORAGE_KEY = "camera_canvas_gallery";
const CameraCanvas = () => { const CameraCanvas = () => {
const [isMenuOpen, setIsMenuOpen] = useState(false);
const menuRef = useRef(null);
const navigate = useNavigate(); const navigate = useNavigate();
const videoRef = useRef(null); const videoRef = useRef(null);
@@ -489,30 +493,57 @@ const CameraCanvas = () => {
return ( return (
<div> <div>
<div <div className={styless.dashboardHeader}>
style={{ <div className={styless.logoAndTitle}>
display: "flex", <img src="/PSI.png" alt="Bot Avatar" />
alignItems: "center", <h1 className={styless.h1}>Kawal PSI Dashboard</h1>
padding: "12px 16px", </div>
backgroundColor: "#f5f5f5",
fontFamily: "sans-serif", <div className={styless.dropdownContainer} ref={menuRef}>
fontSize: "16px",
fontWeight: "bold",
}}
>
<button <button
style={{ onClick={() => setIsMenuOpen(!isMenuOpen)}
marginRight: "12px", className={styles.dropdownToggle}
fontSize: "18px", aria-expanded={isMenuOpen ? "true" : "false"}
background: "none", aria-haspopup="true"
border: "none",
cursor: "pointer",
}}
onClick={() => navigate("/dashboard")}
> >
&lt; <svg
width="15"
height="20"
viewBox="0 0 24 24"
fill="none"
stroke="currentColor"
strokeWidth="2"
strokeLinecap="round"
strokeLinejoin="round"
>
<line x1="3" y1="6" x2="21" y2="6" />
<line x1="3" y1="12" x2="21" y2="12" />
<line x1="3" y1="18" x2="21" y2="18" />
</svg>
</button> </button>
<div>Scan KTP atau unggah</div> {isMenuOpen && (
<div className={styless.dropdownMenu}>
<button
onClick={() => {
navigate("/profile");
setIsMenuOpen(false);
}}
className={styless.dropdownItem}
>
Profile
</button>
<button
onClick={() => {
navigate("/dashboard");
setIsMenuOpen(false);
}}
className={styless.dropdownItem}
>
Dashboard
</button>
</div>
)}
</div>
</div> </div>
<video <video
ref={videoRef} ref={videoRef}

View File

@@ -50,7 +50,6 @@ body {
flex-direction: column; flex-direction: column;
} }
/* Header */
.dashboardHeader { .dashboardHeader {
background-color: var(--white); background-color: var(--white);
color: var(--text-primary); color: var(--text-primary);