From a81375fc3406cb61c6f1a1049975c6237bc0d7d2 Mon Sep 17 00:00:00 2001 From: "Alexander \"Arav\" Andreev" Date: Sun, 23 Jul 2023 23:12:14 +0400 Subject: [PATCH] Removed ability to create new category when editing/creating a post. --- internal/http/api_mindflow_handlers.go | 27 ++++++-------------------- web/assets/css/mindflow_admin.css | 3 +-- web/assets/js/mindflow_admin.js | 1 - web/templates/mindflow_admin.pug | 13 +++---------- 4 files changed, 10 insertions(+), 34 deletions(-) diff --git a/internal/http/api_mindflow_handlers.go b/internal/http/api_mindflow_handlers.go index 5c0d1bf..b44b45d 100644 --- a/internal/http/api_mindflow_handlers.go +++ b/internal/http/api_mindflow_handlers.go @@ -60,7 +60,7 @@ func (h *MindflowApiHandlers) NewPost(w http.ResponseWriter, r *http.Request) { func (h *MindflowApiHandlers) EditPost(w http.ResponseWriter, r *http.Request) { var post *mindflow.Post - var category *mindflow.Category + var category mindflow.Category var err error if strings.Contains(r.Header.Get("Content-Type"), "application/x-www-form-urlencoded") { @@ -68,28 +68,13 @@ func (h *MindflowApiHandlers) EditPost(w http.ResponseWriter, r *http.Request) { body := cleanupNewlines(r.FormValue("body")) - if r.FormValue("category") != "" { - category, err = mindflow.NewCategory(r.FormValue("new-category")) - if err != nil { - http.Error(w, err.Error(), http.StatusInternalServerError) - return - } - - category.ID, _ = strconv.ParseInt(r.FormValue("category"), 10, 64) - - if category.ID == 0 { - category.ID, err = h.db.NewCategory(category) - if err != nil { - http.Error(w, err.Error(), http.StatusInternalServerError) - return - } - } - } else { - category = &mindflow.Category{} - category.ID, _ = strconv.ParseInt(r.FormValue("old-category"), 10, 64) + category.ID, err = strconv.ParseInt(r.FormValue("category"), 10, 64) + if err != nil { + http.Error(w, err.Error(), http.StatusBadRequest) + return } - post, err = mindflow.NewPost(*category, r.FormValue("title"), body) + post, err = mindflow.NewPost(category, r.FormValue("title"), body) if err != nil { http.Error(w, err.Error(), http.StatusInternalServerError) return diff --git a/web/assets/css/mindflow_admin.css b/web/assets/css/mindflow_admin.css index aafaf20..d8b3cbc 100644 --- a/web/assets/css/mindflow_admin.css +++ b/web/assets/css/mindflow_admin.css @@ -17,7 +17,7 @@ form#add { display: grid; gap: .5rem; grid-template-areas: - "c n" + "c c" "t t" "b b" "a a"; @@ -27,7 +27,6 @@ form#add { width: 100%; } form#add select { grid-area: c; } -form#add input[name="new-category"] { grid-area: n; } form#add input[name="title"] { grid-area: t; } form#add textarea[name="body"] { grid-area: b; } form#add button[type="submit"] { grid-area: a; } diff --git a/web/assets/js/mindflow_admin.js b/web/assets/js/mindflow_admin.js index 344e74a..76ff1d3 100644 --- a/web/assets/js/mindflow_admin.js +++ b/web/assets/js/mindflow_admin.js @@ -6,7 +6,6 @@ function edit_post(e) { e.preventDefault(); let data = new URLSearchParams(); data.append("category", get_field(e.target, "category")) - data.append("new-category", get_field(e.target, "new-category")) data.append("old-category", get_field(e.target, "old-category")) data.append("title", get_field(e.target, "title")) data.append("body", get_field(e.target, "body")) diff --git a/web/templates/mindflow_admin.pug b/web/templates/mindflow_admin.pug index ddca517..3bcc66b 100755 --- a/web/templates/mindflow_admin.pug +++ b/web/templates/mindflow_admin.pug @@ -25,7 +25,7 @@ block content each category in categories option(value=category.ID) #{category.Name} option(value='0') -- New category -- - input(type='text', placeholder='Category name' name='name') + input(type='text', placeholder='New category name' name='name') button(type="submit" name="add") Add button(type="submit" name="edit") Edit button(type="submit" name="delete") Delete @@ -35,15 +35,10 @@ block content form(id='add' action='/api/mindflow', method='POST') select(name='category' required='') each category in categories - if (category.ID == 1) - option(value=category.ID selected='') #{category.Name} - else - option(value=category.ID) #{category.Name} - option(value='0') -- New category -- - input(type='text', placeholder='New category name' name='new-category') + option(value=category.ID) #{category.Name} input(type='text', placeholder='Title' name='title' required='') textarea(placeholder='Body post' name='body' required='') - button(type="submit") Add + button(type="submit") Add section if (len(posts) > 0) each post in posts @@ -58,8 +53,6 @@ block content option(value=category.ID selected='') #{category.Name} else option(value=category.ID) #{category.Name} - option(value='0') -- New category -- - input(type='text', placeholder='New category name' name='new-category') input(type='hidden', name='post-id' value=post.ID) input(type='hidden', name='old-category' value=post.Category.ID) input(type='text', placeholder='Title' name='title' value=post.Title required='')