license import { ActionCreator , Action } from 'redux' ; import { ThunkAction } from 'redux-thunk' ; import { RootState } from '../store.js' ; export const REQUEST_USER = 'REQUEST_USER' ; export const RECEIVE_USER = 'RECEIVE_USER' ; export const FAIL_USER = 'FAIL_USER' ; export interface UsersActionRequestUser extends Action < 'REQUEST_USER' > { userId : string ; } ; export interface UsersActionReceiveUser extends Action < 'RECEIVE_USER' > { userId : string ; data : object ; } ; export interface UsersActionFailUser extends Action < 'FAIL_USER' > { userId : string ; } ; export type UsersAction = UsersActionRequestUser | UsersActionReceiveUser | UsersActionFailUser ; type ThunkResult = ThunkAction < void , RootState , undefined , UsersAction > ; export const fetchUser = ( user ) => ( dispatch ) => { dispatch ( requestUser ( user . id ) ) ; fetch ( `https://node-hnapi.herokuapp.com/user/${ user . id }` ) . then ( res => res . json ( ) ) . then ( data => { if ( data . error ) { throw data . error ; } dispatch ( receiveUser ( user . id , data ) ) ; } ) . catch ( ( ) => dispatch ( failUser ( user . id ) ) ) ; } ; export const fetchUserIfNeeded = ( user ) => ( dispatch ) => { if ( user && ! user . created_time && ! user . isFetching ) { dispatch ( fetchUser ( user ) ) ; } } ; const requestUser = ( userId ) => { return { type : REQUEST_USER , userId } ; } ; const receiveUser = ( userId , data ) => { return { type : RECEIVE_USER , userId , data } ; } ; const failUser = ( userId ) => { return { type : FAIL_USER , userId } ; } ;