1515 * along with VILLASweb. If not, see <http://www.gnu.org/licenses/>.
1616 ******************************************************************************/
1717
18- import React , { useState , useEffect } from "react" ;
18+ import React , { useState , useEffect , useRef } from "react" ;
1919import { Button } from "react-bootstrap" ;
20-
21- const WidgetButton = ( { widget, editing } ) => {
20+ import { useUpdateWidgetMutation } from "../../../../store/apiSlice" ;
21+ const WidgetButton = ( { widget, editing, onInputChanged } ) => {
2222 const [ pressed , setPressed ] = useState ( widget . customProperties . pressed ) ;
23+ const [ toggle , setToggle ] = useState ( widget . customProperties . toggle ) ;
24+ const [ updateWidget ] = useUpdateWidgetMutation ( ) ;
2325
24- // useEffect(() => {
25- // let widget = props.widget;
26- // widget.customProperties.simStartedSendValue = false;
27- // widget.customProperties.pressed = false;
28-
29- // // AppDispatcher.dispatch({
30- // // type: 'widgets/start-edit',
31- // // token: props.token,
32- // // data: widget
33- // // });
34-
35- // // Effect cleanup
36- // return () => {
37- // // Clean up if needed
38- // };
39- // }, [props.token, props.widget]);
40-
41- // useEffect(() => {
42- // if (props.widget.customProperties.simStartedSendValue) {
43- // let widget = props.widget;
44- // widget.customProperties.simStartedSendValue = false;
45- // widget.customProperties.pressed = false;
46- // AppDispatcher.dispatch({
47- // type: 'widgets/start-edit',
48- // token: props.token,
49- // data: widget
50- // });
51-
52- // props.onInputChanged(widget.customProperties.off_value, '', false, false);
53- // }
54- // }, [props, setPressed]);
55-
56- // useEffect(() => {
57- // setPressed(props.widget.customProperties.pressed);
58- // }, [props.widget.customProperties.pressed]);
26+ //this ref is used for capturing last value of pressed so that it can be saved and sent on unmount
27+ const pressedRef = useRef ( pressed ) ;
5928
60- // const onPress = (e) => {
61- // if (e.button === 0 && !props.widget.customProperties.toggle) {
62- // valueChanged(props.widget.customProperties.on_value, true);
63- // }
64- // };
65-
66- // const onRelease = (e) => {
67- // if (e.button === 0) {
68- // let nextState = false;
69- // if (props.widget.customProperties.toggle) {
70- // nextState = !pressed;
71- // }
72- // valueChanged(nextState ? props.widget.customProperties.on_value : props.widget.customProperties.off_value, nextState);
73- // }
74- // };
75-
76- // const valueChanged = (newValue, newPressed) => {
77- // if (props.onInputChanged) {
78- // props.onInputChanged(newValue, 'pressed', newPressed, true);
79- // }
80- // setPressed(newPressed);
81- // };
29+ useEffect ( ( ) => {
30+ return ( ) => {
31+ //if button is in toggle-mode, we want to save its pressed state for future reloads of dashboard
32+ if ( toggle ) updateSimStartedAndPressedValues ( false , pressedRef . current ) ;
33+ } ;
34+ } , [ ] ) ;
8235
8336 useEffect ( ( ) => {
84- updateSimStartedAndPressedValues ( false , false ) ;
37+ setToggle ( widget . customProperties . toggle ) ;
8538 } , [ widget ] ) ;
8639
8740 const updateSimStartedAndPressedValues = async ( isSimStarted , isPressed ) => {
@@ -105,13 +58,16 @@ const WidgetButton = ({ widget, editing }) => {
10558 } ;
10659
10760 useEffect ( ( ) => {
108- if ( widget . customProperties . simStartedSendValue ) {
109- let widgetCopy = { ...widget } ;
110- widgetCopy . customProperties . simStartedSendValue = false ;
111- widgetCopy . customProperties . pressed = false ;
112-
113- onInputChanged ( widget . customProperties . off_value , "" , false , false ) ;
114- }
61+ pressedRef . current = pressed ;
62+
63+ onInputChanged (
64+ pressed
65+ ? widget . customProperties . on_value
66+ : widget . customProperties . off_value ,
67+ "" ,
68+ false ,
69+ false
70+ ) ;
11571 } , [ pressed ] ) ;
11672
11773 let opacity = widget . customProperties . background_color_opacity ;
@@ -129,8 +85,13 @@ const WidgetButton = ({ widget, editing }) => {
12985 style = { buttonStyle }
13086 active = { pressed }
13187 disabled = { editing }
132- onMouseDown = { ( e ) => setPressed ( true ) }
133- onMouseUp = { ( e ) => setPressed ( false ) }
88+ onMouseDown = { ( e ) => {
89+ if ( ! toggle ) setPressed ( true ) ;
90+ else setPressed ( ! pressed ) ;
91+ } }
92+ onMouseUp = { ( e ) => {
93+ if ( ! toggle ) setPressed ( false ) ;
94+ } }
13495 >
13596 { widget . name }
13697 </ Button >
0 commit comments