Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: basic TODO app; no bells, no whistles #1167

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Makefile.toml
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ dependencies = [
{ name = "check", path = "examples/ssr_modes_axum" },
{ name = "check", path = "examples/tailwind" },
{ name = "check", path = "examples/tailwind_csr_trunk" },
{ name = "check", path = "examples/todo_app_basic" },
{ name = "check", path = "examples/todo_app_sqlite" },
{ name = "check", path = "examples/todo_app_sqlite_axum" },
{ name = "check", path = "examples/todo_app_sqlite_viz" },
Expand Down
9 changes: 9 additions & 0 deletions examples/todo_app_basic/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
[package]
name = "todo_app_basic"
version = "0.1.0"
edition = "2021"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
leptos = { path = "../../leptos" }
9 changes: 9 additions & 0 deletions examples/todo_app_basic/Makefile.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
[tasks.check-debug]
command = "cargo"
args = ["+nightly", "check-all-features"]
install_crate = "cargo-all-features"

[tasks.check-release]
command = "cargo"
args = ["+nightly", "check-all-features", "--release"]
install_crate = "cargo-all-features"
14 changes: 14 additions & 0 deletions examples/todo_app_basic/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# Basic TODO App

This is the client-side only TODO App you learned in Grade school.

## Running This Example

Just use trunk from this folder!

```bash
trunk serve --open
```

If you don't have `trunk` installed,
[click here for install instructions.](https://trunkrs.dev/)
11 changes: 11 additions & 0 deletions examples/todo_app_basic/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<html>
<head>
<style>
* {
box-sizing: border-box;
font-family: Arial;
}
</style>
</head>
<body></body>
</html>
3 changes: 3 additions & 0 deletions examples/todo_app_basic/rust-toolchain.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[toolchain]
channel = "nightly"
targets = ["wasm32-unknown-unknown"]
86 changes: 86 additions & 0 deletions examples/todo_app_basic/src/main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
use leptos::*;

fn main() {
mount_to_body(|cx| view! { cx, <App /> });
}

#[derive(Eq, Hash, PartialEq, Clone, Debug)]
struct TodoItem {
id: u32,
name: String,
}

#[derive(Copy, Clone)]
struct TodoGenerator {
Copy link
Contributor

@flosse flosse Sep 20, 2023

Choose a reason for hiding this comment

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

I'd split the ID, the Todo and the latest ID state.

e.g.:

#[derive(Debug, Default, Clone, Copy, PartialEq, Eq, Hash)]
pub struct TodoId(u64);

impl TodoId {
    const fn next(self) -> Self {
        Self(self.0 + 1)
    }
}

impl TodoItem {
    const fn new(id: TodoId, name: String) -> Self {
        Self { id, name }
    }
}

fn main(){
    let mut current_id = TodoId::default();
    // ...
    let create_new_item = |title| {
        let next_id = current_id.next();
        current_id = next_id;       
        let todo = TodoItem::new(next_id, title);
    };
}

next_id: u32,
}

impl TodoGenerator {
fn new() -> Self {
TodoGenerator { next_id: 1 }
}
fn get_todo(&mut self, name: String) -> TodoItem {
let next_id = self.next_id;
self.next_id += 1;
TodoItem { name, id: next_id }
}
}

#[component]
fn App(cx: Scope) -> impl IntoView {
let (draft_item_name, set_draft_item_name) =
create_signal(cx, String::new());
let (todos, set_todos) = create_signal::<Vec<TodoItem>>(cx, vec![]);
let mut todo_generator = TodoGenerator::new();

let delete_todo = move |id: u32| {
set_todos.update(move |todos| {
todos.retain(|t| t.id != id);
});
};

view! {
cx,
<form on:submit=move |e: ev::SubmitEvent| {
e.prevent_default();
set_todos.update(|todos| {
todos.push(todo_generator.get_todo(draft_item_name()));
set_draft_item_name(String::new())
});
}>
<label for="todo-name">"Todo Name: "</label>
<input
id="todo-name"
type="text"
prop:value=draft_item_name
on:input=move |e| {
let value = event_target_value(&e);
set_draft_item_name(value);
}
/>
</form>
<For
each=todos
key=move |todo| todo.id
view=move |cx, todo| view! { cx, <TodoItem todo=todo delete_todo=Box::new(delete_todo) /> }
/>
}
}

#[component]
fn TodoItem(
cx: Scope,
todo: TodoItem,
delete_todo: Box<dyn Fn(u32)>,
) -> impl IntoView {
// Note that we could have structured our todo list as a vector of signals.
// That would be a bit noisier, but would allow us to implement inline
// editing of TODO items inside this component, where we'd recieve a signal
// here instead of a TODO item, such that we could write to that signal
// to propagate changes throughout our app!
view! {
cx,
<p>{todo.name.clone()}</p>
<button on:click=move |_| delete_todo(todo.id)>"Done"</button>
}
}