Skip to content

Commit a6aecca

Browse files
committed
added function for adding task
1 parent 149eda5 commit a6aecca

File tree

8 files changed

+157
-41
lines changed

8 files changed

+157
-41
lines changed

.prettierrc.toml

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
# .prettierrc.toml
2+
3+
useTabs = true
4+
tabWidth = 4
5+
printWidth = 100
6+
endOfLine = "lf"
7+
8+
# -- Not supported yet --
9+
# trailingComma = "es5"
10+
# embeddedLanguageFormatting = "auto"
11+
12+
# Example override
13+
overrides = [
14+
{ files = ["src/*.rs"], options = { printWidth = 80 } }
15+
]

Cargo.toml

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ crate-type = ["cdylib"]
1111
console_error_panic_hook = []
1212

1313
[dependencies]
14+
once_cell = "1.19.0"
15+
regex = "1.10.6"
1416
wasm-bindgen = "0.2.92"
1517

1618
[dependencies.web-sys]
@@ -21,4 +23,7 @@ features = [
2123
'HtmlElement',
2224
'Node',
2325
'Window',
24-
]
26+
"HtmlInputElement",
27+
"HtmlTextAreaElement",
28+
"Event"
29+
]

src/lib.rs

Lines changed: 104 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1,55 +1,121 @@
11
mod utils;
2+
mod task_inner;
23

4+
use once_cell::sync::Lazy;
5+
use regex::Regex;
36
use wasm_bindgen::prelude::*;
4-
use web_sys::{js_sys::JsString, Document, Element, HtmlElement};
7+
use web_sys::{ Document, Element, HtmlElement, HtmlInputElement };
58

69
#[wasm_bindgen]
710
extern "C" {
8-
fn alert(s: &str);
9-
#[wasm_bindgen(js_namespace = console)]
10-
fn log(s: &str);
11+
fn alert(s: &str);
12+
#[wasm_bindgen(js_namespace = console)]
13+
fn log(s: &str);
1114
}
1215

1316
macro_rules! console_log {
14-
($($t:tt)*) => (log(&format_args!($($t)*).to_string()));
17+
($($t:tt)*) => (log(&format_args!($($t)*).to_string()));
1518
}
1619

1720
fn dcmnt() -> Document {
18-
let window = web_sys::window().expect("no global `window` exists");
19-
let document = window.document().expect("should have a document on window");
20-
return document;
21+
let window = web_sys::window().expect("no global `window` exists");
22+
let document = window.document().expect("should have a document on window");
23+
return document;
24+
}
25+
26+
fn value_test(str: String) -> bool {
27+
static RE: Lazy<Regex> = Lazy::new(|| Regex::new(r"[^a-zA-Z\s]").unwrap());
28+
RE.is_match(&str)
29+
}
30+
31+
// create a new task in todo (onclick button)
32+
#[wasm_bindgen]
33+
pub fn create_task(task_name: String) -> Result<(), JsValue> {
34+
let render: Document = dcmnt();
35+
let body: HtmlElement = render.body().expect("document should have a body");
36+
let tasks_list: web_sys::Element = body
37+
.query_selector(".tasks-list")
38+
.unwrap()
39+
.expect("");
40+
41+
let task = render.create_element("label")?;
42+
task.set_class_name("tasks-list__item");
43+
task.set_inner_html(&task_inner::get_inner(task_name));
44+
tasks_list.append_child(&task)?;
45+
Ok(())
46+
}
47+
48+
#[wasm_bindgen]
49+
pub fn not_valide_input() -> Result<(), JsValue> {
50+
let render: Document = dcmnt();
51+
let body: HtmlElement = render.body().expect("document should have a body");
52+
let not_valide_message: web_sys::Element = body
53+
.query_selector(".todo")
54+
.unwrap()
55+
.expect("");
56+
57+
match body.query_selector(".not-valid") {
58+
Ok(Some(_)) => {}
59+
Ok(None) => {
60+
console_log!("not exists");
61+
let val: Element = render.create_element("p")?;
62+
val.set_text_content(Some("Not valide input!"));
63+
val.set_class_name("not-valid");
64+
not_valide_message.append_child(&val)?;
65+
}
66+
Err(e) => {
67+
console_log!("{:?}", e);
68+
}
69+
}
70+
Ok(())
2171
}
2272

