API Integration
Upzet react having fake-backend setup already.
you will find all files related to api
integrations in the
src/helpers folder.
By default we have provided fake-backend but you can remove it and update with your custom API by doing the following changes in the
src/helpers.
-
src/helpers/api_helper.jsfile contains axios setup to call server API(s) including get, put, post, delete, etc methods, interceptors & token set methods. -
src/helpers/fakebackend_helper.jsfile contain all API's call functions. -
src/helpers/url_helper.jsfile contain all module's API's url.
src/helpers/fakeBackend.js, and remove the code related to Fake-Backend from app.js file. you just need to update API's URL of the related module in the src/helpers/url_helper.js file,
that's it!
How to Integrate custom API?
Please follow the below steps to make the custom API working.
-
Let's assume that our API's URL is
"https://jsonplaceholder.typicode.com/posts". First we have to add this URL in
/src/helpers/url_helper.jsfile... export const GET_DEMO_DATA = "https://jsonplaceholder.typicode.com/posts";
-
Whatever methods you want to use, import it
import { del,get,post,put } from "./api_helper";and add below function insrc/helpers/fakebackend_helper.jsfile. We have created new function getDemoData and exported it so it can be used in another files.import { del, get, post, put } from "./api_helper"; import * as url from "./url_helper"; const getDemoData = () => get(url.GET_DEMO_DATA); export { getDemoData };
Create a folder named with your module in src/store folder i.e. demo module then it should be src/store/demo then
create actions.js, saga.js, reducer.js & actionTypes.js files and follow the pattern of other modules which are added in this template.
Also do not forget to export it in main files (action.js, reducer.js and saga.js ) of src/store folder.
export const GET_DEMO_DATA = "GET_DEMO_DATA"; export const GET_DEMO_DATA_SUCCESS = "GET_DEMO_DATA_SUCCESS"; export const GET_DEMO_DATA_FAIL = "GET_DEMO_DATA_FAIL";
actionTypes.js file.
import {
GET_DEMO_DATA,
GET_DEMO_DATA_SUCCESS,
GET_DEMO_DATA_FAIL,
} from "./actionTypes";
export const getDemoData = () => ({
type: GET_DEMO_DATA,
});
export const getDemoDataSuccess = data => ({
type: GET_DEMO_DATA_SUCCESS,
payload: data,
});
export const getDemoDataFail = error => ({
type: GET_DEMO_DATA_FAIL,
payload: error,
});
import {
GET_DEMO_DATA_SUCCESS,
GET_DEMO_DATA_FAIL,
} from "./actionTypes";
const INIT_STATE = {
demoData: [],
};
const Demo = (state = INIT_STATE, action) => {
switch (action.type) {
case GET_DEMO_DATA_SUCCESS:
return {
...state,
demoData: action.payload,
};
case GET_DEMO_DATA_FAIL:
return {
...state,
error: action.payload,
};
default:
return state;
}
};
export default Demo;
import { takeEvery, put, call,all,fork } from "redux-saga/effects";
// Login Redux States
import {
GET_DEMO_DATA,
} from "./actionTypes"
import {
getDemoDataSuccess,
getDemoDataFail,
} from "./actions"
import { getDemoData } from "../../helpers/fakebackend_helper";
function* fetchDemoData() {
try {
const response = yield call(getDemoData)
yield put(getDemoDataSuccess(response))
} catch (error) {
yield put(getDemoDataFail(error))
}
}
export function* watchFetchDemoData() {
yield takeEvery(GET_DEMO_DATA, fetchDemoData);
}
function* demoSaga() {
yield all([fork(watchFetchDemoData)]);
}
export default demoSaga;
import React, { useEffect } from "react";
//redux
import { useSelector, useDispatch } from "react-redux";
// actions
import { getDemoData } from "../store/actions";
const Demo = (props) => {
const dispatch = useDispatch();
const { demoData } = useSelector(state => ({
demoData: state.Demo.demoData,
}));
/*
get data
*/
useEffect(() => {
dispatch(getDemoData());
}, [dispatch]);
// your API's response data will be in events variable.
console.log(demoData);
return (
<div>
</div>
);
};
export default Demo;