3
3
import { session } from ' $app/stores' ;
4
4
import ListErrors from ' $components/ListErrors.svelte' ;
5
5
import * as api from ' $common/api.js' ;
6
+ import { ajax } from ' $common/actions.js' ;
6
7
7
8
export let article;
8
9
export let slug;
9
10
10
- let inProgress = false ;
11
+ let publishing = false ;
11
12
let errors;
12
13
13
- function addTag (input ) {
14
+ function add_tag (input ) {
14
15
article .tagList = [... article .tagList , input .value ];
15
16
input .value = ' ' ;
16
17
}
19
20
article .tagList = [... article .tagList .slice (0 , index), ... article .tagList .slice (index + 1 )];
20
21
}
21
22
22
- async function publish () {
23
- inProgress = true ;
23
+ const onsubmit = () => {
24
+ publishing = true ;
25
+ };
24
26
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' ));
31
30
}
32
-
33
- inProgress = false ;
34
- }
31
+ };
35
32
36
33
function enter (node , callback ) {
37
34
function onkeydown (event ) {
52
49
<div class =" container page" >
53
50
<div class =" row" >
54
51
<div class =" col-md-10 offset-md-1 col-xs-12" >
55
- <ListErrors {errors }/>
52
+ <ListErrors {errors } />
56
53
57
- <form >
54
+ <form
55
+ action ={slug ? ` /article/${slug }.json?_method=put ` : ` /article.json ` }
56
+ method =" post"
57
+ use:ajax ={{ onsubmit , onresponse }}
58
+ >
58
59
<fieldset >
59
60
<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
+ />
61
67
</fieldset >
62
68
63
69
<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
+ />
65
76
</fieldset >
66
77
67
78
<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
+ />
69
85
</fieldset >
70
86
71
87
<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
+ />
73
94
74
95
<div class =" tag-list" >
75
96
{#each article .tagList as tag , i }
76
97
<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 )} />
78
99
{tag }
79
100
</span >
80
101
{/each }
81
102
</div >
82
103
</fieldset >
83
104
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 }>
85
106
Publish Article
86
107
</button >
87
108
</fieldset >
88
109
</form >
89
110
</div >
90
111
</div >
91
112
</div >
92
- </div >
113
+ </div >
0 commit comments