From 700613b46e91b4d9e7ccb9fb80c0a4fe4fba066e Mon Sep 17 00:00:00 2001 From: manisha Date: May 27 2021 07:08:52 +0000 Subject: added sendRequest method for sending api requests deleted package-lock --- diff --git a/package.json b/package.json index 28db871..c10986e 100644 --- a/package.json +++ b/package.json @@ -9,6 +9,7 @@ "dateformat": "^3.0.3", "nanoid": "^3.1.22", "ramda": "^0.27.1", + "query-string": "^7.0.0", "react": "^16.5.2", "react-dom": "^16.5.2", "react-redux": "^7.2.1", diff --git a/src/actions/reduxActions.js b/src/actions/reduxActions.js index a564b51..bd40230 100644 --- a/src/actions/reduxActions.js +++ b/src/actions/reduxActions.js @@ -4,7 +4,8 @@ import { MAX_RETRY_LIMIT, FIRST_RETRY_GAP, } from "../constants/ProjectConstants" -import { endpoints } from "../constants/endpoints" +import { landingPageApi } from "../api/landingPage" +import { loadWizardDataApi } from "../api/wizard" export const loadDataResp = (payload) => ({ type: ActionTypes.LOAD_DATA_RESP, @@ -19,8 +20,7 @@ export const loadData = payload: payload, }) dispatch(updateLandingPageApiCallStatus(API_CALL_STATUS_VALUES.STARTED)) - fetch(endpoints.landingPage) - .then((blob) => blob.json()) + landingPageApi() .then((data) => { dispatch(loadDataResp(data)) dispatch(updateLandingPageApiCallStatus(API_CALL_STATUS_VALUES.FULFILLED)) @@ -52,8 +52,7 @@ export const loadWizardData = payload: payload, }) dispatch(updateWizardApiCallStatus(API_CALL_STATUS_VALUES.STARTED)) - fetch(endpoints.allActions) - .then((blob) => blob.json()) + loadWizardDataApi() .then((data) => { dispatch(loadWizardDataResp(data)) dispatch(updateWizardApiCallStatus(API_CALL_STATUS_VALUES.FULFILLED)) diff --git a/src/api/landingPage.js b/src/api/landingPage.js new file mode 100644 index 0000000..77daa04 --- /dev/null +++ b/src/api/landingPage.js @@ -0,0 +1,9 @@ +import { sendRequest } from "../utils/sendRequest" +import { endpoints } from "../constants/endpoints" + +export const landingPageApi = () => { + const url = endpoints.landingPage + return sendRequest({ + url, + }) +} diff --git a/src/api/wizard.js b/src/api/wizard.js new file mode 100644 index 0000000..85c49d1 --- /dev/null +++ b/src/api/wizard.js @@ -0,0 +1,9 @@ +import { sendRequest } from "../utils/sendRequest" +import { endpoints } from "../constants/endpoints" + +export const loadWizardDataApi = () => { + const url = endpoints.allActions + return sendRequest({ + url, + }) +} diff --git a/src/utils/sendRequest.js b/src/utils/sendRequest.js new file mode 100644 index 0000000..ec913ac --- /dev/null +++ b/src/utils/sendRequest.js @@ -0,0 +1,32 @@ +import { stringify } from 'query-string'; + +export const sendRequest = ({ + url, + method = "GET", + body, + queryParams, +}) => { + const options = { + method: method, + body: JSON.stringify(body) + }; + + if (queryParams) { + url = `${url}?${stringify(queryParams)}`; + } + + return fetch(url, options).then(res => { + if (res.ok) { + return res.json(); + } else { + return res.json().then((json) => { + return Promise.reject({ + status: res.status, + ok: false, + message: json.message, + body: json + }); + }); + } + }); +}; \ No newline at end of file