From f279900ac9bdb3bcffb485f94126b15385dc1429 Mon Sep 17 00:00:00 2001 From: Rokas Puzonas Date: Tue, 28 Feb 2023 21:54:43 +0200 Subject: [PATCH] add buttons to switch between currently shown tables --- src/app.rs | 88 +++++++++++++++++++++++++++++++++++------------------- 1 file changed, 58 insertions(+), 30 deletions(-) diff --git a/src/app.rs b/src/app.rs index 91781b2..9d3668b 100644 --- a/src/app.rs +++ b/src/app.rs @@ -1,12 +1,11 @@ use std::io::Cursor; -use std::collections::HashMap; +use std::collections::{HashMap, self}; use std::rc::Rc; use gloo::console::console_dbg; -use base64::encode; use gloo::file::callbacks::FileReader; use gloo::file::File; use gloo::storage::{LocalStorage, Storage}; -use web_sys::{DragEvent, Event, FileList, HtmlInputElement}; +use web_sys::{DragEvent, Event, FileList, HtmlInputElement, MouseEvent}; use yew::html::TargetCast; use yew::{html, Callback, Component, Context, Html}; @@ -19,11 +18,15 @@ pub enum Msg { Noop, Loaded(String, Vec), UploadProject(File), + UpdateCurrentProject(Option), + ShowNextTable, + ShowPrevTable } pub struct App { active_readers: HashMap, - current_collection: Option>> + current_collection: Option>>, + currently_shown_table: usize } impl Component for App { @@ -38,7 +41,8 @@ impl Component for App { Self { active_readers: HashMap::default(), - current_collection + current_collection, + currently_shown_table: 0 } } @@ -50,7 +54,8 @@ impl Component for App { let mut collections = parse_project(cursor).expect("oops"); if collections.len() >= 1 { - self.set_current_collection(Some(collections.remove(0))); + let msg = Self::update_current_collection(Some(collections.remove(0))); + ctx.link().send_message(msg); } } @@ -75,7 +80,32 @@ impl Component for App { self.active_readers.insert(file_name, task); true }, - Msg::Noop => false + Msg::Noop => false, + Msg::UpdateCurrentProject(collection) => { + if let Some(collection) = collection { + LocalStorage::set(COLLECTION_STORE_KEY, &collection).unwrap(); + self.current_collection = Some(collection.tables.into_iter().map(Rc::new).collect()); + } else { + LocalStorage::delete(COLLECTION_STORE_KEY); + self.current_collection = None + } + + true + }, + Msg::ShowNextTable => { + if let Some(collection) = &self.current_collection { + self.currently_shown_table = (self.currently_shown_table + 1).min(collection.len()-1); + return true; + } + false + }, + Msg::ShowPrevTable => { + if self.currently_shown_table > 0 { + self.currently_shown_table = self.currently_shown_table - 1; + return true; + } + false + }, } } @@ -124,13 +154,26 @@ impl Component for App { if let Some(collection) = &self.current_collection {

{ "2. Make sure everything looks 👌" }

- // { Self::show_collection(collection) } - { Self::show_table(collection[0].clone()) } -
- -
{ 0 } { " / " } { collection.len() }
- +
+ +
{ self.currently_shown_table + 1 } { " / " } { collection.len() }
+
+ { Self::show_table(collection[self.currently_shown_table].clone()) }
@@ -143,15 +186,6 @@ impl Component for App { } impl App { - fn show_collection(collection: &SQLTableCollection) -> Html { - collection.tables.iter().map(|table| { - html! { -
- { &table.name } -
- } - }).collect() - } fn show_table(table: Rc) -> Html { html!{ @@ -174,13 +208,7 @@ impl App { } } - pub fn set_current_collection(&mut self, current_collection: Option) { - if let Some(collection) = current_collection { - LocalStorage::set(COLLECTION_STORE_KEY, &collection).unwrap(); - self.current_collection = Some(collection.tables.into_iter().map(Rc::new).collect()); - } else { - LocalStorage::delete(COLLECTION_STORE_KEY); - self.current_collection = None - } + pub fn update_current_collection(current_collection: Option) -> Msg { + Msg::UpdateCurrentProject(current_collection) } }