Skip to content

Commit

Permalink
Add save icon
Browse files Browse the repository at this point in the history
  • Loading branch information
vaijira committed Oct 17, 2024
1 parent 9c9597b commit 9e57f4c
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 27 deletions.
72 changes: 52 additions & 20 deletions src/table.rs
Original file line number Diff line number Diff line change
@@ -1,4 1,4 @@
use std::sync::Arc;
use std::{sync::Arc, usize::MAX};

Check warning on line 1 in src/table.rs

View workflow job for this annotation

GitHub Actions / Format & Clippy (1.80.1)

unused import: `usize::MAX`

Check warning on line 1 in src/table.rs

View workflow job for this annotation

GitHub Actions / Format & Clippy (1.80.1)

importing a legacy numeric constant

Check warning on line 1 in src/table.rs

View workflow job for this annotation

GitHub Actions / Unit Tests on 1.80.1

unused import: `usize::MAX`

Check warning on line 1 in src/table.rs

View workflow job for this annotation

GitHub Actions / Unit Tests on 1.80.1

unused import: `usize::MAX`

use dominator::{clone, events, html, with_node, Dom};
use futures_signals::{
Expand All @@ -11,7 11,10 @@ use web_sys::HtmlElement;
use crate::{
css::TABLE_ROW,
data::Aeat720Record,
utils::{icons::{render_svg_delete_square_icon, render_svg_edit_icon}, usize_to_date},
utils::{
icons::{render_svg_delete_square_icon, render_svg_edit_icon, render_svg_save_icon},
usize_to_date,
},
};

pub struct Table {
Expand All @@ -22,6 25,8 @@ pub struct Table {

impl Table {
pub fn new(aeat720_records: MutableVec<Aeat720Record>) -> Arc<Self> {
let editable = MutableVec::new_with_values(vec![false; aeat720_records.lock_ref().len()]);

Check warning on line 28 in src/table.rs

View workflow job for this annotation

GitHub Actions / Format & Clippy (1.80.1)

unused variable: `editable`

Check warning on line 28 in src/table.rs

View workflow job for this annotation

GitHub Actions / Unit Tests on 1.80.1

unused variable: `editable`

Check warning on line 28 in src/table.rs

View workflow job for this annotation

GitHub Actions / Unit Tests on 1.80.1

unused variable: `editable`

Arc::new(Self {
headers: vec![
"Nombre compañía",
Expand Down Expand Up @@ -84,6 89,24 @@ impl Table {
let date = usize_to_date(record.first_tx_date)
.map_or("".to_string(), |d| d.format("%d/%m/%Y").to_string());

let action_span = html!("span" => HtmlElement, {
.child(render_svg_edit_icon("red", "24"))
.with_node!(_element => {
.event(clone!(this => move |_: events::Click| {
*this.editable.lock_mut() = true;
}))
})
});

let delete_span = html!("span" => HtmlElement, {
.child(render_svg_delete_square_icon("red", "24"))
.with_node!(_element => {
.event(clone!(this => move |_: events::Click| {
this.data.lock_mut().remove(index);
}))
})
});

html!("tr", {
.class(&*TABLE_ROW)
.children(&mut [
Expand Down Expand Up @@ -112,14 135,9 @@ impl Table {
.text(&record.percentage.to_string())
.text("%")
}),
html!("td" => HtmlElement, {
.child(render_svg_edit_icon("red", "24"))
.child(render_svg_delete_square_icon("red", "24"))
.with_node!(_element => {
.event(clone!(this => move |_: events::Click| {
this.data.lock_mut().remove(index);
}))
})
html!("td", {
.child(action_span)
.child(delete_span)
}),
])
})
Expand All @@ -129,6 147,24 @@ impl Table {
let date = usize_to_date(record.first_tx_date)
.map_or("".to_string(), |d| d.format("%d/%m/%Y").to_string());

let action_span = html!("span" => HtmlElement, {
.child(render_svg_save_icon("red", "24"))
.with_node!(_element => {
.event(clone!(this => move |_: events::Click| {
*this.editable.lock_mut() = false;
}))
})
});

let delete_span = html!("span" => HtmlElement, {
.child(render_svg_delete_square_icon("red", "24"))
.with_node!(_element => {
.event(clone!(this => move |_: events::Click| {
this.data.lock_mut().remove(index);
}))
})
});

html!("tr", {
.class(&*TABLE_ROW)
.children(&mut [
Expand Down Expand Up @@ -178,13 214,9 @@ impl Table {
}))
.text("%")
}),
html!("td" => HtmlElement, {
.child(render_svg_delete_square_icon("red", "24"))
.with_node!(_element => {
.event(clone!(this => move |_: events::Click| {
this.data.lock_mut().remove(index);
}))
})
html!("td", {
.child(action_span)
.child(delete_span)
}),
])
})
Expand All @@ -194,11 226,11 @@ impl Table {
html!("tbody", {
.children_signal_vec(this.data.signal_vec_cloned()
.enumerate().map(clone!(this => move |(index, record)| {
let i = index.get().unwrap_or(usize::MAX);
if this.editable.get() {

Table::render_editable_row(&this, index.get().unwrap_or(usize::MAX), &record)
Table::render_editable_row(&this, i, &record)
} else {
Table::render_row(&this, index.get().unwrap_or(usize::MAX), &record)
Table::render_row(&this, i, &record)
}
}))
)
Expand Down
18 changes: 11 additions & 7 deletions src/utils/icons.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 2,7 @@ use dominator::{svg, Dom, DomBuilder};
use web_sys::SvgElement;

fn svg_icon_attrs(icon: DomBuilder<SvgElement>) -> DomBuilder<SvgElement> {
icon
.attr("viewBox", "0 0 24 24")
icon.attr("viewBox", "0 0 24 24")
.attr("fill", "none")
.attr("stroke-width", "2")
.attr("stroke-linecap", "round")
Expand All @@ -12,16 11,21 @@ fn svg_icon_attrs(icon: DomBuilder<SvgElement>) -> DomBuilder<SvgElement> {

pub fn render_svg_save_icon(color: &str, size: &str) -> Dom {
svg!("svg", {
.attr("alt", "edit icon")
.attr("alt", "save icon")
.attr("width", size)
.attr("height", size)
.attr("stroke", color)
.apply(svg_icon_attrs)
.children(&mut[
svg!("path", {
.attr("d", "M11 4H4a2 2 0 0 0-2 2v14a2 2 0 0 0 2 2h14a2 2 0 0 0 2-2v-7")
.attr("d", "M18.5 2.5a2.121 2.121 0 0 1 3 3L12 15l-4 1 1-4 9.5-9.5z")
})
.attr("d", "M19 21H5a2 2 0 0 1-2-2V5a2 2 0 0 1 2-2h11l5 5v11a2 2 0 0 1-2 2z")
}),
svg!("polyline", {
.attr("points", "17 21 17 13 7 13 7 21")
}),
svg!("polyline", {
.attr("points", "7 3 7 8 15 8")
}),
])
})
}
Expand All @@ -39,7 43,7 @@ pub fn render_svg_edit_icon(color: &str, size: &str) -> Dom {
}),
svg!("path", {
.attr("d", "M18.5 2.5a2.121 2.121 0 0 1 3 3L12 15l-4 1 1-4 9.5-9.5z")
})
}),
])
})
}
Expand Down

1 comment on commit 9e57f4c

@github-actions
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.