diff --git a/src/components/Common/KilnInfo/Kiln.jsx b/src/components/Common/KilnInfo/Kiln.jsx index d72b08d..05c8a65 100644 --- a/src/components/Common/KilnInfo/Kiln.jsx +++ b/src/components/Common/KilnInfo/Kiln.jsx @@ -1,9 +1,8 @@ import cls from "./kiln.module.scss"; import Container from "../../Container"; -import { useSelector } from "react-redux"; -import { stateNameMap } from "../../../store/features/kilnSlice"; import { useEffect } from "react"; -import { useDispatch } from "react-redux"; +import { useSelector, useDispatch } from "react-redux"; +import { stateNameMap } from "../../../store/features/kilnSlice"; export default function Kiln() { const kilnInfo = useSelector((state) => state.kiln); diff --git a/src/components/Common/TimeFireDir/index.jsx b/src/components/Common/TimeFireDir/index.jsx index 8174493..765a0bf 100644 --- a/src/components/Common/TimeFireDir/index.jsx +++ b/src/components/Common/TimeFireDir/index.jsx @@ -1,99 +1,121 @@ +import cls from "./leftbox.module.scss"; import React, { useState, useContext, useEffect } from "react"; -// import SocketContext from '../../../store/socket-data-provider'; +import { useSelector, useDispatch } from "react-redux"; + import icon1 from "../../../assets/CenterChart2icon1.svg"; import icon2 from "../../../assets/CenterChart2icon2.svg"; import icon3 from "../../../assets/CenterChart2icon3.svg"; -import icon4 from "../../../assets/CenterChart2icon4.svg"; +// import icon4 from "../../../assets/CenterChart2icon4.svg"; -import cls from "./leftbox.module.scss"; +import useTimeCounter from "../../../hooks/useTimeCounter"; -const Chart2 = () => { - // const ctx = useContext(SocketContext); - const ctx = null; +const FireInfo = () => { + const dispatch = useDispatch(); + const fireInfo = useSelector((state) => state.fireInfo); + const time = fireInfo.lastFireChangeTime || "0分0秒"; + const [min, sec] = useTimeCounter(time); - let [time, setTime] = useState([0, 0]); + // useEffect(() => { + // const restTime = ctx?.runState?.lastFireChangeTime; + // if (restTime == null) return; + // let timer = null; + // if (/分/.test(restTime) && /秒/.test(restTime)) { + // let [min, sec] = restTime.replace(/分/, ":").replace(/秒/, "").split(":"); + // timer = setInterval(() => { + // if (Number(sec) === 0 && Number(min) === 0) { + // clearInterval(timer); + // return; + // } + // if (Number(sec) === 0) { + // sec = 59; + // min--; + // } else { + // sec--; + // } + // setTime([min, sec]); + // }, 1000); + // } else if (/分/.test(restTime)) { + // let sec, + // min = restTime.replace(/分/, ":"); + // sec = 0; + + // timer = setInterval(() => { + // if (Number(sec) === 0 && Number(min) === 0) { + // clearInterval(timer); + // return; + // } + // if (Number(sec) === 0) { + // sec = 59; + // min--; + // } else { + // sec--; + // } + // setTime([min, sec]); + // }, 1000); + // } else if (/秒/.test(restTime)) { + // let min, + // sec = restTime.replace(/秒/, ""); + // min = 0; + + // timer = setInterval(() => { + // if (Number(sec) === 0 && Number(min) === 0) { + // clearInterval(timer); + // return; + // } + // if (Number(sec) === 0) { + // sec = 59; + // min--; + // } else { + // sec--; + // } + // setTime([min, sec]); + // }, 1000); + // } + // return () => { + // clearInterval(timer); + // }; + // }, [ctx?.runState?.lastFireChangeTime]); useEffect(() => { - const restTime = ctx?.runState?.lastFireChangeTime; - if (restTime == null) return; - let timer = null; - if (/分/.test(restTime) && /秒/.test(restTime)) { - let [min, sec] = restTime.replace(/分/, ":").replace(/秒/, "").split(":"); - timer = setInterval(() => { - if (Number(sec) === 0 && Number(min) === 0) { - clearInterval(timer); - return; - } - if (Number(sec) === 0) { - sec = 59; - min--; - } else { - sec--; - } - setTime([min, sec]); - }, 1000); - } else if (/分/.test(restTime)) { - let sec, - min = restTime.replace(/分/, ":"); - sec = 0; - - timer = setInterval(() => { - if (Number(sec) === 0 && Number(min) === 0) { - clearInterval(timer); - return; - } - if (Number(sec) === 0) { - sec = 59; - min--; - } else { - sec--; - } - setTime([min, sec]); - }, 1000); - } else if (/秒/.test(restTime)) { - let min, - sec = restTime.replace(/秒/, ""); - min = 0; - - timer = setInterval(() => { - if (Number(sec) === 0 && Number(min) === 0) { - clearInterval(timer); - return; - } - if (Number(sec) === 0) { - sec = 59; - min--; - } else { - sec--; - } - setTime([min, sec]); - }, 1000); - } - return () => { - clearInterval(timer); - }; - }, [ctx?.runState?.lastFireChangeTime]); + setInterval(() => { + dispatch({ + type: "fireInfo/setFireInfo", + payload: { + fireChangeTime: `${Math.ceil(Math.random() * 10)}:${Math.ceil( + Math.random() * 10 + )}`, + fireDirection: Math.random * 10 < 5 ? "东火" : "西火", + lastFireChangeTime: `${Math.ceil(Math.random() * 60)}分${Math.ceil( + Math.random() * 50 + )}秒`, + }, + }); + }, 10000); + }, []); const data = [ { icon: icon1, label: "换火时间", - value: ctx?.runState?.fireChangeTime || "00:00", + // value: ctx?.runState?.fireChangeTime || "00:00", + value: fireInfo.fireChangeTime || "00:00", }, { icon: icon3, label: "剩余时间", - value: `${time[0]}分${time[1]}秒`, + // value: `${time[0]}分${time[1]}秒`, + value: `${min}分${sec}秒`, }, { icon: icon2, label: "当前火向", - value: ctx?.runState?.fireDirection || "东火", + // value: ctx?.runState?.fireDirection || "东火", + value: fireInfo.fireDirection || "东火", }, ]; return ( -