import React, { useState } from "react"; import "./HeadCount.css"; interface ApiResponse { total_unique_faces: number; daily_counts: { date: string; unique_faces: number }[]; } interface DateTimeRange { date: string; time: string; } const HeadCount: React.FC = () => { const [from, setFrom] = useState({ date: "", time: "" }); const [to, setTo] = useState({ date: "", time: "" }); const [count, setCount] = useState(null); const [loading, setLoading] = useState(false); const [error, setError] = useState(null); const [dailyCounts, setDailyCounts] = useState< { date: string; count: number }[] >([]); const handleInputChange = ( e: React.ChangeEvent, field: "from" | "to" ) => { const { name, value } = e.target; if (field === "from") { setFrom((prev) => ({ ...prev, [name]: value })); } else { setTo((prev) => ({ ...prev, [name]: value })); } }; const formatDateTime = (date: string, time: string) => { return `${date}T${time}:00+00:00`; }; const handleSubmit = async (e: React.FormEvent) => { e.preventDefault(); if (!from.date || !from.time || !to.date || !to.time) { setError("Please fill in all date and time fields."); return; } setLoading(true); setError(null); const start = formatDateTime(from.date, from.time); const end = formatDateTime(to.date, to.time); console.log(start, end); try { const response = await fetch( `${ process.env.NEXT_PUBLIC_BASE_URL }/face/headcount?start_time=${encodeURIComponent( start )}&end_time=${encodeURIComponent(end)}`, { method: "GET", headers: { "Content-Type": "application/json", }, } ); if (!response.ok) { throw new Error("Failed to fetch data"); } const data: ApiResponse = await response.json(); setCount(data.total_unique_faces); if (data?.daily_counts) { setDailyCounts( data.daily_counts.map((d) => ({ date: d.date, count: d.unique_faces, })) ); } } catch (err) { setError("An error occurred while fetching data."); console.error(err); } finally { setLoading(false); } }; return (

Head Count

handleInputChange(e, "from")} required className="input" />
handleInputChange(e, "from")} required className="input" />
handleInputChange(e, "to")} required className="input" />
handleInputChange(e, "to")} required className="input" />
{error &&

{error}

} {count && (

Total Unique Face Count:

    {count}
)} {dailyCounts?.length > 0 && (

Daily Counts:

    {dailyCounts.map((item, index) => (
  • {item.date}: {item.count}
  • ))}
)}
); }; export default HeadCount;