import axios from "axios" ; import api from "./api" ; import * as Notification from "../Utils/Notifications.js" ; import querystring from "querystring" ; const requestMap = api ; export const actions = { FETCH_REQUEST : "FETCH_REQUEST" , FETCH_REQUEST_SUCCESS : "FETCH_REQUEST_SUCCESS" , FETCH_REQUEST_ERROR : "FETCH_REQUEST_ERROR" , SET_DATA : "SET_DATA" , } ; const isRunning = { } ; export const setStoreData = ( key , value ) => { return { type : actions . SET_DATA , key , value , } ; } ; export const fetchDataRequest = ( key ) => { return { type : actions . FETCH_REQUEST , key , } ; } ; export const fetchDataRequestError = ( key , error ) => { return { type : actions . FETCH_REQUEST_ERROR , key , error , } ; } ; export const fetchResponseSuccess = ( key , data ) => { return { type : actions . FETCH_REQUEST_SUCCESS , key , data , } ; } ; export const fireRequest = ( key , path = [ ] , params = { } , pathParam ? , altKey ? ) => { return ( dispatch ) => { if ( isRunning [ altKey ? altKey : key ] ) { isRunning [ altKey ? altKey : key ] . cancel ( ) ; } isRunning [ altKey ? altKey : key ] = axios . CancelToken . source ( ) ; const request = Object . assign ( { } , requestMap [ key ] ) ; if ( path . length > 0 ) { request . path += "/" + path . join ( "/" ) ; } if ( request . method === undefined || request . method === "GET" ) { request . method = "GET" ; const qs = querystring . stringify ( params ) ; if ( qs !== "" ) { request . path += `?${ qs }` ; } } if ( pathParam ) { Object . keys ( pathParam ) . forEach ( ( param ) => { request . path = request . path . replace ( `{${ param }}` , pathParam [ param ] ) ; } ) ; } const config = { headers : { } , } ; if ( ! request . noAuth && localStorage . getItem ( "care_access_token" ) ) { config . headers [ "Authorization" ] = "Bearer " + localStorage . getItem ( "care_access_token" ) ; } const axiosApiCall = axios . create ( config ) ; dispatch ( fetchDataRequest ( key ) ) ; return axiosApiCall [ request . method . toLowerCase ( ) ] ( request . path , { ... params , cancelToken : isRunning [ altKey ? altKey : key ] . token , } ) . then ( ( response ) => { dispatch ( fetchResponseSuccess ( key , response . data ) ) ; return response ; } ) . catch ( ( error ) => { dispatch ( fetchDataRequestError ( key , error ) ) ; if ( error . response ) { if ( error . response . status === 400 && key === "searchPatient" ) { return ; } if ( error . response . status === 404 && key === "deleteUser" ) { Notification . Error ( { msg : "Permission denied!" , } ) ; return ; } if ( error . response . status === 403 && key === "currentUser" ) { if ( localStorage . getItem ( "care_access_token" ) ) { localStorage . removeItem ( "care_access_token" ) ; } return ; } if ( error . response . status === 400 || error . response . status === 406 ) { Notification . BadRequest ( { errs : error . response . data , } ) ; return error . response ; } if ( error . response . status > 400 && error . response . status < 500 ) { if ( error . response . status === 429 ) { return error . response ; } else if ( error . response . data && error . response . data . detail ) { Notification . Error ( { msg : error . response . data . detail , } ) ; } else { Notification . Error ( { msg : "Something went wrong...!" , } ) ; } return ; } if ( error . response . status >= 500 && error . response . status <= 599 ) { Notification . Error ( { msg : "Something went wrong...!" , } ) ; return ; } } } ) ; } ; } ; export const fireRequestV2 = ( key , path = [ ] , params = { } , successCallback = ( ) => { } , errorCallback = ( ) => { } , pathParam ? , altKey ? ) => { if ( isRunning [ altKey ? altKey : key ] ) { isRunning [ altKey ? altKey : key ] . cancel ( ) ; } isRunning [ altKey ? altKey : key ] = axios . CancelToken . source ( ) ; const request = Object . assign ( { } , requestMap [ key ] ) ; if ( path . length > 0 ) { request . path += "/" + path . join ( "/" ) ; } if ( request . method === undefined || request . method === "GET" ) { request . method = "GET" ; const qs = querystring . stringify ( params ) ; if ( qs !== "" ) { request . path += `?${ qs }` ; } } if ( pathParam ) { Object . keys ( pathParam ) . forEach ( ( param ) => { request . path = request . path . replace ( `{${ param }}` , pathParam [ param ] ) ; } ) ; } const config = { headers : { } , } ; if ( ! request . noAuth && localStorage . getItem ( "care_access_token" ) ) { config . headers [ "Authorization" ] = "Bearer " + localStorage . getItem ( "care_access_token" ) ; } const axiosApiCall = axios . create ( config ) ; fetchDataRequest ( key ) ; return axiosApiCall [ request . method . toLowerCase ( ) ] ( request . path , { ... params , cancelToken : isRunning [ altKey ? altKey : key ] . token , } ) . then ( ( response ) => { successCallback ( response . data ) ; } ) . catch ( ( error ) => { errorCallback ( error ) ; if ( error . response ) { if ( error . response . status === 400 && key === "searchPatient" ) { return ; } if ( error . response . status === 404 && key === "deleteUser" ) { Notification . Error ( { msg : "Permission denied!" , } ) ; } if ( error . response . status === 403 && key === "currentUser" ) { if ( localStorage . getItem ( "care_access_token" ) ) { localStorage . removeItem ( "care_access_token" ) ; } } if ( error . response . status === 400 || error . response . status === 406 ) { Notification . BadRequest ( { errs : error . response . data , } ) ; } if ( error . response . status > 400 && error . response . status < 500 ) { if ( error . response . status === 429 ) { return error . response ; } else if ( error . response . data && error . response . data . detail ) { Notification . Error ( { msg : error . response . data . detail , } ) ; } else { Notification . Error ( { msg : "Something went wrong...!" , } ) ; } } if ( error . response . status >= 500 && error . response . status <= 599 ) { Notification . Error ( { msg : "Something went wrong...!" , } ) ; return ; } } } ) ; } ; export const fireRequestForFiles = ( key , path = [ ] , params = { } , pathParam ? , altKey ? ) => { return ( dispatch ) => { if ( isRunning [ altKey ? altKey : key ] ) { isRunning [ altKey ? altKey : key ] . cancel ( ) ; } isRunning [ altKey ? altKey : key ] = axios . CancelToken . source ( ) ; const request = Object . assign ( { } , requestMap [ key ] ) ; if ( path . length > 0 ) { request . path += "/" + path . join ( "/" ) ; } if ( request . method === undefined || request . method === "GET" ) { request . method = "GET" ; const qs = querystring . stringify ( params ) ; if ( qs !== "" ) { request . path += `?${ qs }` ; } } if ( pathParam ) { Object . keys ( pathParam ) . forEach ( ( param ) => { request . path = request . path . replace ( `{${ param }}` , pathParam [ param ] ) ; } ) ; } const config = { headers : { "Content-type" : "application/pdf" , "Content-disposition" : "inline" , } , } ; if ( ! request . noAuth && localStorage . getItem ( "care_access_token" ) ) { config . headers [ "Authorization" ] = "Bearer " + localStorage . getItem ( "care_access_token" ) ; } const axiosApiCall = axios . create ( config ) ; dispatch ( fetchDataRequest ( key ) ) ; return axiosApiCall [ request . method . toLowerCase ( ) ] ( request . path , { ... params , cancelToken : isRunning [ altKey ? altKey : key ] . token , } ) . then ( ( response ) => { dispatch ( fetchResponseSuccess ( key , response . data ) ) ; return response ; } ) . catch ( ( error ) => { dispatch ( fetchDataRequestError ( key , error ) ) ; if ( error . response ) { if ( error . response . status === 400 && key === "searchPatient" ) { return ; } if ( error . response . status === 403 && key === "currentUser" ) { if ( localStorage . getItem ( "care_access_token" ) ) { localStorage . removeItem ( "care_access_token" ) ; } return ; } if ( error . response . status === 400 || error . response . status === 406 ) { Notification . BadRequest ( { errs : error . response . data , } ) ; return error . response ; } if ( error . response . status > 400 && error . response . status < 500 ) { if ( error . response . status === 429 ) { return error . response ; } else if ( error . response . data && error . response . data . detail ) { Notification . Error ( { msg : error . response . data . detail , } ) ; } else { Notification . Error ( { msg : "Something went wrong...!" , } ) ; } return ; } if ( error . response . status >= 500 && error . response . status <= 599 ) { Notification . Error ( { msg : "Something went wrong...!" , } ) ; return ; } } } ) ; } ; } ;