import { Request , RequestStatus } from 'src/models/requests' ; import { IRequestWithOffersAndTimeline , RequestWithOffersAndTimeline , } from 'src/models/requests/RequestWithOffersAndTimeline' ; import createReducer from 'src/store/utils/createReducer' ; import { CHANGE_MODAL , GET_ACCEPTED , GET_ARCHIVED , GET_FINISHED , GET_ONGOING , GET_OPEN , OBSERVE_CANCELLED_REQUESTS , OBSERVE_COMPLETED_REQUESTS , OBSERVE_ONGOING_REQUESTS , OBSERVE_OPEN_REQUESTS , OBSERVE_REMOVED_REQUESTS , RequestState , RESET_SET , SET , SET_TEMP_REQUEST , } from './types' ; const initialSetActionState = { loading : false , success : false , error : undefined , modalState : false , } ; const initialRequestsState = { loading : false , observerReceivedFirstUpdate : false , data : undefined , error : undefined , } ; const initialSyncRequestsState = { loading : false , data : undefined , error : undefined , } ; const initialState = { syncOpenRequestsState : initialSyncRequestsState , syncAcceptedRequestsState : initialSyncRequestsState , syncOngoingRequestsState : initialSyncRequestsState , syncArchivedRequestsState : initialSyncRequestsState , syncFinishedRequestsState : initialSyncRequestsState , openRequests : initialRequestsState , ongoingRequests : initialRequestsState , completedRequests : initialRequestsState , cancelledRequests : initialRequestsState , removedRequests : initialRequestsState , setAction : initialSetActionState , newRequestTemp : undefined , } ; const requestStatusMapper = { [ RequestStatus . pending ] : 'openRequests' , [ RequestStatus . ongoing ] : 'ongoingRequests' , [ RequestStatus . completed ] : 'completedRequests' , [ RequestStatus . cancelled ] : 'cancelledRequests' , [ RequestStatus . removed ] : 'removedRequests' , } ; const updateRequestState = ( state , payload ) => { state [ requestStatusMapper [ payload . requestStatus ] ] . data = payload . snap . docs . reduce ( ( acc , doc ) => ( { ... acc , [ doc . id ] : doc . data ( ) , } ) , { } ) ; state [ requestStatusMapper [ payload . requestStatus ] ] . loading = false ; state [ requestStatusMapper [ payload . requestStatus ] ] . observerReceivedFirstUpdate = true ; } ; export default createReducer < RequestState > ( { [ GET_OPEN . PENDING ] : ( state ) => { state . syncOpenRequestsState . loading = true ; state . syncOpenRequestsState . data = undefined ; } , [ GET_OPEN . COMPLETED ] : ( state , { payload , } ) => { state . syncOpenRequestsState . loading = false ; state . syncOpenRequestsState . error = undefined ; const mappedData = Object . keys ( payload . data . data ) . reduce ( ( acc , key ) => ( { ... acc , [ key ] : RequestWithOffersAndTimeline . factory ( payload . data . data [ key ] ) , } ) , { } ) ; state . syncOpenRequestsState . data = mappedData ; } , [ GET_OPEN . REJECTED ] : ( state , { payload } ) => { state . syncOpenRequestsState . data = undefined ; state . syncOpenRequestsState . loading = false ; state . syncOpenRequestsState . error = payload ; } , [ GET_ACCEPTED . PENDING ] : ( state ) => { state . syncAcceptedRequestsState . loading = true ; state . syncAcceptedRequestsState . data = undefined ; } , [ GET_ACCEPTED . COMPLETED ] : ( state , { payload , } ) => { state . syncAcceptedRequestsState . loading = false ; state . syncAcceptedRequestsState . error = undefined ; state . syncAcceptedRequestsState . data = Object . keys ( payload . data . data ) . reduce ( ( acc , key ) => ( { ... acc , [ key ] : RequestWithOffersAndTimeline . factory ( payload . data . data [ key ] ) , } ) , { } ) ; } , [ GET_ACCEPTED . REJECTED ] : ( state , { payload } ) => { state . syncAcceptedRequestsState . data = undefined ; state . syncAcceptedRequestsState . loading = false ; state . syncAcceptedRequestsState . error = payload ; } , [ GET_ARCHIVED . PENDING ] : ( state ) => { state . syncArchivedRequestsState . loading = true ; state . syncArchivedRequestsState . data = undefined ; } , [ GET_ARCHIVED . COMPLETED ] : ( state , { payload , } ) => { state . syncArchivedRequestsState . loading = false ; state . syncArchivedRequestsState . error = undefined ; state . syncArchivedRequestsState . data = Object . keys ( payload . data . data ) . reduce ( ( acc , key ) => ( { ... acc , [ key ] : RequestWithOffersAndTimeline . factory ( payload . data . data [ key ] ) , } ) , { } ) ; } , [ GET_ARCHIVED . REJECTED ] : ( state , { payload } ) => { state . syncArchivedRequestsState . data = undefined ; state . syncArchivedRequestsState . loading = false ; state . syncArchivedRequestsState . error = payload ; } , [ GET_FINISHED . PENDING ] : ( state ) => { state . syncFinishedRequestsState . loading = true ; state . syncFinishedRequestsState . data = undefined ; } , [ GET_FINISHED . COMPLETED ] : ( state , { payload , } ) => { state . syncFinishedRequestsState . loading = false ; state . syncFinishedRequestsState . error = undefined ; state . syncFinishedRequestsState . data = Object . keys ( payload . data . data ) . reduce ( ( acc , key ) => ( { ... acc , [ key ] : RequestWithOffersAndTimeline . factory ( payload . data . data [ key ] ) , } ) , { } ) ; } , [ GET_FINISHED . REJECTED ] : ( state , { payload } ) => { state . syncFinishedRequestsState . data = undefined ; state . syncFinishedRequestsState . loading = false ; state . syncFinishedRequestsState . error = payload ; } , [ GET_ONGOING . PENDING ] : ( state ) => { state . syncOngoingRequestsState . loading = true ; state . syncOngoingRequestsState . data = undefined ; } , [ GET_ONGOING . COMPLETED ] : ( state , { payload , } ) => { state . syncOngoingRequestsState . loading = false ; state . syncOngoingRequestsState . error = undefined ; state . syncOngoingRequestsState . data = Object . keys ( payload . data . data ) . reduce ( ( acc , key ) => ( { ... acc , [ key ] : RequestWithOffersAndTimeline . factory ( payload . data . data [ key ] ) , } ) , { } ) ; } , [ GET_ONGOING . REJECTED ] : ( state , { payload } ) => { state . syncOngoingRequestsState . data = undefined ; state . syncOngoingRequestsState . loading = false ; state . syncOngoingRequestsState . error = payload ; } , [ SET . PENDING ] : ( state ) => { state . setAction . loading = true ; state . setAction . error = undefined ; } , [ SET . COMPLETED ] : ( state ) => { state . setAction . error = undefined ; state . setAction . loading = false ; state . setAction . success = true ; state . newRequestTemp = undefined ; } , [ SET . REJECTED ] : ( state , { payload } ) => { state . setAction . loading = false ; state . setAction . error = payload ; state . setAction . success = false ; state . newRequestTemp = undefined ; } , [ SET_TEMP_REQUEST ] : ( state , { payload , } ) => { state . newRequestTemp = payload ; } , [ RESET_SET ] : ( state ) => { state . setAction . loading = false ; state . setAction . success = false ; state . syncOpenRequestsState . data = undefined ; state . syncOpenRequestsState . loading = false ; state . syncOngoingRequestsState . data = undefined ; state . syncOngoingRequestsState . loading = false ; state . syncAcceptedRequestsState . data = undefined ; state . syncAcceptedRequestsState . loading = false ; state . syncFinishedRequestsState . data = undefined ; state . syncFinishedRequestsState . loading = false ; state . syncArchivedRequestsState . data = undefined ; state . syncArchivedRequestsState . loading = false ; } , [ CHANGE_MODAL ] : ( state , { payload } ) => { state . setAction . modalState = payload ; if ( ! payload ) { state . setAction . success = false ; } } , [ OBSERVE_OPEN_REQUESTS . SUBSCRIBE ] : ( state ) => { state . openRequests . loading = true ; } , [ OBSERVE_OPEN_REQUESTS . UPDATED ] : ( state , { payload , } ) => { state . openRequests . data = payload . docs . reduce ( ( acc , doc ) => ( { ... acc , [ doc . id ] : doc . data ( ) , } ) , { } ) ; state . openRequests . loading = false ; state . openRequests . observerReceivedFirstUpdate = true ; } , [ OBSERVE_OPEN_REQUESTS . ERROR ] : ( state , { payload } ) => { state . openRequests . error = payload ; state . openRequests . loading = false ; } , [ OBSERVE_ONGOING_REQUESTS . SUBSCRIBE ] : ( state ) => { state . openRequests . loading = true ; } , [ OBSERVE_ONGOING_REQUESTS . UPDATED ] : ( state , { payload , } ) => updateRequestState ( state , payload ) , [ OBSERVE_COMPLETED_REQUESTS . SUBSCRIBE ] : ( state ) => { state . completedRequests . loading = true ; } , [ OBSERVE_COMPLETED_REQUESTS . UPDATED ] : ( state , { payload , } ) => updateRequestState ( state , payload ) , [ OBSERVE_CANCELLED_REQUESTS . SUBSCRIBE ] : ( state ) => { state . cancelledRequests . loading = true ; } , [ OBSERVE_CANCELLED_REQUESTS . UPDATED ] : ( state , { payload , } ) => updateRequestState ( state , payload ) , [ OBSERVE_REMOVED_REQUESTS . SUBSCRIBE ] : ( state ) => { state . cancelledRequests . loading = true ; } , [ OBSERVE_REMOVED_REQUESTS . UPDATED ] : ( state , { payload , } ) => updateRequestState ( state , payload ) , } , initialState ) ;