Skip to content

Commit 4df9f63

Browse files
author
ejoepes
committed
Add records manually.
Posibility to add records manually clicking in icon or in button.
1 parent 75e8a8f commit 4df9f63

File tree

5 files changed

+107
-6
lines changed

5 files changed

+107
-6
lines changed

src/app.rs

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,20 @@ impl App {
138138
})
139139
}
140140

141+
fn render_insert_button(this: &Arc<Self>) -> Dom {
142+
html!("span", {
143+
.child(html!("input" => HtmlInputElement, {
144+
.attr("type", "button")
145+
.attr("value", "Insertar movimiento")
146+
.with_node!(_element => {
147+
.event(clone!(this => move |_: events::Click| {
148+
this.table.add_default();
149+
}))
150+
})
151+
}))
152+
})
153+
}
154+
141155
fn render_download_button(this: &Arc<Self>) -> Dom {
142156
html!("section", {
143157
.child_signal(
@@ -183,7 +197,8 @@ impl App {
183197
}))
184198
.child(PersonalInfoViewer::render(&this.personal_info_viewer))
185199
.child(html!("h2", {
186-
.text("Paso 2: Descarga los informes de Interactive brokers y/o Degiro e importalos.")
200+
.text("Paso 2: Descarga los informes de Interactive brokers y/o Degiro ")
201+
.text("e importalos o añade movimientos manualmente.")
187202
}))
188203
.child(
189204
Table::render(&this.table)
@@ -194,6 +209,9 @@ impl App {
194209
.child(
195210
App::render_clear_button(&this)
196211
)
212+
.child(
213+
App::render_insert_button(&this)
214+
)
197215
.child(html!("h2", {
198216
.text("Paso 3: Revisa las fechas de 1º adquisición y los datos importados y descarga el fichero generado.")
199217
}))

src/data.rs

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,10 @@ use chrono::NaiveDate;
22
use num_format::Locale;
33
use rust_decimal::Decimal;
44
use serde::{Deserialize, Serialize};
5-
use std::{convert::From, sync::Arc};
5+
use std::{
6+
convert::From,
7+
sync::{Arc, LazyLock},
8+
};
69

710
pub type AccountNotes = Vec<AccountNote>;
811
pub type BalanceNotes = Vec<BalanceNote>;
@@ -13,6 +16,13 @@ pub const SPAIN_COUNTRY_CODE: &str = "ES";
1316
pub const DEFAULT_LOCALE: &Locale = &Locale::es;
1417
pub const DEFAULT_NUMBER_OF_DECIMALS: u16 = 2;
1518

19+
pub static DEFAULT_BROKER: LazyLock<Arc<BrokerInformation>> = LazyLock::new(|| {
20+
Arc::new(BrokerInformation {
21+
name: "Desconocido".to_string(),
22+
country_code: "IE".to_string(),
23+
})
24+
});
25+
1626
#[derive(Clone, Debug, Eq, PartialEq, Deserialize, Serialize)]
1727
pub enum BrokerOperation {
1828
Buy,

src/table.rs

Lines changed: 42 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,14 @@ use web_sys::{HtmlElement, HtmlInputElement};
1212

1313
use crate::{
1414
css::{TABLE_CAPTION, TABLE_HEADER, TABLE_ROW, TABLE_STYLE},
15-
data::{Aeat720Record, BrokerInformation, DEFAULT_LOCALE, DEFAULT_NUMBER_OF_DECIMALS},
15+
data::{
16+
Aeat720Record, BrokerInformation, CompanyInfo, DEFAULT_BROKER, DEFAULT_LOCALE,
17+
DEFAULT_NUMBER_OF_DECIMALS, DEFAULT_YEAR,
18+
},
1619
utils::{
20+
date_to_usize,
1721
decimal::{decimal_to_str_locale, valid_str_number_with_decimals},
18-
icons::render_svg_trash_icon,
22+
icons::{render_svg_plus_icon, render_svg_trash_icon},
1923
usize_to_date,
2024
},
2125
};
@@ -77,6 +81,33 @@ impl Table {
7781
}
7882
}
7983

84+
fn create_default_record() -> Aeat720RecordInfo {
85+
let record = Aeat720Record {
86+
company: CompanyInfo {
87+
name: "Nueva compañía".to_string(),
88+
isin: "".to_string(),
89+
},
90+
quantity: Decimal::ONE_HUNDRED,
91+
value_in_euro: Decimal::ZERO,
92+
first_tx_date: date_to_usize(DEFAULT_YEAR as i32, 1, 1),
93+
broker: DEFAULT_BROKER.clone(),
94+
percentage: Decimal::ONE_HUNDRED,
95+
};
96+
Aeat720RecordInfo {
97+
record,
98+
name_err_msg: Mutable::new(None),
99+
isin_err_msg: Mutable::new(Some(ISIN_NOT_VALID_ERR_MSG)),
100+
value_err_msg: Mutable::new(Some(VALUE_NOT_VALID_ERR_MSG)),
101+
quantity_err_msg: Mutable::new(None),
102+
percent_err_msg: Mutable::new(None),
103+
}
104+
}
105+
106+
pub fn add_default(&self) {
107+
let record = Self::create_default_record();
108+
self.data.lock_mut().insert_cloned(0, Mutable::new(record));
109+
}
110+
80111
pub fn get_records(&self) -> Vec<Aeat720Record> {
81112
let mut result = vec![];
82113
for record in self.data.lock_ref().iter() {
@@ -125,7 +156,15 @@ impl Table {
125156
.style("vertical-align", "bottom")
126157
.style("font-weight", "bold")
127158
.style("background-color", "#ddd")
128-
.text("")
159+
.child(html!("span" => HtmlElement, {
160+
.child(render_svg_plus_icon("red", "24"))
161+
.with_node!(_element => {
162+
.event(clone!(this => move |_: events::Click| {
163+
let record_info = Mutable::new(Self::create_default_record());
164+
this.data.lock_mut().insert_cloned(0, record_info);
165+
}))
166+
})
167+
}))
129168
})
130169
)
131170
})

src/utils/icons.rs

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,12 @@ fn svg_icon_attrs(icon: DomBuilder<SvgElement>) -> DomBuilder<SvgElement> {
77
.attr("stroke-width", "2")
88
.attr("stroke-linecap", "round")
99
.attr("stroke-linejoin", "round")
10+
.attr("cursor", "pointer")
1011
}
1112

1213
pub fn render_svg_trash_icon(color: &str, size: &str) -> Dom {
1314
svg!("svg", {
14-
.attr("alt", "trash icon")
15+
.attr("alt", "Borrar registro")
1516
.attr("width", size)
1617
.attr("height", size)
1718
.attr("stroke", color)
@@ -39,6 +40,31 @@ pub fn render_svg_trash_icon(color: &str, size: &str) -> Dom {
3940
})
4041
}
4142

43+
pub fn render_svg_plus_icon(color: &str, size: &str) -> Dom {
44+
svg!("svg", {
45+
.attr("alt", "Añadir registro")
46+
.attr("width", size)
47+
.attr("height", size)
48+
.attr("stroke", color)
49+
.apply(svg_icon_attrs)
50+
.children(&mut[
51+
svg!("line", {
52+
.attr("x1", "12")
53+
.attr("y1", "5")
54+
.attr("x2", "12")
55+
.attr("y2", "19")
56+
}),
57+
svg!("line", {
58+
.attr("x1", "5")
59+
.attr("y1", "12")
60+
.attr("x2", "19")
61+
.attr("y2", "12")
62+
}),
63+
64+
])
65+
})
66+
}
67+
4268
pub fn render_svg_save_icon(color: &str, size: &str) -> Dom {
4369
svg!("svg", {
4470
.attr("alt", "save icon")

src/utils/mod.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,14 @@ fn read_degiro_csv(content: Vec<u8>) -> Result<(BalanceNotes, AccountNotes)> {
9090
}
9191
}
9292

93+
pub(crate) fn date_to_usize(year: i32, month: u32, day: u32) -> usize {
94+
let date = NaiveDate::from_ymd_opt(year, month, day)
95+
.unwrap()
96+
.format("%Y%m%d")
97+
.to_string();
98+
date.parse::<usize>().unwrap_or(0)
99+
}
100+
93101
fn transform_to_aeat720_records(notes: (BalanceNotes, AccountNotes)) -> Result<Aeat720Records> {
94102
let mut result = vec![];
95103

0 commit comments

Comments
 (0)