diff options
author | Joris | 2022-01-09 14:37:35 +0100 |
---|---|---|
committer | Joris | 2022-01-09 14:37:35 +0100 |
commit | d0a9063631e71928484a698f6c585ebb3915e8a2 (patch) | |
tree | 9a5fc430684aea73877711dd3392b1845d4b9904 /src/app/form.rs | |
parent | 5166efe517291f5c9fc6326f30651ef799d6db65 (diff) |
Allow to update events
Diffstat (limited to 'src/app/form.rs')
-rw-r--r-- | src/app/form.rs | 46 |
1 files changed, 24 insertions, 22 deletions
diff --git a/src/app/form.rs b/src/app/form.rs index 08a2af1..6c42cd0 100644 --- a/src/app/form.rs +++ b/src/app/form.rs @@ -1,25 +1,19 @@ use gtk4 as gtk; -use async_channel::Sender; -use chrono::NaiveDate; use gtk::glib; use gtk::prelude::*; -use rusqlite::Connection; -use std::rc::Rc; -use uuid::Uuid; -use crate::{app::update, app::update::Msg, db, model::event}; +use crate::{ + app::{update, update::Msg, App}, + db, + model::{event, event::Event}, +}; -pub async fn show( - conn: Rc<Connection>, - tx: Sender<Msg>, - window: Rc<gtk::ApplicationWindow>, - date: NaiveDate, -) { +pub async fn show(app: &App, event: Event, is_new: bool) { let dialog = gtk::Dialog::builder() - .transient_for(&*window) + .transient_for(&*app.window) .modal(true) - .title("Ajouter") + .title(if is_new { "Ajouter" } else { "Modifier" }) .css_classes(vec!["g-Form".to_string()]) .build(); @@ -31,30 +25,38 @@ pub async fn show( vbox.add_css_class("g-Form__Inputs"); content_area.append(&vbox); - let name = entry(""); + let name = entry(&event.name); vbox.append(&label("Événement")); vbox.append(&name); - let date = entry(&date.format(event::DATE_FORMAT).to_string()); + let date = entry(&event.date.format(event::DATE_FORMAT).to_string()); vbox.append(&label("Jour")); vbox.append(&date); - let start = entry(""); + let start = entry( + &event + .start + .map(event::pprint_time) + .unwrap_or("".to_string()), + ); vbox.append(&label("Début")); vbox.append(&start); - let end = entry(""); + let end = entry(&event.end.map(event::pprint_time).unwrap_or("".to_string())); vbox.append(&label("Fin")); vbox.append(&end); let button = gtk::Button::with_label("Créer"); vbox.append(&button); + let conn = app.conn.clone(); + let tx = app.tx.clone(); button.connect_clicked(glib::clone!(@weak dialog => move |_| { - match event::validate(Uuid::new_v4(), date.buffer().text(), name.buffer().text(), start.buffer().text(), end.buffer().text()) { - Some(event) => { - match db::insert(&conn, &event) { + 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) } { Ok(_) => { - update::send(tx.clone(), Msg::AddEvent { event: event }); + let msg = if is_new { Msg::AddEvent { new } } else { Msg::UpdateEvent { old: event.clone(), new } }; + update::send(tx.clone(), msg); dialog.close() }, Err(_) => () |