From e92c9695be7ed4c7f9871c8fef9618b2052cb02f Mon Sep 17 00:00:00 2001 From: Magnus Hovland Hoff Date: Mon, 24 Sep 2018 22:55:10 +0200 Subject: [PATCH] Fix editing of new articles with noscript. This also generalizes the code. Neat! --- src/resources/new_article_resource.rs | 12 ++++-------- src/wiki_lookup.rs | 4 ++-- 2 files changed, 6 insertions(+), 10 deletions(-) diff --git a/src/resources/new_article_resource.rs b/src/resources/new_article_resource.rs index a348a99..c5b9935 100644 --- a/src/resources/new_article_resource.rs +++ b/src/resources/new_article_resource.rs @@ -28,6 +28,7 @@ fn title_from_slug(slug: &str) -> String { pub struct NewArticleResource { state: State, slug: Option, + edit: bool, } #[derive(Deserialize)] @@ -39,8 +40,8 @@ struct CreateArticle { } impl NewArticleResource { - pub fn new(state: State, slug: Option) -> Self { - Self { state, slug } + pub fn new(state: State, slug: Option, edit: bool) -> Self { + Self { state, slug, edit } } } @@ -98,12 +99,7 @@ impl Resource for NewArticleResource { body: &Template { revision: NEW, last_updated: None, - - // Implicitly start in edit-mode when no slug is given. This - // currently directly corresponds to the /_new endpoint - // TODO: Also start in edit mode when the ?edit query arg is given - edit: self.slug.is_none(), - + edit: self.edit, cancel_url: self.slug.as_ref().map(|x| &**x), title: &title, raw: "", diff --git a/src/wiki_lookup.rs b/src/wiki_lookup.rs index 26bb9f6..2ac5ecc 100644 --- a/src/wiki_lookup.rs +++ b/src/wiki_lookup.rs @@ -198,7 +198,7 @@ impl WikiLookup { ("_diff", Some(tail)) => self.diff_lookup_f(tail, query), ("_new", None) => - Box::new(finished(Some(Box::new(NewArticleResource::new(self.state.clone(), None)) as BoxResource))), + Box::new(finished(Some(Box::new(NewArticleResource::new(self.state.clone(), None, true)) as BoxResource))), ("_revisions", Some(tail)) => self.revisions_lookup(tail, query), ("_search", None) => @@ -236,7 +236,7 @@ impl WikiLookup { Box::new(self.state.lookup_slug(slug.clone()) .and_then(move |x| Ok(Some(match x { SlugLookup::Miss => - Box::new(NewArticleResource::new(state, Some(slug))) as BoxResource, + Box::new(NewArticleResource::new(state, Some(slug), edit)) as BoxResource, SlugLookup::Hit { article_id, revision } => Box::new(ArticleResource::new(state, article_id, revision, edit)) as BoxResource, SlugLookup::Redirect(slug) =>