[wip] category selector for article composer

This commit is contained in:
James Shiffer 2020-06-21 17:22:09 -07:00
parent 20579e2c17
commit 7fe7ddcfac
2 changed files with 32 additions and 5 deletions

View File

@ -16,7 +16,7 @@
const { session } = stores(); const { session } = stores();
let editor; let editor;
let title = '', image = ''; let title = '', image = '', category = '';
let actions = [ let actions = [
{ {
@ -26,6 +26,7 @@
result: function save() { result: function save() {
window.localStorage['title'] = title; window.localStorage['title'] = title;
window.localStorage['image'] = image; window.localStorage['image'] = image;
window.localStorage['category'] = category;
window.localStorage['html'] = editor.getHtml(true); window.localStorage['html'] = editor.getHtml(true);
alert('Successfully saved draft to browser local storage'); alert('Successfully saved draft to browser local storage');
} }
@ -53,6 +54,7 @@
onMount(function load() { onMount(function load() {
title = window.localStorage['title'] || ''; title = window.localStorage['title'] || '';
image = window.localStorage['image'] || ''; image = window.localStorage['image'] || '';
category = window.localStorage['category'] || '';
editor.setHtml(window.localStorage['html'] || '', false); editor.setHtml(window.localStorage['html'] || '', false);
}); });
@ -88,6 +90,15 @@
<form method="POST" action="/cms/article"> <form method="POST" action="/cms/article">
<p>Article Title: <input type="text" name="title" bind:value={title} required placeholder="How to Assassinate the Governor of California"></p> <p>Article Title: <input type="text" name="title" bind:value={title} required placeholder="How to Assassinate the Governor of California"></p>
<p>Article Author: <strong>{$session.user.realname}</strong></p> <p>Article Author: <strong>{$session.user.realname}</strong></p>
<p>Article Category: <select>
{#await fetch('/c').then(r => r.json())}
Loading...
{:then categories}
{#each categories as category}
<option value={category}>{category}</option>
{/each}
{/await}
</select></p>
<p>Article Header Image URL: <input type="text" name="image" bind:value={image} required placeholder="http:// ..."></p> <p>Article Header Image URL: <input type="text" name="image" bind:value={image} required placeholder="http:// ..."></p>
</form> </form>
<Editor bind:this={editor} {actions} /> <Editor bind:this={editor} {actions} />

View File

@ -205,16 +205,25 @@ express()
isAuthor, isAuthor,
async function(req, res, next) { async function(req, res, next) {
try { try {
const { html, title, image } = req.body; const { html, title, image, category } = req.body;
if (!title || !image || !html) { if (!title || !image || !html || !category) {
res.writeHead(422, { res.writeHead(422, {
'Content-Type': 'application/json' 'Content-Type': 'application/json'
}); });
res.end(JSON.stringify({ res.end(JSON.stringify({
message: `You must supply an article title, image URL, and content.` message: `You must supply an article title, image URL, category, and content.`
})); }));
} }
const article = await new Article({ html, title, image, author: req.user._id }); const cat = await Category.findOne({ name: category });
if (!cat) {
res.writeHead(404, {
'Content-Type': 'application/json'
});
res.end(JSON.stringify({
message: `That category does not exist.`
}));
}
const article = await new Article({ html, title, image, category: cat, author: req.user._id });
await article.save(); await article.save();
res.writeHead(200, { res.writeHead(200, {
'Content-Type': 'application/json' 'Content-Type': 'application/json'
@ -288,7 +297,14 @@ express()
'Content-Type': 'application/json' 'Content-Type': 'application/json'
}); });
res.end(JSON.stringify(articles)); res.end(JSON.stringify(articles));
})
.get('/c', async function (req, res, next) {
let categories = await Category.find();
res.writeHead(200, {
'Content-Type': 'application/json'
});
res.end(JSON.stringify(categories));
}) })
.get('/me', function(req, res, next) { .get('/me', function(req, res, next) {