2373
#[wasm_bindgen]
2474
pub fn greet() -> Result<(), JsValue> {
25-
let render: Document = dcmnt();
26-
let body: HtmlElement = render.body().expect("document should have a body");
27-
let container: Element = body.query_selector(".container").unwrap().expect("");
28-
29-
// event-listener
30-
let closure: Closure<dyn FnMut(HtmlElement)> =
31-
Closure::<dyn FnMut(_)>::new(move |event: web_sys::HtmlElement| {
32-
let input = dcmnt()
33-
.get_element_by_id("input-task")
34-
.unwrap()
35-
.dyn_into::<HtmlElement>();
36-
console_log!("{:?}", input);
37-
});
38-
39-
let button = render
40-
.get_element_by_id("add-task")
41-
.unwrap()
42-
.add_event_listener_with_callback("click", closure.as_ref().unchecked_ref());
43-
44-
closure.forget();
45-
46-
// testing
47-
let i: u32 = 1;
48-
for _n in i..5 {
49-
let val: Element = render.create_element("p")?;
50-
val.set_text_content(Some("abc"));
51-
container.append_child(&val)?;
52-
}
53-
54-
Ok(())
75+
let render: Document = dcmnt();
76+
77+
// let body: HtmlElement = render.body().expect("document should have a body");
78+
// let container: Element = body
79+
// .query_selector(".container")
80+
// .unwrap()
81+
// .expect("");
82+
83+
// event-listener
84+
let closure: Closure<dyn FnMut(HtmlElement)> = Closure::<dyn FnMut(_)>::new(
85+
move |_: web_sys::HtmlElement| {
86+
let input_value: String = dcmnt()
87+
.get_element_by_id("input-task")
88+
.unwrap()
89+
.dyn_into::<HtmlInputElement>()
90+
.unwrap()
91+
.value();
92+
93+
if !value_test(input_value.clone()) {
94+
console_log!("{}", input_value);
95+
let _ = create_task(input_value);
96+
} else {
97+
let _ = not_valide_input();
98+
}
99+
}
100+
);
101+
102+
let _button: Result<(), JsValue> = render
103+
.get_element_by_id("add-task")
104+
.unwrap()
105+
.add_event_listener_with_callback(
106+
"click",
107+
closure.as_ref().unchecked_ref()
108+
);
109+
110+
closure.forget();
111+
112+
// testing
113+
// let i: u32 = 1;
114+
// for _n in i..5 {
115+
// let val: Element = render.create_element("p")?;
116+
// val.set_text_content(Some("abc"));
117+
// container.append_child(&val)?;
118+
// }
119+
120+
Ok(())
55121
}

src/task_inner.rs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
pub fn get_inner(text: String) -> String {
2+
let inner_task_text =
3+
format!(r#"
4+
5+
<article class="flex align-center justify-center">
6+
<p>{}</p>
7+
</article>
8+
<button>
9+
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" width="24" height="24">
10+
<path d="M18 6L6 18M6 6l12 12" stroke='#B0B0B0' stroke-width='2' stroke-linecap="round" />
11+
</svg>
12+
</button>
13+
"#, text);
14+
15+
inner_task_text
16+
}

src/utils.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,4 @@ pub fn set_panic_hook() {
77
// https://github.com/rustwasm/console_error_panic_hook#readme
88
#[cfg(feature = "console_error_panic_hook")]
99
console_error_panic_hook::set_once();
10-
}
10+
}

style/main.css

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,4 +130,11 @@
130130
}
131131
#rust-logo:hover {
132132
left: 25px;
133+
}
134+
135+
.not-valid {
136+
margin-top: 12px;
137+
font-size: 16px;
138+
font-weight: 300;
139+
color: #1D2939;
133140
}/*# sourceMappingURL=main.css.map */

style/main.css.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

style/main.scss

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -156,4 +156,11 @@
156156
&:hover {
157157
left: 25px;
158158
}
159+
}
160+
161+
.not-valid {
162+
margin-top: 12px;
163+
font-size: 16px;
164+
font-weight: 300;
165+
color: #1D2939;
159166
}

0 commit comments

Comments
 (0)