From b68000c149e8597c52ad794bbf3a63a28932d134 Mon Sep 17 00:00:00 2001 From: Pierre Jarriges <pierre.jarriges@tutanota.com> Date: Mon, 8 Nov 2021 12:29:25 +0100 Subject: [PATCH] admin panel create update --- admin-frontend/src/article.js | 25 +++++++++++++ .../src/components/create-article-form.js | 36 ++++++++++--------- .../src/components/update-article-form.js | 12 +++++-- src/model/article.rs | 2 ++ 4 files changed, 56 insertions(+), 19 deletions(-) create mode 100644 admin-frontend/src/article.js diff --git a/admin-frontend/src/article.js b/admin-frontend/src/article.js new file mode 100644 index 0000000..6ba6b37 --- /dev/null +++ b/admin-frontend/src/article.js @@ -0,0 +1,25 @@ +"use strict"; + +class Article { + constructor(data) { + if (data) { + this.from(data) + } else { + this.title = ""; + this.subtitle = ""; + this.category = ""; + this.details = []; + this.images = []; + this.body = ""; + this.locale = ""; + } + } + + from(data) { + Object.entries(data).forEach(k_v => { + const [key, value] = k_v; + this[key] = value; + }); + } +} +module.exports = Article; \ No newline at end of file diff --git a/admin-frontend/src/components/create-article-form.js b/admin-frontend/src/components/create-article-form.js index c5ed71d..e7651c0 100644 --- a/admin-frontend/src/components/create-article-form.js +++ b/admin-frontend/src/components/create-article-form.js @@ -1,5 +1,6 @@ "use strict"; +const Article = require("../article"); const { images_url } = require("../constants"); const { fetch_post_article, fetch_article, fetch_update_article } = require("../xhr"); @@ -7,27 +8,13 @@ class CreateArticleForm { constructor(params) { this.params = params || {}; this.state = { - output: this.params.data || { - title: "", - subtitle: "", - category: "", - details: [], - images: [], - body: "", - }, + output: new Article(this.params.data), article_sent: {}, } } reset() { - this.state.output = { - title: "", - subtitle: "", - category: "", - details: [], - images: [], - body: "", - }; + this.state.output = new Article(); this.state.article_sent = {}; this.refresh(); } @@ -274,6 +261,7 @@ class CreateArticleForm { fetch_article(id) .then(article => { this.state.article_sent = article; + this.params.on_article_sent && this.params.on_article_sent(); this.refresh(); }) .catch(er => console.log(er)); @@ -287,6 +275,22 @@ class CreateArticleForm { value: this.state.output.category, oninput: this.handle_text_input.bind(this, "category") }, + { + tag: "select", value: this.state.output.locale, + onchange: e => this.state.output.locale = e.target.value, + contents: [{ + tag: "option", + value: "", + contents: "-- LOCALE --" + }].concat(["fr", "en", "es"].map(loc => { + return { + tag: "option", + value: loc, + contents: loc, + selected: this.state.output.locale === loc + } + })) + }, { tag: "input", type: "text", placeholder: "Article title", diff --git a/admin-frontend/src/components/update-article-form.js b/admin-frontend/src/components/update-article-form.js index 0acb6cc..beff446 100644 --- a/admin-frontend/src/components/update-article-form.js +++ b/admin-frontend/src/components/update-article-form.js @@ -18,8 +18,6 @@ class UpdateArticleForm { search_result: {}, article_to_update: {}, }; - - this.refresh(); } handle_search_article() { @@ -43,6 +41,7 @@ class UpdateArticleForm { .then(res => { alert(res); this.reset(); + this.refresh(); }) .catch(err => alert(err)) } @@ -114,7 +113,13 @@ class UpdateArticleForm { tag: "div", id: "update-article-form-container", contents: this.state.article_to_update._id - ? [new CreateArticleForm({ data: this.state.article_to_update }).render()] + ? [new CreateArticleForm({ + data: this.state.article_to_update, + on_article_sent: () => { + this.reset(); + this.refresh_search_result(); + } + }).render()] : [] } } @@ -136,6 +141,7 @@ class UpdateArticleForm { contents: [ this.render_search(), this.render_search_result(), + { tag: "hr", style_rules: { width: "100%" } }, this.render_update_form(), ] } diff --git a/src/model/article.rs b/src/model/article.rs index ac65b2e..f968722 100644 --- a/src/model/article.rs +++ b/src/model/article.rs @@ -24,6 +24,7 @@ pub struct Article { pub details: Vec<ArticleDetail>, pub images: Vec<String>, pub category: String, + pub locale: String, } impl Article { @@ -47,6 +48,7 @@ impl Article { ], images: vec!["an_image.png".to_string()], category: "testing".to_string(), + locale: "fr".to_string(), } } } -- GitLab