@@ -11,7 +11,8 @@ import {
1111 getHeaders
1212} from '../sources/helpers' ;
1313import {
14- setNotification
14+ setNotification ,
15+ setLoading
1516} from './app' ;
1617
1718export function authed ( token , tokenDecoded ) {
@@ -68,20 +69,24 @@ export function updateFolders(folders) {
6869 * @param token
6970 * @returns {Promise.<T> }
7071 */
71- export function verifyToken ( token ) {
72- return ( dispatch ) => {
73- return fetch ( `${ types . BASE_API } /auth/verify` , {
72+ export function verifyToken ( ) {
73+ return ( dispatch , getState ) => {
74+ return fetch ( `${ types . BASE } /auth/verify` , {
7475 method : 'post' ,
7576 headers : {
7677 'Accept' : 'application/json' ,
7778 'Content-Type' : 'application/json'
7879 } ,
79- body : JSON . stringify ( { token : token } )
80+ body : JSON . stringify ( { token : getState ( ) . auth . token } )
8081 } )
8182 . then ( checkStatus )
8283 . then ( parseJSON )
8384 . then ( json => {
84- return json . isValid ;
85+ if ( json . isValid ) {
86+ return json . isValid
87+ } else {
88+ dispatch ( logout ( ) ) ;
89+ }
8590 } )
8691 . catch ( ( err ) => {
8792 dispatch ( setNotification ( types . RESPONSE_FAILED , err . message ) ) ;
@@ -98,6 +103,7 @@ export function verifyToken(token) {
98103 */
99104export function signin ( body ) {
100105 return ( dispatch ) => {
106+ dispatch ( setLoading ( true ) ) ;
101107 return fetch ( `${ types . BASE } /login` , {
102108 method : 'post' ,
103109 headers : {
@@ -112,18 +118,19 @@ export function signin(body) {
112118 let tokenDecoded = decodeToken ( json ) ;
113119 dispatch ( saveToken ( json ) ) ;
114120 dispatch ( authed ( json , tokenDecoded ) ) ;
121+ dispatch ( setLoading ( false ) ) ;
115122 } )
116123 . catch ( ( err ) => {
117124 dispatch ( unauthed ( ) ) ;
118125 dispatch ( setNotification ( types . RESPONSE_FAILED , err . message ) ) ;
126+ dispatch ( setLoading ( false ) ) ;
119127 } )
120128 }
121129}
122130
123131/**
124132 * Get latest folders
125133 * once successful set current folder if existent
126- * @param currentFolderParam {string}
127134 * @returns {Function }
128135 */
129136export function getFolders ( ) {
@@ -149,11 +156,11 @@ export function getFolders() {
149156 * then call save collection
150157 * @param newFolder
151158 * @param newCollection
152- * @param currentFolderParam
153159 * @returns {function() }
154160 */
155161export function addNewFolderAndSaveCollection ( newFolder , newCollection ) {
156162 return ( dispatch ) => {
163+ dispatch ( setLoading ( true ) ) ;
157164 let endpoint = `${ types . BASE_API } /users/${ getUserId ( ) } /folders` ;
158165 return fetch ( endpoint , {
159166 method : 'post' ,
@@ -168,8 +175,10 @@ export function addNewFolderAndSaveCollection(newFolder, newCollection) {
168175 dispatch ( updateFolders ( json . folders ) ) ;
169176 // save new collection
170177 dispatch ( saveCollection ( newCollection ) ) ;
178+ dispatch ( setLoading ( false ) ) ;
171179 } ) . catch ( ( err ) => {
172180 dispatch ( setNotification ( types . RESPONSE_FAILED , err . message ) ) ;
181+ dispatch ( setLoading ( false ) ) ;
173182 } )
174183 }
175184}
@@ -181,6 +190,7 @@ export function addNewFolderAndSaveCollection(newFolder, newCollection) {
181190 */
182191export function saveCollection ( body ) {
183192 return ( dispatch ) => {
193+ dispatch ( setLoading ( true ) ) ;
184194 let endpoint = `${ types . BASE_API } /users/${ getUserId ( ) } /folders/${ body . folderId } /collections` ;
185195 return fetch ( endpoint , {
186196 method : 'post' ,
@@ -191,7 +201,9 @@ export function saveCollection(body) {
191201 . then ( parseJSON )
192202 . then ( json => {
193203 dispatch ( setNotification ( types . RESPONSE_SUCCESSFUL ) ) ;
204+ dispatch ( setLoading ( false ) ) ;
194205 } ) . catch ( ( err ) => {
206+ dispatch ( setLoading ( false ) ) ;
195207 dispatch ( setNotification ( types . RESPONSE_FAILED , err . message ) ) ;
196208 } )
197209 }
0 commit comments