1
1
import { createProcess } from '@dojo/framework/stores/process' ;
2
- import { remove , replace } from '@dojo/framework/stores/state/operations' ;
3
2
import { getHeaders , commandFactory } from './utils' ;
4
3
import { baseUrl } from '../config' ;
5
4
import {
@@ -11,66 +10,64 @@ import {
11
10
NewCommentPayload
12
11
} from './interfaces' ;
13
12
14
- const startLoadingArticleCommand = commandFactory ( ( { path, payload : { slug } } ) => {
15
- return [
16
- replace ( path ( 'article' , slug , 'item' ) , undefined ) ,
17
- replace ( path ( 'article' , slug , 'comments' ) , [ ] ) ,
18
- replace ( path ( 'article' , slug , 'isLoading' ) , true ) ,
19
- replace ( path ( 'article' , slug , 'isLoaded' ) , false )
20
- ] ;
13
+ const startLoadingArticleCommand = commandFactory ( ( { state, payload : { slug } } ) => {
14
+ state . article [ slug ] = {
15
+ item : undefined ,
16
+ comments : [ ] ,
17
+ newComment : '' ,
18
+ isLoading : true ,
19
+ isLoaded : false
20
+ } ;
21
21
} ) ;
22
22
23
- const loadArticleCommand = commandFactory < SlugPayload > ( async ( { get , path , payload : { slug } } ) => {
24
- const token = get ( path ( ' session' , ' token' ) ) ;
23
+ const loadArticleCommand = commandFactory < SlugPayload > ( async ( { state , payload : { slug } } ) => {
24
+ const token = state . session ?. token ;
25
25
const response = await fetch ( `${ baseUrl } /articles/${ slug } ` , {
26
26
headers : getHeaders ( token )
27
27
} ) ;
28
28
const json = await response . json ( ) ;
29
29
30
- return [
31
- replace ( path ( 'article' , slug , 'item' ) , json . article ) ,
32
- replace ( path ( 'article' , slug , 'isLoading' ) , false ) ,
33
- replace ( path ( 'article' , slug , 'isLoaded' ) , true )
34
- ] ;
30
+ state . article [ slug ] . item = json . article ;
31
+ state . article [ slug ] . isLoading = false ;
32
+ state . article [ slug ] . isLoaded = true ;
35
33
} ) ;
36
34
37
35
const favoriteArticleCommand = commandFactory < FavoriteArticlePayload > (
38
- async ( { get , path , payload : { slug, favorited } } ) => {
39
- const token = get ( path ( ' session' , ' token' ) ) ;
36
+ async ( { state , payload : { slug, favorited } } ) => {
37
+ const token = state . session ?. token ;
40
38
const response = await fetch ( `${ baseUrl } /articles/${ slug } /favorite` , {
41
39
method : favorited ? 'delete' : 'post' ,
42
40
headers : getHeaders ( token )
43
41
} ) ;
44
42
const json = await response . json ( ) ;
45
- return [ replace ( path ( ' article' , slug , ' item' ) , json . article ) ] ;
43
+ state . article [ slug ] . item = json . article ;
46
44
}
47
45
) ;
48
46
49
47
const followUserCommand = commandFactory < Required < FollowUserPayload > > (
50
- async ( { get , path , payload : { slug, username, following } } ) => {
51
- const token = get ( path ( ' session' , ' token' ) ) ;
48
+ async ( { state , payload : { slug, username, following } } ) => {
49
+ const token = state . session ?. token ;
52
50
const response = await fetch ( `${ baseUrl } /profiles/${ username } /follow` , {
53
51
method : following ? 'delete' : 'post' ,
54
52
headers : getHeaders ( token )
55
53
} ) ;
56
54
const json = await response . json ( ) ;
57
- const article = get ( path ( ' article' , slug , ' item' ) ) ;
58
- return [ replace ( path ( ' article' , slug , ' item' ) , { ...article , author : json . profile } ) ] ;
55
+ const article = state . article [ slug ] ?. item ;
56
+ state . article [ slug ] . item = article && { ...article , author : json . profile } ;
59
57
}
60
58
) ;
61
59
62
- const loadCommentsCommand = commandFactory < SlugPayload > ( async ( { get , path , payload : { slug } } ) => {
63
- const token = get ( path ( ' session' , ' token' ) ) ;
60
+ const loadCommentsCommand = commandFactory < SlugPayload > ( async ( { state , payload : { slug } } ) => {
61
+ const token = state . session ?. token ;
64
62
const response = await fetch ( `${ baseUrl } /articles/${ slug } /comments` , {
65
63
headers : getHeaders ( token )
66
64
} ) ;
67
65
const json = await response . json ( ) ;
68
-
69
- return [ replace ( path ( 'article' , slug , 'comments' ) , json . comments ) ] ;
66
+ state . article [ slug ] . comments = json . comments ;
70
67
} ) ;
71
68
72
- const addCommentCommand = commandFactory < AddCommentPayload > ( async ( { get , path , payload : { slug, newComment } } ) => {
73
- const token = get ( path ( ' session' , ' token' ) ) ;
69
+ const addCommentCommand = commandFactory < AddCommentPayload > ( async ( { state , payload : { slug, newComment } } ) => {
70
+ const token = state . session ?. token ;
74
71
const requestPayload = {
75
72
comment : {
76
73
body : newComment
@@ -82,21 +79,19 @@ const addCommentCommand = commandFactory<AddCommentPayload>(async ({ get, path,
82
79
body : JSON . stringify ( requestPayload )
83
80
} ) ;
84
81
const json = await response . json ( ) ;
85
- const comments = get ( path ( ' article' , slug , ' comments' ) ) ;
82
+ const comments = state . article [ slug ] . comments ;
86
83
87
- return [
88
- replace ( path ( 'article' , slug , 'comments' ) , [ ...comments , json . comment ] ) ,
89
- replace ( path ( 'article' , slug , 'newComment' ) , '' )
90
- ] ;
84
+ state . article [ slug ] . comments = [ ...comments , json . comment ] ;
85
+ state . article [ slug ] . newComment = '' ;
91
86
} ) ;
92
87
93
- const deleteCommentCommand = commandFactory < DeleteCommentPayload > ( async ( { at , get , path , payload : { slug, id } } ) => {
94
- const token = get ( path ( ' session' , ' token' ) ) ;
88
+ const deleteCommentCommand = commandFactory < DeleteCommentPayload > ( async ( { state , payload : { slug, id } } ) => {
89
+ const token = state . session ?. token ;
95
90
await fetch ( `${ baseUrl } /articles/${ slug } /comments/${ id } ` , {
96
91
method : 'delete' ,
97
92
headers : getHeaders ( token )
98
93
} ) ;
99
- const comments = get ( path ( ' article' , slug , ' comments' ) ) ;
94
+ const comments = state . article [ slug ] . comments ;
100
95
let index = - 1 ;
101
96
for ( let i = 0 ; i < comments . length ; i ++ ) {
102
97
if ( comments [ i ] . id === id ) {
@@ -106,22 +101,21 @@ const deleteCommentCommand = commandFactory<DeleteCommentPayload>(async ({ at, g
106
101
}
107
102
108
103
if ( index !== - 1 ) {
109
- return [ remove ( at ( path ( ' article' , slug , ' comments' ) , index ) ) ] ;
104
+ state . article [ slug ] . comments . splice ( index , 1 ) ;
110
105
}
111
- return [ ] ;
112
106
} ) ;
113
107
114
- const newCommentInputCommand = commandFactory < NewCommentPayload > ( ( { path , payload : { newComment, slug } } ) => {
115
- return [ replace ( path ( ' article' , slug , ' newComment' ) , newComment ) ] ;
108
+ const newCommentInputCommand = commandFactory < NewCommentPayload > ( ( { state , payload : { newComment, slug } } ) => {
109
+ state . article [ slug ] . newComment = newComment ;
116
110
} ) ;
117
111
118
- const deleteArticleCommand = commandFactory < SlugPayload > ( async ( { get , path , payload : { slug } } ) => {
119
- const token = get ( path ( ' session' , ' token' ) ) ;
112
+ const deleteArticleCommand = commandFactory < SlugPayload > ( async ( { state , payload : { slug } } ) => {
113
+ const token = state . session ?. token ;
120
114
await fetch ( `${ baseUrl } /articles/${ slug } ` , {
121
115
method : 'delete' ,
122
116
headers : getHeaders ( token )
123
117
} ) ;
124
- return [ replace ( path ( ' routing' , ' outlet' ) , 'home' ) ] ;
118
+ state . routing . outlet = 'home' ;
125
119
} ) ;
126
120
127
121
export const getArticleProcess = createProcess ( 'get-article' , [
0 commit comments