license import { ActionCreator , Action } from 'redux' ; import { ThunkAction } from 'redux-thunk' ; import { RootState } from '../store.js' ; export const REQUEST_LIST = 'REQUEST_LIST' ; export const RECEIVE_LIST = 'RECEIVE_LIST' ; export const FAIL_LIST = 'FAIL_LIST' ; export interface ListsActionRequestList extends Action < 'REQUEST_LIST' > { listId : string ; page : number ; } ; export interface ListsActionReceiveList extends Action < 'RECEIVE_LIST' > { listId : string ; page : number ; items : Array < { id : string ; } > ; } ; export interface ListsActionFailList extends Action < 'FAIL_LIST' > { listId : string ; page : number ; } ; export type ListsAction = ListsActionRequestList | ListsActionReceiveList | ListsActionFailList ; type ThunkResult = ThunkAction < void , RootState , undefined , ListsAction > ; export const fetchList = ( list , page ) => ( dispatch ) => { dispatch ( requestList ( list . id , page ) ) ; fetch ( `https://node-hnapi.herokuapp.com/${ list . id }?page=${ page }` ) . then ( res => res . json ( ) ) . then ( items => dispatch ( receiveList ( list . id , page , items ) ) ) . catch ( ( ) => dispatch ( failList ( list . id , page ) ) ) ; } ; export const fetchListIfNeeded = ( list , page ) => ( dispatch ) => { if ( ! list ) return ; if ( ! list . pages || ! list . pages [ page ] || ( ! list . pages [ page ] . items && ! list . pages [ page ] . isFetching ) ) { dispatch ( fetchList ( list , page ) ) ; } } ; const requestList = ( listId , page ) => { return { type : REQUEST_LIST , listId , page } ; } ; const receiveList = ( listId , page , items ) => { return { type : RECEIVE_LIST , listId , page , items } ; } ; const failList = ( listId , page ) => { return { type : FAIL_LIST , listId , page } ; } ;