From e7c1f56f9101c3a9787a76ae8adac27fd4aaf60e Mon Sep 17 00:00:00 2001 From: Vassshhh Date: Sun, 15 Jun 2025 11:35:34 +0700 Subject: [PATCH] ok --- src/App.js | 39 +++++++++++++--------- src/ChatBot.js | 1 - src/Dashboard.js | 84 +++++++++++++++++++++--------------------------- 3 files changed, 61 insertions(+), 63 deletions(-) diff --git a/src/App.js b/src/App.js index 57130c9..d1d3d97 100644 --- a/src/App.js +++ b/src/App.js @@ -1,15 +1,15 @@ import React, { useState, useEffect } from 'react'; -import { BrowserRouter, Routes, Route, useParams } from 'react-router-dom'; +import { BrowserRouter, Routes, Route, Navigate, useParams } from 'react-router-dom'; import axios from 'axios'; + import Dashboard from './Dashboard'; import TenantDashboard from './TenantDashboard'; import ChatBot from './ChatBot'; - -import './App.css'; import Login from './Login'; -function App() { - function ChatBotWrapper() { +import './App.css'; + +function ChatBotWrapper() { const { agentId } = useParams(); const [agentDetails, setAgentDetails] = useState(null); const [loading, setLoading] = useState(true); @@ -33,20 +33,29 @@ function App() { fetchAgent(); }, [agentId]); - // if (loading) return
Loading...
; - // if (!agentDetails) return
No agent found
; - return ; } + +// ✅ Komponen proteksi route +const ProtectedRoute = ({ element }) => { + const token = localStorage.getItem('token'); + return token ? element : ; +}; + +function App() { return (
- - - } /> - } /> - } /> - - + + + } /> + } /> + {/* ✅ Route /dashboard diproteksi */} + } />} + /> + +
); } diff --git a/src/ChatBot.js b/src/ChatBot.js index cef5872..b9c6164 100644 --- a/src/ChatBot.js +++ b/src/ChatBot.js @@ -114,7 +114,6 @@ let parsedObj = JSON.parse(cleanText); return parsedObj.jawaban; } catch (e) { - console.error("JSON parsing error:", e); // Log error parsing if it occurs return msg.text; // Return an empty string if there is an error } diff --git a/src/Dashboard.js b/src/Dashboard.js index 2ca35a8..87d7ca5 100644 --- a/src/Dashboard.js +++ b/src/Dashboard.js @@ -15,6 +15,7 @@ const Dashboard = () => { const [discussedTopics, setDiscussedTopics] = useState([]); const [modalContent, setModalContent] = useState(null); const [rawData, setRawData] = useState([]); + const [loading, setLoading] = useState(true); // ⬅️ Tambahkan state loading const [stats, setStats] = useState({ totalChats: 0, @@ -24,9 +25,10 @@ const Dashboard = () => { const [isDragging, setIsDragging] = useState(false); const [selectedFile, setSelectedFile] = useState(null); - const [isLoggedIn, setIsLoggedIn] = useState(false); + const navigate = useNavigate(); + const handleFile = (file) => { if (file) { setSelectedFile(file); @@ -41,17 +43,37 @@ const Dashboard = () => { const handleLogout = () => { localStorage.removeItem('token'); localStorage.removeItem('user'); - window.location.reload(); // Bisa juga: window.location.href = '/login'; + window.location.reload(); }; useEffect(() => { - async function fetchStats() { - try { - const response = await fetch('https://bot.kediritechnopark.com/webhook/master-agent/dashboard'); - const data = await response.json(); - setDiscussedTopics(data[0]?.result?.topics) + const fetchData = async () => { + const token = localStorage.getItem('token'); - const graphObj = data[0].result.graph; + try { + const response = await fetch('https://bot.kediritechnopark.com/webhook/profile', { + method: 'POST', + headers: { + 'Content-Type': 'application/json', + 'Authorization': `Bearer ${token}`, + }, + }); + + if (response.status === 401 || response.status === 403) { + localStorage.removeItem('token'); + navigate('/login'); + return; + } + + if (!response.ok) { + throw new Error('Fetch gagal dengan status: ' + response.status); + } + + const data = await response.json(); + console.log(data); + setDiscussedTopics(data?.result?.topics) + + const graphObj = data.result.graph; const rawDataArray = Object.entries(graphObj).map(([hour, sesi]) => ({ hour, sesi, @@ -76,47 +98,10 @@ const Dashboard = () => { userMessages, botMessages, }); - - } catch (error) { - console.error('Failed to fetch dashboard data:', error); - } - } - - fetchStats(); - }, []); - -const navigate = useNavigate(); - - useEffect(() => { - const fetchData = async () => { - const token = localStorage.getItem('token'); - - try { - const response = await fetch('https://bot.kediritechnopark.com/webhook/profile', { - method: 'POST', - headers: { - 'Content-Type': 'application/json', - 'Authorization': `Bearer ${token}`, - }, - }); - - if (response.status === 401 || response.status === 403) { - // Token tidak valid atau tidak punya akses, redirect ke login - localStorage.removeItem('token'); // Hapus token agar bersih - navigate('/login'); - return; - } - - if (!response.ok) { - throw new Error('Fetch gagal dengan status: ' + response.status); - } - - const data = await response.json(); - console.log(data); - // lanjutkan penggunaan data + + setLoading(false); // ⬅️ Setelah berhasil, hilangkan loading } catch (error) { console.error('Error:', error); - // Bisa juga redirect ke login kalau error tertentu navigate('/login'); } }; @@ -212,6 +197,11 @@ const navigate = useNavigate(); }); }, [rawData]); + // ⬇️ Jika masih loading, tampilkan full white screen + if (loading) { + return
; + } + return (