Edit an Article
+Choose an article to edit:
+-
+ {#each articles as article}
+
- + + {article.title} by {article.author.realname} + ({article.views} views) + + + {:else} +
- There are no published articles. + {/each} +
From 60a76979df5170ab4026e1122cd1e8f31d174e50 Mon Sep 17 00:00:00 2001 From: James Shiffer <2191476+scoliono@users.noreply.github.com> Date: Tue, 1 Sep 2020 21:30:40 -0700 Subject: [PATCH] article edit feature --- src/models/article.js | 1 + src/routes/a/[slug].json.js | 4 +- src/routes/a/[slug].svelte | 3 ++ src/routes/cms/create.svelte | 13 +++-- src/routes/cms/edit.svelte | 42 ++++++++++++++++ src/routes/cms/index.svelte | 2 +- src/server.js | 97 ++++++++++++++++++++++++------------ 7 files changed, 123 insertions(+), 39 deletions(-) create mode 100644 src/routes/cms/edit.svelte diff --git a/src/models/article.js b/src/models/article.js index 550baf4..5cc4c10 100644 --- a/src/models/article.js +++ b/src/models/article.js @@ -11,6 +11,7 @@ const ArticleSchema = new Schema({ slug: { type: String, index: { unique: true } }, image: { type: String, required: true }, created_at: { type: Date, default: Date.now }, + updated_at: { type: Date }, html: { type: String, required: true }, comments: [{ content: { type: String, required: true }, diff --git a/src/routes/a/[slug].json.js b/src/routes/a/[slug].json.js index 22b1771..1abc16d 100644 --- a/src/routes/a/[slug].json.js +++ b/src/routes/a/[slug].json.js @@ -12,7 +12,9 @@ export async function get(req, res, next) { }); if (article) { - article.set({ views: article.views + 1 }); + if (req.query.no_view !== '1') { + article.set({ views: article.views + 1 }); + } await article.save(); res.writeHead(200, { 'Content-Type': 'application/json' diff --git a/src/routes/a/[slug].svelte b/src/routes/a/[slug].svelte index 46d51bd..435a501 100644 --- a/src/routes/a/[slug].svelte +++ b/src/routes/a/[slug].svelte @@ -214,6 +214,9 @@
Author: {article.author.realname}
Category: {article.category.name}
Published: {new Date(article.created_at).toLocaleString()}
+ {#if article.updated_at} +Last Updated: {new Date(article.updated_at).toLocaleString()}
+ {/if}Views: {article.views}
diff --git a/src/routes/cms/create.svelte b/src/routes/cms/create.svelte index fec1d73..6cf6009 100644 --- a/src/routes/cms/create.svelte +++ b/src/routes/cms/create.svelte @@ -6,7 +6,9 @@ } const res = await this.fetch('/c.json'); const categories = await res.json(); - return { user: session.user, categories }; + const origRes = page.query.edit && await this.fetch(`/a/${page.query.edit}.json?no_view=1`) + const editArticle = origRes && await origRes.json(); + return { user: session.user, categories, editArticle }; } @@ -20,6 +22,7 @@ let editor, form, uploadForm; let loading = false, loadingAttach = false; let title = '', category = ''; + export let editArticle = undefined; export let categories; let actions = [ @@ -55,9 +58,9 @@ ]; onMount(function load() { - title = window.localStorage['title'] || ''; - category = window.localStorage['category'] || ''; - editor.setHtml(window.localStorage['html'] || '', false); + title = editArticle ? editArticle.title : (window.localStorage['title'] || ''); + category = editArticle ? editArticle.category.slug : (window.localStorage['category'] || ''); + editor.setHtml(editArticle ? editArticle.html : (window.localStorage['html'] || ''), false); }); async function submit() @@ -67,7 +70,7 @@ fd.append('html', html); loading = true; try { - const res = await fetch(`/cms/article`, { + const res = await fetch(editArticle ? `/cms/article/${editArticle.slug}` : `/cms/article`, { method: 'POST', headers: { 'Accept': 'application/json' diff --git a/src/routes/cms/edit.svelte b/src/routes/cms/edit.svelte new file mode 100644 index 0000000..f888651 --- /dev/null +++ b/src/routes/cms/edit.svelte @@ -0,0 +1,42 @@ + + +Choose an article to edit:
+Edit an existing article Coming soon!