import * as R from 'ramda'; import { reducerKey } from './redux-ajax.constants'; import { RequestStatus } from './redux-ajax.type'; /** * Path to the status of the requet in the store. */ const getStatusPath = (requestId: string) => [reducerKey, requestId, 'status']; /** * Check if the given request is pending. */ export const isRequestPending = (requestId: string) => R.pathEq(getStatusPath(requestId), RequestStatus.PENDING); /** * Check if the given request is successful. */ export const isRequestSuccessful = (requestId: string) => R.pathEq(getStatusPath(requestId), RequestStatus.SUCCESS); /** * Check if the given request has failed. */ export const isRequestFailed = (requestId: string) => R.pathEq(getStatusPath(requestId), RequestStatus.FAILURE); /** * Check if the given request is complete (failed or success). */ export const isRequestComplete = (requestId: string) => R.pathSatisfies( (status: RequestStatus) => [RequestStatus.SUCCESS, RequestStatus.FAILURE, RequestStatus.ABORTED].includes(status), getStatusPath(requestId) ); /** * Check if the given request has been aborted. */ export const isRequestAborted = (requestId: string) => R.pathOr(false, [reducerKey, requestId, 'signal', 'aborted']); /** * Retrieve the response of the request if there is one. */ export const getResponse = (requestId: string) => R.path([reducerKey, requestId, 'response']); /** * Check if the given request has a response. */ export const hasResponse = (requestId: string) => R.pipe(R.path([reducerKey, requestId, 'response']), R.isNil, R.not); /** * Retrieve the error of the response if there is one. */ export const getError = (requestId: string) => R.path([reducerKey, requestId, 'error']); /** * Check if the given request has an error. */ export const hasError = (requestId: string) => R.pipe(R.path([reducerKey, requestId, 'error']), R.isNil, R.not); export const getStatus = (requestId: string) => R.path(getStatusPath(requestId));