edge_frame/
wifi_setup.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
use std::rc::Rc;

use yew::prelude::*;
use yew_router::Routable;
use yewdux::use_store_value;
use yewdux_middleware::*;

use crate::frame::{RouteNavItem, RouteStatusItem};
use crate::wifi::{Wifi, WifiState};

pub use crate::wifi::{WifiConf, WifiConfScope};

#[derive(Default, Clone, Debug, Eq, PartialEq, Store)]
pub struct WifiConfStore(pub Option<WifiConf>);

impl Reducer<WifiConfStore> for WifiConf {
    fn apply(self, mut store: Rc<WifiConfStore>) -> Rc<WifiConfStore> {
        let state = Rc::make_mut(&mut store);

        state.0 = Some(self);

        store
    }
}

#[derive(Properties, Clone, Debug, PartialEq, Eq)]
pub struct WifiNavItemProps<R: Routable + PartialEq + Clone + 'static> {
    pub route: R,
}

#[function_component(WifiNavItem)]
pub fn wifi_nav_item<R: Routable + PartialEq + Clone + 'static>(
    props: &WifiNavItemProps<R>,
) -> Html {
    html! {
        <RouteNavItem<R>
            text="Wifi"
            icon="fa-solid fa-wifi"
            route={props.route.clone()}/>
    }
}

#[derive(Properties, Clone, Debug, PartialEq)]
pub struct WifiStatusItemProps<R: Routable + PartialEq + Clone + 'static> {
    pub route: R,
}

#[function_component(WifiStatusItem)]
pub fn wifi_status_item<R: Routable + PartialEq + Clone + 'static>(
    props: &WifiStatusItemProps<R>,
) -> Html {
    html! {
        <RouteStatusItem<R>
            icon="fa-lg fa-solid fa-wifi"
            route={props.route.clone()}/>
    }
}

#[derive(Properties, Clone, Debug, PartialEq)]
pub struct WifiSetupProps {
    #[prop_or_default]
    pub conf_scope: WifiConfScope,

    #[prop_or_default]
    pub mobile: bool,
}

#[function_component(WifiSetup)]
pub fn wifi_setup(props: &WifiSetupProps) -> Html {
    let mcx = use_mcx();
    let conf_store = use_store_value::<WifiConfStore>();

    let state = use_state(|| WifiState::Unchanged);

    let conf = conf_store.0.as_ref().cloned().unwrap_or(Default::default());

    let state_changed = {
        let state = state.clone();

        Callback::from(move |new_state| state.set(new_state))
    };

    let onclick = {
        let state = state.clone();

        Callback::from(move |_| {
            if let WifiState::Conf(conf) = (*state).clone() {
                mcx.invoke(Some(conf));
            }
        })
    };

    html! {
        <div class="container">
        <Wifi conf={conf} conf_scope={props.conf_scope} mobile={props.mobile} state_changed={state_changed}/>

        <input
            type="button"
            class={"button my-4"}
            value="Save"
            disabled={!matches!(&*state, WifiState::Conf(_))}
            {onclick}
        />
        </div>
    }
}