import { Offer } from 'src/models/offers' ; import createReducer from 'src/store/utils/createReducer' ; import { GET_OFFERS_FOR_REQUEST , IgetRequestOffers , OBSERVE_OFFERS , OffersState , RESET_SET , SET , SET_TEMP_OFFER , } from './types' ; const initialSetActionState = { loading : false , success : undefined , error : undefined , modalState : false , } ; const initialState = { forRequest : undefined , data : undefined , loading : false , error : undefined , observerReceivedFirstUpdate : false , setAction : initialSetActionState , } ; export default createReducer < OffersState > ( { [ SET . PENDING ] : ( state ) => { state . setAction . loading = true ; state . setAction . error = undefined ; state . newOfferTemp = undefined ; } , [ SET . COMPLETED ] : ( state , { payload } ) => { state . setAction . error = undefined ; state . setAction . loading = false ; state . setAction . success = payload ; } , [ SET . REJECTED ] : ( state , { payload } ) => { state . setAction . loading = false ; state . setAction . error = payload ; state . setAction . success = undefined ; } , [ SET_TEMP_OFFER ] : ( state , { payload , } ) => { state . newOfferTemp = payload ; } , [ RESET_SET ] : ( state ) => { state . setAction . loading = false ; state . setAction . success = undefined ; } , [ OBSERVE_OFFERS . SUBSCRIBE ] : ( state ) => { state . loading = true ; } , [ OBSERVE_OFFERS . UPDATED ] : ( state , { payload , } ) => { state . data = payload . docs . reduce ( ( acc , doc ) => ( { ... acc , [ doc . id ] : doc . data ( ) , } ) , { } ) ; state . loading = false ; state . observerReceivedFirstUpdate = true ; } , [ OBSERVE_OFFERS . ERROR ] : ( state , { payload } ) => { state . error = payload ; state . loading = false ; } , [ GET_OFFERS_FOR_REQUEST . PENDING ] : ( state , { payload } ) => { state . forRequest = { loading : true , data : undefined , requestId : payload . requestId , error : undefined , } ; } , [ GET_OFFERS_FOR_REQUEST . COMPLETED ] : ( state , { payload , } ) => { if ( state . forRequest ) { state . forRequest . data = payload . docs . reduce ( ( acc , doc ) => ( { ... acc , [ doc . id ] : doc . data ( ) , } ) , { } ) ; state . forRequest . loading = false ; } } , [ GET_OFFERS_FOR_REQUEST . REJECTED ] : ( state , { payload , } ) => { if ( state . forRequest ) { state . forRequest . error = payload ; state . forRequest . loading = false ; } } , } , initialState ) ;