class Interface extends React.Component { constructor(props) { super(props); this.state = { realtime: this.getCookie(), resetting: false, opstate: props.opstate } this.polling = false; this.isSecure = (window.location.protocol === 'https:'); if (this.getCookie()) { this.startTimer(); } } startTimer = () => { this.setState({realtime: true}) this.polling = setInterval(() => { this.setState({fetching: true, resetting: false}); axios.get(window.location.pathname, {time: Date.now()}) .then((response) => { this.setState({opstate: response.data}); }); }, this.props.realtimeRefresh * 1000); } stopTimer = () => { this.setState({realtime: false, resetting: false}) clearInterval(this.polling) } realtimeHandler = () => { const realtime = !this.state.realtime; if (!realtime) { this.stopTimer(); this.removeCookie(); } else { this.startTimer(); this.setCookie(); } } resetHandler = () => { if (this.state.realtime) { this.setState({resetting: true}); axios.get(window.location.pathname, {params: {reset: 1}}) .then((response) => { console.log('success: ', response.data); }); } else { window.location.href = '?reset=1'; } } setCookie = () => { let d = new Date(); d.setTime(d.getTime() + (this.props.cookie.ttl * 86400000)); document.cookie = `${this.props.cookie.name}=true;expires=${d.toUTCString()};path=/${this.isSecure ? ';secure' : ''}`; } removeCookie = () => { document.cookie = `${this.props.cookie.name}=;expires=Thu, 01 Jan 1970 00:00:01 GMT;path=/${this.isSecure ? ';secure' : ''}`; } getCookie = () => { const v = document.cookie.match(`(^|;) ?${this.props.cookie.name}=([^;]*)(;|$)`); return v ? !!v[2] : false; }; txt = (text, ...args) => { if (this.props.language !== null && this.props.language.hasOwnProperty(text) && this.props.language[text]) { text = this.props.language[text]; } args.forEach((arg, i) => { text = text.replaceAll(`{${i}}`, arg); }); return text; }; render() { const { opstate, realtimeRefresh, ...otherProps } = this.props; return ( <>