Axios & Saga for API
Mock API calls and setup Saga middle-tier
Mock Calls
DashboardKit uses fack/mock data to render some pages and actions. Mocking has been achieved with the help of Axios. It helps users to do minimal changes to load live data. Users just need to change the mock API URL to the Live service URL.
Setup Axios
Axios has been added request interceptor in the file ..src\utils\axios.js so that every request intercepts through this.
/**
* Axios setup to use mock service
*/
import axios from 'axios';
const axiosServices = axios.create();
// interceptor for http
axiosServices.interceptors.response.use(
(response) => response,
(error) => Promise.reject((error.response && error.response.data) || 'Wrong Services')
);
export default axiosServices;
Setup axios-mock-adapter
How does data get loaded into components?
To understand this, we are going to take an example of one app panel and try to understand it. Let's say I want to load data for the leave page of SIS panel. There are following things that need to be done.
Create reducer action
Create reducer to update store
Setup saga and worker to listen to every request
Create Axios servive to be called from Saga worker
Finally, Dispatch action from component to get data
Create reducer action
Create reducer to update store after getting response back from saga
The component dispatches a plain object action to the store. We'll create a Saga that watches for all
FETCH_LEAVE_LISTactions and triggers an Axios API call to fetch the user data. Once a response received, we will dispatch another action using 'put' that will update the store
We did set up saga, now we will define Axios service so that it returns data.
Axios call will happen and returns data.
Now everything has been setup, you can dispatch action to get data from component.
To know more about redux-saga and Axios, refer below:
Last updated
Was this helpful?