Skip to content

Commit 39a6aa0

Browse files
committed
some changes i apparently made before christmas
1 parent bcbb033 commit 39a6aa0

File tree

3 files changed

+48
-22
lines changed

3 files changed

+48
-22
lines changed

examples/realworld.svelte.dev/src/routes/article/[slug]/index.json.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,3 +8,7 @@ export async function get(request, context) {
88
body: article
99
};
1010
}
11+
12+
export async function put(request, context) {
13+
console.log('put', request);
14+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export async function post(request, context) {}

examples/realworld.svelte.dev/src/routes/editor/_Editor.svelte

Lines changed: 43 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,15 @@
33
import { session } from '$app/stores';
44
import ListErrors from '$components/ListErrors.svelte';
55
import * as api from '$common/api.js';
6+
import { ajax } from '$common/actions.js';
67
78
export let article;
89
export let slug;
910
10-
let inProgress = false;
11+
let publishing = false;
1112
let errors;
1213
13-
function addTag(input) {
14+
function add_tag(input) {
1415
article.tagList = [...article.tagList, input.value];
1516
input.value = '';
1617
}
@@ -19,19 +20,15 @@
1920
article.tagList = [...article.tagList.slice(0, index), ...article.tagList.slice(index + 1)];
2021
}
2122
22-
async function publish() {
23-
inProgress = true;
23+
const onsubmit = () => {
24+
publishing = true;
25+
};
2426
25-
const response = await (slug
26-
? api.put(`articles/${slug}`, { article }, $session.user && $session.user.token)
27-
: api.post('articles', { article }, $session.user && $session.user.token));
28-
29-
if (response.article) {
30-
goto(`/article/${response.article.slug}`);
27+
const onresponse = async (res) => {
28+
if (res.ok) {
29+
goto(res.headers.get('location'));
3130
}
32-
33-
inProgress = false;
34-
}
31+
};
3532
3633
function enter(node, callback) {
3734
function onkeydown(event) {
@@ -52,41 +49,65 @@
5249
<div class="container page">
5350
<div class="row">
5451
<div class="col-md-10 offset-md-1 col-xs-12">
55-
<ListErrors {errors}/>
52+
<ListErrors {errors} />
5653

57-
<form>
54+
<form
55+
action={slug ? `/article/${slug}.json?_method=put` : `/article.json`}
56+
method="post"
57+
use:ajax={{ onsubmit, onresponse }}
58+
>
5859
<fieldset>
5960
<fieldset class="form-group">
60-
<input class="form-control form-control-lg" type="text" placeholder="Article Title" bind:value={article.title}>
61+
<input
62+
class="form-control form-control-lg"
63+
type="text"
64+
placeholder="Article Title"
65+
bind:value={article.title}
66+
/>
6167
</fieldset>
6268

6369
<fieldset class="form-group">
64-
<input class="form-control" type="text" placeholder="What's this article about?" bind:value={article.description}>
70+
<input
71+
class="form-control"
72+
type="text"
73+
placeholder="What's this article about?"
74+
bind:value={article.description}
75+
/>
6576
</fieldset>
6677

6778
<fieldset class="form-group">
68-
<textarea class="form-control" rows="8" placeholder="Write your article (in markdown)" bind:value={article.body}/>
79+
<textarea
80+
class="form-control"
81+
rows="8"
82+
placeholder="Write your article (in markdown)"
83+
bind:value={article.body}
84+
/>
6985
</fieldset>
7086

7187
<fieldset class="form-group">
72-
<input class="form-control" type="text" placeholder="Enter tags" use:enter={addTag}>
88+
<input
89+
class="form-control"
90+
type="text"
91+
placeholder="Enter tags"
92+
use:enter={add_tag}
93+
/>
7394

7495
<div class="tag-list">
7596
{#each article.tagList as tag, i}
7697
<span class="tag-default tag-pill">
77-
<i class="ion-close-round" on:click='{() => remove(i)}'/>
98+
<i class="ion-close-round" on:click={() => remove(i)} />
7899
{tag}
79100
</span>
80101
{/each}
81102
</div>
82103
</fieldset>
83104

84-
<button class="btn btn-lg pull-xs-right btn-primary" type="button" disabled={inProgress} on:click={publish}>
105+
<button class="btn btn-lg pull-xs-right btn-primary" disabled={publishing}>
85106
Publish Article
86107
</button>
87108
</fieldset>
88109
</form>
89110
</div>
90111
</div>
91112
</div>
92-
</div>
113+
</div>

0 commit comments

Comments
 (0)