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
use crate::Todo;
use mox::mox;
use moxie_dom::{
    elements::{
        html::*,
        text_content::{Li, Ul},
    },
    prelude::*,
};
use Visibility::{Active, All, Completed};

#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub enum Visibility {
    All,
    Active,
    Completed,
}

impl Default for Visibility {
    fn default() -> Self {
        All
    }
}

impl std::fmt::Display for Visibility {
    fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
        f.write_str(match self {
            All => "All",
            Active => "Active",
            Completed => "Completed",
        })
    }
}

impl Visibility {
    pub fn should_show(self, todo: &Todo) -> bool {
        match self {
            All => true,
            Active => !todo.completed,
            Completed => todo.completed,
        }
    }
}

#[topo::nested]
#[illicit::from_env(visibility: &Key<Visibility>)]
pub fn filter_link(to_set: Visibility) -> Li {
    let visibility = visibility.clone();
    mox! {
        <li>
            <a style="cursor: pointer;"
             class = if *visibility == to_set { "selected" } else { "" }
             onclick = move |_| visibility.set(to_set)>
                { to_set }
            </a>
        </li>
    }
}

#[topo::nested]
pub fn filter() -> Ul {
    let mut list = ul();
    list = list.class("filters");
    for &to_set in &[All, Active, Completed] {
        list = list.child(filter_link(to_set));
    }

    list.build()
}