Skip to content

Commit

Permalink
Refactor todo example
Browse files Browse the repository at this point in the history
  • Loading branch information
RandomInsano committed Jan 4, 2025
1 parent 69e3b5f commit f3e28e8
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 47 deletions.
19 changes: 11 additions & 8 deletions examples/todomvc/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,7 @@ impl Component for App {

fn create(_ctx: &Context<Self>) -> Self {
let entries = LocalStorage::get(KEY).unwrap_or_else(|_| Vec::new());
let state = State {
entries,
filter: Filter::All,
edit_value: "".into(),
};
let state = State::new(entries);
let focus_ref = NodeRef::default();
Self { state, focus_ref }
}
Expand Down Expand Up @@ -79,10 +75,10 @@ impl Component for App {
}
Msg::ToggleAll => {
let status = !self.state.is_all_completed();
self.state.toggle_all(status);
self.state.set_completed(status);
}
Msg::Toggle(idx) => {
self.state.toggle(idx);
self.state.toggle_completed(idx);
}
Msg::ClearCompleted => {
self.state.clear_completed();
Expand Down Expand Up @@ -120,7 +116,14 @@ impl Component for App {
/>
<label for="toggle-all" />
<ul class="todo-list">
{ for self.state.entries.iter().filter(|e| self.state.filter.fits(e)).enumerate().map(|e| self.view_entry(e, ctx.link())) }
{ for self
.state
.entries
.iter()
.enumerate()
.filter(|(_, entry)| self.state.filter.fits(entry))
.map(|(i, e)| self.view_entry((i, e), ctx.link()))
}
</ul>
</section>
<footer class={classes!("footer", hidden_class)}>
Expand Down
53 changes: 14 additions & 39 deletions examples/todomvc/src/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,14 @@ pub struct State {
}

impl State {
pub fn new(entries: Vec<Entry>) -> Self {
Self {
entries,
filter: Filter::All,
edit_value: "".into(),
}
}

pub fn total(&self) -> usize {
self.entries.len()
}
Expand All @@ -37,26 +45,15 @@ impl State {
}

pub fn clear_completed(&mut self) {
let entries = self
.entries
.drain(..)
.filter(|e| Filter::Active.fits(e))
.collect();
self.entries = entries;
self.entries.retain(|e| Filter::Active.fits(e));
}

pub fn toggle(&mut self, idx: usize) {
let filter = self.filter;
let entry = self
.entries
.iter_mut()
.filter(|e| filter.fits(e))
.nth(idx)
.unwrap();
pub fn toggle_completed(&mut self, idx: usize) {
let entry = self.entries.get_mut(idx).unwrap();
entry.completed = !entry.completed;
}

pub fn toggle_all(&mut self, value: bool) {
pub fn set_completed(&mut self, value: bool) {
for entry in &mut self.entries {
if self.filter.fits(entry) {
entry.completed = value;
Expand All @@ -65,13 +62,7 @@ impl State {
}

pub fn toggle_edit(&mut self, idx: usize) {
let filter = self.filter;
let entry = self
.entries
.iter_mut()
.filter(|e| filter.fits(e))
.nth(idx)
.unwrap();
let entry = self.entries.get_mut(idx).unwrap();
entry.editing = !entry.editing;
}

Expand All @@ -85,29 +76,13 @@ impl State {
if val.is_empty() {
self.remove(idx);
} else {
let filter = self.filter;
let entry = self
.entries
.iter_mut()
.filter(|e| filter.fits(e))
.nth(idx)
.unwrap();
let entry = self.entries.get_mut(idx).unwrap();
entry.description = val;
entry.editing = !entry.editing;
}
}

pub fn remove(&mut self, idx: usize) {
let idx = {
let entries = self
.entries
.iter()
.enumerate()
.filter(|&(_, e)| self.filter.fits(e))
.collect::<Vec<_>>();
let &(idx, _) = entries.get(idx).unwrap();
idx
};
self.entries.remove(idx);
}
}
Expand Down

0 comments on commit f3e28e8

Please sign in to comment.