From ec712cc4c0e12329f51d10d9bd626d1859a011b8 Mon Sep 17 00:00:00 2001 From: Vincent Ambo Date: Sun, 15 Apr 2018 21:13:20 +0200 Subject: [PATCH] refactor(templates/render): Add generic post editing template Adds a generic template that can be used for submitting, responding to and editing posts. --- src/db.rs | 4 +- src/handlers.rs | 14 +++--- src/render.rs | 49 +++++++++++++++--- templates/new-thread.html | 56 --------------------- templates/post.html | 103 ++++++++++++++++++++++++++++++++++++++ templates/thread.html | 2 +- 6 files changed, 156 insertions(+), 72 deletions(-) delete mode 100644 templates/new-thread.html create mode 100644 templates/post.html diff --git a/src/db.rs b/src/db.rs index 22438d2b8..ad1c148a7 100644 --- a/src/db.rs +++ b/src/db.rs @@ -80,7 +80,7 @@ impl Handler for DbExecutor { /// Message used to create a new thread pub struct CreateThread { pub new_thread: NewThread, - pub body: String, + pub post: String, } impl Message for CreateThread { @@ -105,7 +105,7 @@ impl Handler for DbExecutor { // ... then create the first post in the thread. let new_post = NewPost { thread_id: thread.id, - body: msg.body, + body: msg.post, author_name: msg.new_thread.author_name.clone(), author_email: msg.new_thread.author_email.clone(), }; diff --git a/src/handlers.rs b/src/handlers.rs index 1cd596aad..8bfd3c151 100644 --- a/src/handlers.rs +++ b/src/handlers.rs @@ -99,7 +99,7 @@ fn anonymous() -> Author { #[derive(Deserialize)] pub struct NewThreadForm { pub title: String, - pub body: String, + pub post: String, } const NEW_THREAD_LENGTH_ERR: &'static str = "Title and body can not be empty!"; @@ -112,16 +112,16 @@ pub fn submit_thread(state: State, // Trim whitespace out of inputs: let input = NewThreadForm { title: input.title.trim().into(), - body: input.body.trim().into(), + post: input.post.trim().into(), }; // Perform simple validation and abort here if it fails: - if input.title.is_empty() || input.body.is_empty() { + if input.title.is_empty() || input.post.is_empty() { return state.renderer .send(NewThreadPage { alerts: vec![NEW_THREAD_LENGTH_ERR], title: Some(input.title), - body: Some(input.body), + post: Some(input.post), }) .flatten() .map(|res| HttpResponse::Ok().content_type(HTML).body(res)) @@ -140,7 +140,7 @@ pub fn submit_thread(state: State, let msg = CreateThread { new_thread, - body: input.body, + post: input.post, }; state.db.send(msg) @@ -158,7 +158,7 @@ pub fn submit_thread(state: State, #[derive(Deserialize)] pub struct NewPostForm { pub thread_id: i32, - pub body: String, + pub post: String, } /// This handler receives a "Reply"-form and redirects the user to the @@ -172,7 +172,7 @@ pub fn reply_thread(state: State, let new_post = NewPost { thread_id: input.thread_id, - body: input.body.trim().into(), + body: input.post.trim().into(), author_name: author.name, author_email: author.email, }; diff --git a/src/render.rs b/src/render.rs index 9cc9efd4d..d95fb6934 100644 --- a/src/render.rs +++ b/src/render.rs @@ -159,6 +159,41 @@ impl Handler for Renderer { } } +/// The different types of editing modes supported by the editing +/// template: +#[derive(Debug, Serialize)] +pub enum EditingMode { + NewThread, + PostReply, + EditPost, +} + +impl Default for EditingMode { + fn default() -> EditingMode { EditingMode::NewThread } +} + +/// This struct represents the context submitted to the template used +/// for rendering the new thread, edit post and reply to thread forms. +#[derive(Default, Serialize)] +pub struct FormContext { + /// Which editing mode is to be used by the template? + pub mode: EditingMode, + + /// Potential alerts to display to the user (e.g. input validation + /// results) + pub alerts: Vec<&'static str>, + + /// Either the title to be used in the subject field or the title + /// of the thread the user is responding to. + pub title: Option, + + /// Body of the post being edited, if present. + pub post: Option, + + /// ID of the thread being replied to or the post being edited. + pub id: Option, +} + /// Message used to render new thread page. /// /// It can optionally contain a vector of warnings to display to the @@ -167,7 +202,7 @@ impl Handler for Renderer { pub struct NewThreadPage { pub alerts: Vec<&'static str>, pub title: Option, - pub body: Option, + pub post: Option, } impl Message for NewThreadPage { @@ -178,11 +213,13 @@ impl Handler for Renderer { type Result = Result; fn handle(&mut self, msg: NewThreadPage, _: &mut Self::Context) -> Self::Result { - let mut ctx = Context::new(); - ctx.add("alerts", &msg.alerts); - ctx.add("title", &msg.title.map(|s| escape_html(&s))); - ctx.add("body", &msg.body.map(|s| escape_html(&s))); - Ok(self.tera.render("new-thread.html", &ctx)?) + let ctx: FormContext = FormContext { + alerts: msg.alerts, + title: msg.title, + post: msg.post, + ..Default::default() + }; + Ok(self.tera.render("post.html", &ctx)?) } } diff --git a/templates/new-thread.html b/templates/new-thread.html deleted file mode 100644 index 855626b8e..000000000 --- a/templates/new-thread.html +++ /dev/null @@ -1,56 +0,0 @@ - - - - - - - - - Converse Index - - -
- -
-
-
- {% for alert in alerts %} -
{{ alert }}
- {% endfor %} -

Make your own thread on these here forums!

-

Remember that you can use Markdown when - writing your posts.

-
-
-
-
- Title: -
- -
-
-
-
-
- Body: -
- -
-
-
-
- -
-
-
-
-
- - diff --git a/templates/post.html b/templates/post.html new file mode 100644 index 000000000..74cf03abf --- /dev/null +++ b/templates/post.html @@ -0,0 +1,103 @@ + + + + + + + + + + + Converse Index + + +
+ +
+
+
+ {%- for alert in alerts %} +
{{ alert }}
+ {% endfor -%} + + {%- if mode == "NewThread" %} +
Create a new thread
+ {% elif mode == "PostReply" %} +
Respond to thread '{{ title }}'
+ {% elif mode == "EditPost" %} +
Edit your post
+ {% endif -%} +
+
+ {% if mode == "NewThread" %} +
+ {% elif mode == "PostReply" %} + + {% elif mode == "EditPost" %} + + {% endif %} + {% if mode == "PostReply" %} + + {% elif mode == "EditPost" %} + + {% endif %} + + {% if mode == "NewThread" %} +
+
+ Title: +
+ +
+ {% endif %} + +
+
+
+ Post: +
+ +
+
+

+ Remember that you can use Markdown when + writing your posts: +

+

*italic text*

+

**bold text**

+

~strikethrough text~

+

[link text](https://some.link.com/)

+

![image text](https://foo.com/thing.jpg)

+

Use * or - to enumerate lists.

+

See Markdown documentation for more information!

+
+
+ +
+
+
+ + diff --git a/templates/thread.html b/templates/thread.html index f11b96b4d..6a89135cd 100644 --- a/templates/thread.html +++ b/templates/thread.html @@ -55,7 +55,7 @@
- +