66 fetchLogIn ,
77 fetchNewPost ,
88 fetchPosts ,
9+ fetchUser ,
910} from "../adapters" ;
1011
1112//USERS############################################################################
@@ -29,11 +30,30 @@ export const loggingIn = (dispatch, userData) => {
2930 return ( dispatch ) => {
3031 dispatch ( { type : "ASYNC_START" } ) ;
3132 fetchLogIn ( userData ) . then ( ( data ) => {
32- if ( data . user ) {
33+ if ( data !== "false" ) {
34+ window . localStorage . setItem ( "userId" , JSON . stringify ( data . user . id ) ) ;
35+ dispatch ( { type : LOG_IN , data } ) ;
36+ history . push ( "/dashboard" ) ;
37+ } else {
38+ history . push ( "/" ) ;
39+ alert ( "Please login again." ) ;
40+ }
41+ } ) ;
42+ } ;
43+ } ;
44+
45+ export const setCurrentUser = ( dispatch ) => {
46+ return ( dispatch ) => {
47+ dispatch ( { type : "ASYNC_START" } ) ;
48+ fetchUser ( ) . then ( ( data ) => {
49+ if ( data . logged_in !== false ) {
50+ debugger ;
51+ window . localStorage . setItem ( "userId" , `${ data . user . id } ` ) ;
3352 dispatch ( { type : LOG_IN , data } ) ;
3453 history . push ( "/dashboard" ) ;
3554 } else {
36- alert ( "Login failed, please try again." ) ;
55+ history . push ( "/" ) ;
56+ alert ( "Please login again." ) ;
3757 }
3858 } ) ;
3959 } ;
@@ -44,17 +64,19 @@ export const loggingIn = (dispatch, userData) => {
4464export const newPost = ( dispatch , postData ) => {
4565 return ( dispatch ) => {
4666 dispatch ( { type : "ASYNC_START" } ) ;
47- fetchNewPost ( postData ) . then ( ( data ) => {
48- dispatch ( { type : NEW_POST , data } ) ;
49- } ) ;
67+ fetchNewPost ( postData ) ;
5068 } ;
5169} ;
5270
5371export const getAllPosts = ( dispatch ) => {
5472 return ( dispatch ) => {
5573 dispatch ( { type : "ASYNC_START" } ) ;
5674 fetchPosts ( ) . then ( ( data ) => {
57- dispatch ( { type : GET_POSTS , data } ) ;
75+ if ( data !== null ) {
76+ dispatch ( { type : GET_POSTS , data } ) ;
77+ } else {
78+ return null ;
79+ }
5880 } ) ;
5981 } ;
6082} ;
0 commit comments