import { IRequest , Request , RequestStatus } from 'src/models/requests' ; import { createUserRequest , getAcceptedRequest as getAcceptedRequestFunc , getArchivedRequest as getArchivedRequestFunc , getFinishedRequest as getFinishedRequestFunc , getOngoingRequest as getOngoingRequestFunc , getOpenRequest as getOpenRequestFunc , observeNonOpenRequests as observeNonOpenRequestsFunc , observeOpenRequests as observeOpenRequestsFunc , setUserRequest , } from './functions' ; import { CHANGE_MODAL , GET_ACCEPTED , GET_ARCHIVED , GET_FINISHED , GET_ONGOING , GET_OPEN , IgetNonOpenRequests , IgetOpenRequests , OBSERVE_CANCELLED_REQUESTS , OBSERVE_COMPLETED_REQUESTS , OBSERVE_ONGOING_REQUESTS , OBSERVE_OPEN_REQUESTS , OBSERVE_REMOVED_REQUESTS , RESET_SET , SET , SET_TEMP_REQUEST , } from './types' ; const requestStatusMapper = { [ RequestStatus . ongoing ] : OBSERVE_ONGOING_REQUESTS , [ RequestStatus . completed ] : OBSERVE_COMPLETED_REQUESTS , [ RequestStatus . cancelled ] : OBSERVE_CANCELLED_REQUESTS , [ RequestStatus . removed ] : OBSERVE_REMOVED_REQUESTS , } ; export const getOpenRequests = ( payload ) => ( dispatch ) => dispatch ( { type : GET_OPEN , firebase : getOpenRequestFunc , payload , } ) ; export const getAcceptedRequests = ( payload ) => ( dispatch ) => dispatch ( { type : GET_ACCEPTED , firebase : getAcceptedRequestFunc , payload , } ) ; export const getOngoingRequests = ( payload ) => ( dispatch ) => dispatch ( { type : GET_ONGOING , firebase : getOngoingRequestFunc , payload , } ) ; export const getFinishedRequests = ( payload ) => ( dispatch ) => dispatch ( { type : GET_FINISHED , firebase : getFinishedRequestFunc , payload , } ) ; export const getArchivedRequests = ( payload ) => ( dispatch ) => dispatch ( { type : GET_ARCHIVED , firebase : getArchivedRequestFunc , payload , } ) ; export const observeOpenRequests = ( dispatch , payload ) : ( ( ) => void ) => { dispatch ( { type : OBSERVE_OPEN_REQUESTS , observer : observeOpenRequestsFunc , payload , } ) ; return ( ) => dispatch ( { type : OBSERVE_OPEN_REQUESTS . UNSUBSCRIBE , observerName : OBSERVE_OPEN_REQUESTS , } ) ; } ; export const observeNonOpenRequests = ( dispatch , payload ) : any => { dispatch ( { type : requestStatusMapper [ payload . requestStatus ] , observer : observeNonOpenRequestsFunc , payload , } ) ; return ( ) => dispatch ( { type : requestStatusMapper [ payload . requestStatus ] . UNSUBSCRIBE , observerName : requestStatusMapper [ payload . requestStatus ] , } ) ; } ; export const setRequest = ( payload , requestId ? , phoneNumber ? ) => ( dispatch ) => { const requestPayload = Request . factory ( { ... payload , } ) ; dispatch ( { type : phoneNumber ? SET : SET_TEMP_REQUEST , payload : { requestPayload , requestId , } , firebase : phoneNumber ? requestId ? setUserRequest : createUserRequest : null , } ) ; } ; export const resetSetRequestState = ( ) => ( dispatch ) => dispatch ( { type : RESET_SET , payload : true , } ) ; export const changeModal = state => ( dispatch ) => dispatch ( { type : CHANGE_MODAL , payload : state , } ) ;