import AppClient from '../httpClients/appClient' ; import { ThunkAction , ThunkDispatch } from 'redux-thunk' ; import { Action } from 'redux' ; import { ItemDetail , ItemSimple , UserItemsRes , UserTransactionsRes , } from '../types/appApiTypes' ; import { TimelineItem , TransactionItem } from '../dataObjects/item' ; import { UserData } from '../dataObjects/user' ; import { ShippingStatus } from '../dataObjects/shipping' ; import { TransactionStatus } from '../dataObjects/transaction' ; import { AppState } from '../index' ; import { ErrorActions } from './errorAction' ; import { checkAppResponse } from '../actionHelper/responseChecker' ; import { ajaxErrorHandler } from '../actionHelper/ajaxErrorHandler' ; export const FETCH_USER_PAGE_DATA_START = 'FETCH_USER_PAGE_DATA_START' ; export const FETCH_USER_PAGE_DATA_SUCCESS = 'FETCH_USER_PAGE_DATA_SUCCESS' ; export const FETCH_USER_PAGE_DATA_FAIL = 'FETCH_USER_PAGE_DATA_FAIL' ; export type FetchUserPageDataActions = FetchUserPageDataStartAction | FetchUserPageDataSuccessAction | FetchUserPageDataFailAction | ErrorActions ; type ThunkResult < R > = ThunkAction < R , AppState , undefined , FetchUserPageDataActions > ; async function fetchUserPageData ( userId , isMyPage ) { const userDataRes = await AppClient . get ( `/users/${ userId }.json` ) ; await checkAppResponse ( userDataRes ) ; const userData = await userDataRes . json ( ) ; let transactions ; if ( isMyPage ) { const transactionRes = await AppClient . get ( '/users/transactions.json' ) ; await checkAppResponse ( transactionRes ) ; transactions = await transactionRes . json ( ) ; } return [ userData , transactions ] ; } export function fetchUserPageDataAction ( userId , isMyPage ) { return ( dispatch ) => { Promise . resolve ( ) . then ( ( ) => { dispatch ( fetchUserPageDataStartAction ( ) ) ; } ) . then ( ( ) => { return fetchUserPageData ( userId , isMyPage ) ; } ) . then ( ( res ) => { const userPageData = res [ 0 ] ; const payload = { user : { id : userPageData . user . id , accountName : userPageData . user . account_name , numSellItems : userPageData . user . num_sell_items , } , items : userPageData . items . map ( ( item ) => ( { id : item . id , status : item . status , name : item . name , price : item . price , thumbnailUrl : item . image_url , createdAt : item . created_at , } ) ) , itemsHasNext : userPageData . has_next , } ; let transactions = { transactions : [ ] , transactionsHasNext : false , } ; if ( isMyPage && res [ 1 ] !== undefined ) { const transactionRes = res [ 1 ] ; transactions = { transactions : transactionRes . items . map ( ( item ) => ( { id : item . id , status : item . status , transactionEvidenceStatus : item . transaction_evidence_status as TransactionStatus , shippingStatus : item . shipping_status as ShippingStatus , name : item . name , thumbnailUrl : item . image_url , createdAt : item . created_at , } ) ) , transactionsHasNext : transactionRes . has_next , } ; } dispatch ( fetchUserPageDataSuccessAction ( { ... payload , ... transactions } ) ) ; } ) . catch ( async ( err ) => { dispatch < FetchUserPageDataActions > ( await ajaxErrorHandler ( err , fetchUserPageDataFailAction ) ) ; } ) ; } ; } export interface FetchUserPageDataStartAction extends Action < typeof FETCH_USER_PAGE_DATA_START > { } const fetchUserPageDataStartAction = ( ) : FetchUserPageDataStartAction => { return { type : FETCH_USER_PAGE_DATA_START , } ; } ; export interface FetchUserPageDataSuccessAction extends Action < typeof FETCH_USER_PAGE_DATA_SUCCESS > { payload : { user : UserData ; items : TimelineItem [ ] ; itemsHasNext : boolean ; transactions : TransactionItem [ ] ; transactionsHasNext : boolean ; } ; } const fetchUserPageDataSuccessAction = ( payload ) : FetchUserPageDataSuccessAction => { return { type : FETCH_USER_PAGE_DATA_SUCCESS , payload , } ; } ; export interface FetchUserPageDataFailAction extends Action < typeof FETCH_USER_PAGE_DATA_FAIL > { message : string ; } const fetchUserPageDataFailAction = ( message ) : FetchUserPageDataFailAction => { return { type : FETCH_USER_PAGE_DATA_FAIL , message } ; } ;