import React, { useEffect, useState } from "react"; import MultiLevelDropdown from "../../../../ui/inputs/MultiLevelDropDown"; import { AddIcon } from "../../../../icons/ExportCommonIcons"; import RegularDropDown from "../../../../ui/inputs/RegularDropDown"; import useChartStore from "../../../../../store/visualization/useChartStore"; import { useSelectedZoneStore } from "../../../../../store/visualization/useZoneStore"; import { useWidgetStore } from "../../../../../store/useWidgetStore"; import axios from "axios"; import RenameInput from "../../../../ui/inputs/RenameInput"; import { useParams } from "react-router-dom"; type Props = {}; const Progress1Input = (props: Props) => { const [widgetName, setWidgetName] = useState("Widget"); const { setMeasurements, updateDuration, updateName } = useChartStore(); const [duration, setDuration] = useState("1h"); const [dropDowndata, setDropDownData] = useState({}); const [selections, setSelections] = useState< Record >({}); const { selectedZone } = useSelectedZoneStore(); const { selectedChartId } = useWidgetStore(); const iotApiUrl = process.env.REACT_APP_IOT_SOCKET_SERVER_URL; const email = localStorage.getItem("email") || ""; const organization = email?.split("@")[1]?.split(".")[0]; const [isLoading, setLoading] = useState(true); const { projectId } = useParams(); useEffect(() => { const fetchZoneData = async () => { try { setLoading(true); const response = await axios.get(`http://${iotApiUrl}/floatinput`); if (response.status === 200) { // console.log("dropdown data:", response.data); setDropDownData(response.data); setLoading(false); } else { console.log("Unexpected response:", response); } } catch (error) { echo.error("Failed to fetch zone data"); console.error("There was an error!", error); } }; fetchZoneData(); }, []); useEffect(() => { const fetchSavedInputes = async () => { if (selectedChartId.id !== "") { try { const response = await axios.get( `http://${process.env.REACT_APP_SERVER_REST_API_BASE_URL}/api/V1/widget/data?widgetID=${selectedChartId.id}&zoneUuid=${selectedZone.zoneUuid}&projectId=${projectId}`, { headers: { Authorization: "Bearer ", // Replace with actual token "Content-Type": "application/json", token: localStorage.getItem("token") || "", // Coerce null to empty string refresh_token: localStorage.getItem("refreshToken") || "", }, } ); if (response.status === 200) { setSelections(response.data.Data.measurements); setDuration(response.data.Data.duration); setWidgetName(response.data.widgetName); } else { console.log("Unexpected response:", response); } } catch (error) { echo.error("Failed to fetch saved inputs"); console.error("There was an error!", error); } } }; fetchSavedInputes(); }, [selectedChartId.id]); // Sync Zustand state when component mounts useEffect(() => { setMeasurements(selections); updateDuration(duration); updateName(widgetName); }, [selections, duration, widgetName]); const sendInputes = async ( inputMeasurement: any, inputDuration: any, inputName: any ) => { try { const response = await axios.post( `http://${process.env.REACT_APP_SERVER_REST_API_BASE_URL}/api/V1/widget/save`, { headers: { Authorization: "Bearer ", // Replace with actual token "Content-Type": "application/json", token: localStorage.getItem("token") || "", // Coerce null to empty string refresh_token: localStorage.getItem("refreshToken") || "", }, }, { organization: organization, zoneUuid: selectedZone.zoneUuid, widget: { id: selectedChartId.id, panel: selectedChartId.panel, widgetName: inputName, Data: { measurements: inputMeasurement, duration: inputDuration, }, }, } as any ); if (response.status === 200) { return true; } else { console.log("Unexpected response:", response); return false; } } catch (error) { echo.error("Failed to send input"); console.error("There was an error!", error); return false; } }; const handleSelect = async ( inputKey: string, selectedData: { name: string; fields: string } | null ) => { // async() => { const newSelections = { ...selections }; if (selectedData === null) { delete newSelections[inputKey]; } else { newSelections[inputKey] = selectedData; } // setMeasurements(newSelections); // Update Zustand store console.log(newSelections); if (await sendInputes(newSelections, duration, widgetName)) { setSelections(newSelections); } }; const handleSelectDuration = async (option: string) => { if (await sendInputes(selections, option, widgetName)) { setDuration(option); } }; const handleNameChange = async (name: any) => { if (await sendInputes(selections, duration, name)) { setWidgetName(name); } }; return ( <>
Title
{[...Array(1)].map((_, index) => { const inputKey = `input${index + 1}`; return (
Input {index + 1}
handleSelect(inputKey, selectedData) } onUnselect={() => handleSelect(inputKey, null)} selectedValue={selections[inputKey]} // Load from Zustand isLoading={isLoading} allSelections={selections} />
); })}
{/*
Duration
*/} ); }; export default Progress1Input;