diff options
author | Joris | 2022-01-09 14:50:45 +0100 |
---|---|---|
committer | Joris | 2022-01-09 14:50:45 +0100 |
commit | 6c47403b11e7aaf1a22778bdc7615051779cb7bd (patch) | |
tree | 66b643ed4032b65e16dffce9182a6a1e46c56e3a | |
parent | d0a9063631e71928484a698f6c585ebb3915e8a2 (diff) |
Allow to delete events
-rw-r--r-- | README.md | 6 | ||||
-rw-r--r-- | src/app/form.rs | 23 | ||||
-rw-r--r-- | src/app/update.rs | 10 | ||||
-rw-r--r-- | src/db/mod.rs | 9 |
4 files changed, 41 insertions, 7 deletions
@@ -12,11 +12,6 @@ nix develop --command cargo run # TODO -## CRUD - -1. Modify an event with double click. -2. Delete an event with right click. - ## Complexify event Be able to specify repetition. @@ -52,3 +47,4 @@ Be able to specify repetition. - Apply a style on times in the calendar (bold ?). - Print errors on forms when validating. - Validate the form when pressing enter on any field +- Remove event with right click diff --git a/src/app/form.rs b/src/app/form.rs index 6c42cd0..7f75db0 100644 --- a/src/app/form.rs +++ b/src/app/form.rs @@ -46,11 +46,14 @@ pub async fn show(app: &App, event: Event, is_new: bool) { vbox.append(&label("Fin")); vbox.append(&end); - let button = gtk::Button::with_label("Créer"); + let button = gtk::Button::builder() + .label(if is_new { "Créer" } else { "Modifier" }) + .margin_bottom(10) + .build(); vbox.append(&button); let conn = app.conn.clone(); let tx = app.tx.clone(); - button.connect_clicked(glib::clone!(@weak dialog => move |_| { + button.connect_clicked(glib::clone!(@weak dialog, @strong event => move |_| { match event::validate(event.id, date.buffer().text(), name.buffer().text(), start.buffer().text(), end.buffer().text()) { Some(new) => { match if is_new { db::insert(&conn, &new) } else { db::update(&conn, &new) } { @@ -66,6 +69,22 @@ pub async fn show(app: &App, event: Event, is_new: bool) { } })); + if !is_new { + let button = gtk::Button::builder().label("Supprimer").build(); + vbox.append(&button); + let conn = app.conn.clone(); + let tx = app.tx.clone(); + button.connect_clicked(glib::clone!(@weak dialog => move |_| { + match db::delete(&conn, &event.id) { + Ok(_) => { + update::send(tx.clone(), Msg::DeleteEvent { event: event.clone() }); + dialog.close() + }, + Err(_) => () + } + })); + } + dialog.run_future().await; } diff --git a/src/app/update.rs b/src/app/update.rs index f1576b5..baf4651 100644 --- a/src/app/update.rs +++ b/src/app/update.rs @@ -17,6 +17,7 @@ pub enum Msg { ShowUpdateForm { event: Event }, AddEvent { new: Event }, UpdateEvent { old: Event, new: Event }, + DeleteEvent { event: Event }, } pub async fn event_handler(rx: Receiver<Msg>, mut app: App) { @@ -47,6 +48,15 @@ pub async fn event_handler(rx: Receiver<Msg>, mut app: App) { None => println!("Event not found when updating from {:?} to {:?}", old, new), } } + Msg::DeleteEvent { event } => { + match app.events.iter().position(|e| e.id == event.id) { + Some(index) => { + app.events.remove(index); + calendar::refresh_date(&app, event.date); + } + None => println!("Event not found when trying to delete {:?}", event), + } + } } } } diff --git a/src/db/mod.rs b/src/db/mod.rs index 03692e9..0dd4ddf 100644 --- a/src/db/mod.rs +++ b/src/db/mod.rs @@ -30,6 +30,15 @@ pub fn update(conn: &Connection, event: &Event) -> Result<()> { Ok(()) } +pub fn delete(conn: &Connection, id: &Uuid) -> Result<()> { + conn.execute( + "DELETE FROM events WHERE id = ?", + params![id.to_hyphenated().to_string()], + )?; + + Ok(()) +} + // TODO: Don’t use unwrap pub fn list(conn: &Connection) -> Result<Vec<Event>> { let mut stmt = conn.prepare("SELECT id, date, start, end, name FROM events")?; |