From fbf10e2eea0b7ffbf0dfa95dc430a8f065a727ed Mon Sep 17 00:00:00 2001 From: asonix Date: Fri, 11 Dec 2020 21:36:07 -0600 Subject: [PATCH] Fix index mobile styles, add delete confirmations --- Cargo.lock | 2 +- Cargo.toml | 2 +- scss/layout.scss | 15 ++++- src/lib.rs | 80 +++++++++++++++++++++----- templates/confirm_delete.rs.html | 27 +++++++++ templates/confirm_entry_delete.rs.html | 33 +++++++++++ templates/edit_collection.rs.html | 29 +++++----- templates/image.rs.html | 10 ++++ templates/index.rs.html | 4 +- templates/return_home.rs.html | 9 +++ templates/view_collection.rs.html | 9 +-- 11 files changed, 178 insertions(+), 42 deletions(-) create mode 100644 templates/confirm_delete.rs.html create mode 100644 templates/confirm_entry_delete.rs.html create mode 100644 templates/image.rs.html create mode 100644 templates/return_home.rs.html diff --git a/Cargo.lock b/Cargo.lock index 577792a..638e014 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1334,7 +1334,7 @@ checksum = "d4fd5641d01c8f18a23da7b6fe29298ff4b55afcccdf78973b24cf3175fee32e" [[package]] name = "pict-rs-aggregator" -version = "0.1.2" +version = "0.1.3" dependencies = [ "actix-web", "anyhow", diff --git a/Cargo.toml b/Cargo.toml index b46c6b9..1c47b0b 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "pict-rs-aggregator" -version = "0.1.2" +version = "0.1.3" authors = ["asonix "] edition = "2018" build = "src/build.rs" diff --git a/scss/layout.scss b/scss/layout.scss index d191829..0756831 100644 --- a/scss/layout.scss +++ b/scss/layout.scss @@ -51,6 +51,11 @@ article .content-group { margin: 0; } +.delete-confirmation { + margin: 0; + padding: 16px 0 0; +} + h3 { margin: 0; } @@ -135,8 +140,11 @@ a { } } -.button-space { +.button-group { margin: 0 -8px; +} + +.button-space { padding-top: 16px; } @@ -237,7 +245,7 @@ ul { width: 100%; } - .button-space { + .button-group { display: flex; flex-wrap: wrap; justify-content: space-between; @@ -248,6 +256,9 @@ ul { flex: 1; min-width: 150px; text-align: center; + display: flex; + justify-content: space-around; + align-items: center; } .title { diff --git a/src/lib.rs b/src/lib.rs index 26337dc..271e53f 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -89,8 +89,15 @@ impl State { self.scoped(&format!("{}?token={}", id, token.token)) } - fn delete_collection_path(&self, id: Uuid, token: &ValidToken) -> String { - self.scoped(&format!("{}/delete?token={}", id, token.token)) + fn delete_collection_path(&self, id: Uuid, token: &ValidToken, confirmed: bool) -> String { + if confirmed { + self.scoped(&format!( + "{}/delete?token={}&confirmed=true", + id, token.token + )) + } else { + self.scoped(&format!("{}/delete?token={}", id, token.token)) + } } fn public_collection_path(&self, id: Uuid) -> String { @@ -108,11 +115,24 @@ impl State { )) } - fn delete_entry_path(&self, collection_id: Uuid, id: Uuid, token: &ValidToken) -> String { - self.scoped(&format!( - "{}/entry/{}/delete?token={}", - collection_id, id, token.token - )) + fn delete_entry_path( + &self, + collection_id: Uuid, + id: Uuid, + token: &ValidToken, + confirmed: bool, + ) -> String { + if confirmed { + self.scoped(&format!( + "{}/entry/{}/delete?token={}&confirmed=true", + collection_id, id, token.token + )) + } else { + self.scoped(&format!( + "{}/entry/{}/delete?token={}", + collection_id, id, token.token + )) + } } fn statics_path(&self, file: &str) -> String { @@ -448,13 +468,7 @@ async fn view_collection( let mut cursor = Cursor::new(vec![]); - self::templates::view_collection( - &mut cursor, - path.collection, - &collection, - &entries, - &state, - )?; + self::templates::view_collection(&mut cursor, path.collection, &collection, &entries, &state)?; Ok(HttpResponse::Ok() .content_type(mime::TEXT_HTML.essence_str()) @@ -544,12 +558,36 @@ async fn update_entry( Ok(to_edit_page(entry_path.collection, &token, &state)) } +#[derive(serde::Deserialize)] +struct ConfirmQuery { + confirmed: Option, +} + async fn delete_entry( entry_path: web::Path, + query: web::Query, token: ValidToken, conn: web::Data, state: web::Data, ) -> Result { + if !query.confirmed.unwrap_or(false) { + let entry = state.store.entry(&entry_path).await?; + + let mut cursor = Cursor::new(vec![]); + self::templates::confirm_entry_delete( + &mut cursor, + entry_path.collection, + entry_path.entry, + &entry, + &token, + &state, + )?; + + return Ok(HttpResponse::Ok() + .content_type(mime::TEXT_HTML.essence_str()) + .body(cursor.into_inner())); + } + let entry = state.store.entry(&entry_path).await?; conn.delete(&entry.filename, &entry.delete_token).await?; @@ -565,10 +603,22 @@ async fn delete_entry( async fn delete_collection( path: web::Path, - _token: ValidToken, + query: web::Query, + token: ValidToken, conn: web::Data, state: web::Data, ) -> Result { + if !query.confirmed.unwrap_or(false) { + let collection = state.store.collection(&path).await?; + + let mut cursor = Cursor::new(vec![]); + self::templates::confirm_delete(&mut cursor, path.collection, &collection, &token, &state)?; + + return Ok(HttpResponse::Ok() + .content_type(mime::TEXT_HTML.essence_str()) + .body(cursor.into_inner())); + } + let entries = state.store.entries(path.entry_range()).await?; let future_vec = entries diff --git a/templates/confirm_delete.rs.html b/templates/confirm_delete.rs.html new file mode 100644 index 0000000..5b70282 --- /dev/null +++ b/templates/confirm_delete.rs.html @@ -0,0 +1,27 @@ +@use crate::{ui::ButtonKind, Collection, State, ValidToken}; +@use super::{layout, button_link, return_home}; +@use uuid::Uuid; + +@(id: Uuid, collection: &Collection, token: &ValidToken, state: &State) + +@:layout(state, &format!("Delete: {}", collection.title), Some(&format!("Are you sure you want to delete {}", collection.title)), { + +}, { +
+
+
+

Delete @collection.title

+
+
+

Are you sure you want to delete @collection.title

+
+
+
+ @:button_link("Delete Collection", &state.delete_collection_path(id, token, true), ButtonKind::Submit) + @:button_link("Cancel", &state.edit_collection_path(id, token), ButtonKind::Outline) +
+
+
+
+@:return_home(state) +}) diff --git a/templates/confirm_entry_delete.rs.html b/templates/confirm_entry_delete.rs.html new file mode 100644 index 0000000..19000a6 --- /dev/null +++ b/templates/confirm_entry_delete.rs.html @@ -0,0 +1,33 @@ +@use crate::{ui::ButtonKind, Entry, State, ValidToken}; +@use super::{layout, button_link, image, return_home}; +@use uuid::Uuid; + +@(collection_id: Uuid, id: Uuid, entry: &Entry, token: &ValidToken, state: &State) + +@:layout(state, "Delete Image", Some("Are you sure you want to delete this image?"), { + +}, { +
+
+
+

Delete Image

+
+
+
+
+ @:image(entry, state) +
+
+

Are you sure you want to delete this image?

+
+ @:button_link("Delete Image", &state.delete_entry_path(collection_id, id, token, true), ButtonKind::Submit) + @:button_link("Cancel", &state.edit_collection_path(collection_id, token), ButtonKind::Outline) +
+
+
+
+
+
+@:return_home(state) +}) + diff --git a/templates/edit_collection.rs.html b/templates/edit_collection.rs.html index fcac71f..c40fe99 100644 --- a/templates/edit_collection.rs.html +++ b/templates/edit_collection.rs.html @@ -1,5 +1,5 @@ @use crate::{ui::ButtonKind, Collection, Entry, State, ValidToken}; -@use super::{button, button_link, image_preview, file_input, layout, text_area, text_input, statics::file_upload_js}; +@use super::{button, button_link, image, file_input, layout, return_home, text_area, text_input, statics::file_upload_js}; @use uuid::Uuid; @(collection: &Collection, collection_id: Uuid, entries: &[(Uuid, Entry)], token: &ValidToken, state: &State) @@ -34,11 +34,11 @@
- @:text_input("title", Some("Title"), Some(&collection.title)) - @:text_area("description", Some("Description"), Some(&collection.description)) -
+ @:text_input("title", Some("Collection Title"), Some(&collection.title)) + @:text_area("description", Some("Collection Description"), Some(&collection.description)) +
@:button("Update Collection", ButtonKind::Submit) - @:button_link("Delete Collection", &state.delete_collection_path(collection_id, token), ButtonKind::Outline) + @:button_link("Delete Collection", &state.delete_collection_path(collection_id, token, false), ButtonKind::Outline)
@@ -48,21 +48,17 @@
- @:image_preview(entry, state) -
-
@entry.title
-
@entry.description
-
+ @:image(entry, state)
- @:text_input("title", Some("Title"), Some(&entry.title)) - @:text_area("description", Some("Description"), Some(&entry.description)) + @:text_input("title", Some("Image Title"), Some(&entry.title)) + @:text_area("description", Some("Image Description"), Some(&entry.description)) -
+
@:button("Update Image", ButtonKind::Submit) - @:button_link("Delete Image", &state.delete_entry_path(collection_id, *id, token), ButtonKind::Outline) + @:button_link("Delete Image", &state.delete_entry_path(collection_id, *id, token, false), ButtonKind::Outline)
@@ -83,14 +79,15 @@

Add Image

-
+
@:file_input("images[]", Some("Select Image"), Some(crate::accept()), false)
-
+
@:button("Upload", ButtonKind::Submit)
+@:return_home(state) }) diff --git a/templates/image.rs.html b/templates/image.rs.html new file mode 100644 index 0000000..b4115bc --- /dev/null +++ b/templates/image.rs.html @@ -0,0 +1,10 @@ +@use crate::{Entry, State}; +@use super::image_preview; + +@(entry: &Entry, state: &State) + +@:image_preview(entry, state) +
+
@entry.title
+
@entry.description
+
diff --git a/templates/index.rs.html b/templates/index.rs.html index a6b58a0..c3ebfc5 100644 --- a/templates/index.rs.html +++ b/templates/index.rs.html @@ -15,7 +15,9 @@ @:text_area("description", Some("Description"), None)
- @:button("Create Collection", ButtonKind::Submit) +
+ @:button("Create Collection", ButtonKind::Submit) +
diff --git a/templates/return_home.rs.html b/templates/return_home.rs.html new file mode 100644 index 0000000..563c6eb --- /dev/null +++ b/templates/return_home.rs.html @@ -0,0 +1,9 @@ +@use crate::State; + +@(state: &State) + +
+ +
diff --git a/templates/view_collection.rs.html b/templates/view_collection.rs.html index ff3e1a4..b6b5238 100644 --- a/templates/view_collection.rs.html +++ b/templates/view_collection.rs.html @@ -1,5 +1,5 @@ @use crate::{Collection, Entry, State}; -@use super::{layout, image_preview}; +@use super::{layout, image, return_home}; @use uuid::Uuid; @(id: Uuid, collection: &Collection, entries: &[(Uuid, Entry)], state: &State) @@ -23,15 +23,12 @@ @for (_, entry) in entries {
  • - @:image_preview(entry, state) -
    -
    @entry.title
    -
    @entry.description
    -
    + @:image(entry, state)
  • } +@:return_home(state) })