From 088bf5dd9e5e07dbfcfb33b2c474a740c241e812 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dawid=20Ci=C4=99=C5=BCarkiewicz?= Date: Mon, 11 May 2020 21:00:44 -0700 Subject: [PATCH] Refactor menu buttons --- resources/style.css | 4 ++ src/main.rs | 20 ++++-- src/render.rs | 149 ++++++++++++++++++++++++++------------------ 3 files changed, 108 insertions(+), 65 deletions(-) diff --git a/resources/style.css b/resources/style.css index f7cdab6..d83cc76 100644 --- a/resources/style.css +++ b/resources/style.css @@ -3,6 +3,10 @@ body { margin: 0 auto; } +a.pure-button { + padding: .5em 1em; +} + .button-warning { color: white; background: rgb(223, 117, 20); diff --git a/src/main.rs b/src/main.rs index 4710ca8..69941cb 100644 --- a/src/main.rs +++ b/src/main.rs @@ -231,17 +231,25 @@ async fn handle_get( )))); } let tags = path_to_tags(&path); + let page_state = render::PageState { + page: None, + edit: query.edit.is_some(), + path: path.as_str().to_string(), + }; + let read = state.page_store.read().await; if let Some(q_id) = query.id { let page = read.get(q_id).await?; return Ok(warp_reply_from_render(render::html_page(render::page( - Some(&page), - query.edit.is_some(), + render::PageState { + page: Some(page), + ..page_state + }, )))); } else if query.edit.is_some() { return Ok(warp_reply_from_render(render::html_page(render::page( - None, true, + page_state, )))); } let results = read.find(tags.as_slice()); @@ -253,8 +261,10 @@ async fn handle_get( if results.matching_pages.len() == 1 { let page = read.get(results.matching_pages[0].id.clone()).await?; Ok(warp_reply_from_render(render::html_page(render::page( - Some(&page), - query.edit.is_some(), + render::PageState { + page: Some(page), + ..page_state + }, )))) } else { let compact_results = read.compact_results(results); diff --git a/src/render.rs b/src/render.rs index aa31503..f067559 100644 --- a/src/render.rs +++ b/src/render.rs @@ -3,7 +3,15 @@ use horrorshow::prelude::*; use horrorshow::{box_html, owned_html}; use crate::index; -use crate::page::{self, Tag}; +use crate::page::{Parsed, Tag}; + +#[derive(Clone, Debug)] +pub struct PageState { + // currently rendered path + pub path: String, + pub edit: bool, + pub page: Option, +} pub fn html_page(body: impl RenderOnce) -> impl RenderOnce { owned_html! { @@ -20,78 +28,99 @@ pub fn html_page(body: impl RenderOnce) -> impl RenderOnce { } } -pub fn page(page: Option<&page::Parsed>, edit: bool) -> Box { - if edit { - Box::new(page_editing_view(page)) as Box +pub fn page(page_state: PageState) -> Box { + if page_state.edit { + Box::new(page_editing_view(page_state)) as Box } else { - Box::new(page_view(page.expect("always some"))) as Box + Box::new(page_view(page_state)) as Box } } -pub fn page_editing_view(page: Option<&page::Parsed>) -> impl RenderOnce { - if let Some(page) = page.as_ref() { +pub fn page_editing_view(page_state: PageState) -> impl RenderOnce { + if let Some(page) = page_state.page.as_ref() { let body = page.source_body.clone(); - let id = page.id().to_owned(); - (box_html! { - form(action=".", method="post", class="pure-form") { - a(href=format!("?id={}", id),class="pure-button"){ : "Cancel" } - : " "; - input(type="submit", value="Save", class="pure-button pure-button-primary"); - input(type="hidden", name="id", value=id); - textarea(name="body") { - : body + menu( + page_state.clone(), + Some( + (box_html! { + textarea(name="body") { + : body + } + }) as Box, + ), + ) + } else { + menu( + page_state.clone(), + Some( + (box_html! { + textarea(name="body"); + }) as Box, + ), + ) + } +} + +pub fn menu(page_state: PageState, subform: Option>) -> impl RenderOnce { + let id = page_state.page.map(|p| p.id().to_owned()); + let edit = page_state.edit; + + owned_html! { + form { + div(class="pure-menu pure-menu-horizontal") { + @ if let Some(id) = id.as_deref() { + input(type="hidden", name="id", value=id); + } + + @ if edit { + @ if let Some(id) = id.as_deref() { + a(href=format!("?id={}", id), class="pure-button"){ : "Cancel" } + : " "; + } else { + a(href="javascript:history.back()", class="pure-button"){ : "Cancel" } + : " "; + } + } else { + a(href="..",class="pure-button") { : "Up" } + : " "; + } + @ if edit { + @ if let Some(_id) = id.as_deref() { + button(type="submit", class="pure-button pure-button-primary", formaction=".", formmethod="post"){ + : "Save" + } + } else { + button(type="submit", class="pure-button pure-button-primary", formaction=".", formmethod="post", name="_method", value="put"){ + : "Save" + } + } + : " "; + } else { + a(href="?edit=true", class="pure-button button-green"){ : "New" } + : " "; + } + @ if !edit { + input(type="hidden", name="edit", value="true"); + button(type="submit", class="pure-button pure-button-primary", formaction=".", formmethod="get"){ + : "Edit" + } + : " "; + button(type="submit", class="pure-button button-warning", formaction=".", formmethod="post", name="_method", value="delete", onclick="return confirm('Are you sure?');"){ + : "Delete" + } } } - }) as Box - } else { - box_html! { - form(action=".", method="post", class="pure-form") { - a(href="javascript:history.back()",class="pure-button"){ : "Cancel" } - : " "; - input(type="submit", value="Save", class="pure-button pure-button-primary"); - input(type="hidden", name="_method", value="put"); - textarea(name="body"); - } + : subform } } } -pub fn page_view(page: &page::Parsed) -> impl RenderOnce { +pub fn page_view(page_state: PageState) -> impl RenderOnce { + let menu = menu(page_state.clone(), None); + let page = page_state.page.expect("always some"); let page_html = page.html.clone(); - let id = page.id().to_owned(); - let id_copy = id.clone(); owned_html! { - div(class="pure-menu pure-menu-horizontal") { - form(action="..", method="get", class="pure-menu-item pure-form") { - button(type="submit", class="pure-button"){ - : "Up" - } - } - : " "; - form(action="/", method="get", class="pure-menu-item pure-form") { - input(type="hidden", name="edit", value="true"); - button(type="submit", class="pure-button button-green"){ - : "New" - } - } - : " "; - form(action=".", method="get", class="pure-menu-item pure-form") { - input(type="hidden", name="edit", value="true"); - input(type="hidden", name="id", value=id); - button(type="submit", class="pure-button pure-button-primary"){ - : "Edit" - } - } - : " "; - form(action=".", method="post", class="pure-menu-item pure-form") { - input(type="hidden", name="edit", value="true"); - input(type="hidden", name="id", value=id_copy); - input(type="hidden", name="_method", value="delete"); - button(type="submit", class="pure-button button-warning",onclick="return confirm('Are you sure?');"){ - : "Delete" - } - } - } + : menu; : Raw(page_html) } }