2024-07-11 11:46:48 +05:30
|
|
|
import axios from "axios";
|
|
|
|
|
import { configureStore } from "@reduxjs/toolkit";
|
|
|
|
|
import { setupListeners } from "@reduxjs/toolkit/query";
|
|
|
|
|
import { sponserMaster } from "../Services/sponser.service";
|
|
|
|
|
import { investmentType } from "../Services/investment.type.service";
|
|
|
|
|
import { exchangeRate } from "../Services/exchange.rate.service";
|
|
|
|
|
import { ioService } from "../Services/io.service";
|
|
|
|
|
import { investorDetails } from "../Services/investor.details.service";
|
|
|
|
|
import { investorTransaction } from "../Services/investor.transaction.service";
|
2024-06-20 12:09:48 +05:30
|
|
|
|
2024-07-11 11:46:48 +05:30
|
|
|
// Create an Axios instance for API calls
|
|
|
|
|
const api = axios.create({
|
|
|
|
|
baseURL: "https://api.example.com", // Replace with your API base URL
|
|
|
|
|
timeout: 10000, // Adjust timeout as needed
|
|
|
|
|
headers: {
|
|
|
|
|
"Content-Type": "application/json",
|
|
|
|
|
},
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
// Add Axios request interceptor to refresh token if expired
|
|
|
|
|
api.interceptors.request.use(
|
|
|
|
|
(config) => {
|
|
|
|
|
// Modify headers or add tokens as needed
|
|
|
|
|
const token = localStorage.getItem("accessToken");
|
|
|
|
|
if (token) {
|
|
|
|
|
config.headers.Authorization = `Bearer ${token}`;
|
|
|
|
|
}
|
|
|
|
|
return config;
|
|
|
|
|
},
|
|
|
|
|
(error) => {
|
|
|
|
|
return Promise.reject(error);
|
|
|
|
|
}
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
// Add Axios response interceptor to handle token refreshing
|
|
|
|
|
api.interceptors.response.use(
|
|
|
|
|
(response) => {
|
|
|
|
|
return response;
|
|
|
|
|
},
|
|
|
|
|
async (error) => {
|
|
|
|
|
const originalRequest = error.config;
|
|
|
|
|
|
|
|
|
|
// Example logic for handling token expiration and refreshing
|
|
|
|
|
if (
|
|
|
|
|
error.response.status === 401 &&
|
|
|
|
|
!originalRequest._retry &&
|
|
|
|
|
localStorage.getItem("refreshToken")
|
|
|
|
|
) {
|
|
|
|
|
originalRequest._retry = true;
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
const response = await api.post("/refresh_token", {
|
|
|
|
|
refreshToken: localStorage.getItem("refreshToken"),
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
if (response.status === 200) {
|
|
|
|
|
// Update tokens in local storage
|
|
|
|
|
localStorage.setItem("accessToken", response.data.accessToken);
|
|
|
|
|
localStorage.setItem(
|
|
|
|
|
"refreshToken",
|
|
|
|
|
response.data.refreshToken
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
// Retry the original request with the new tokens
|
|
|
|
|
return api(originalRequest);
|
|
|
|
|
}
|
|
|
|
|
} catch (error) {
|
|
|
|
|
console.error("Failed to refresh token:", error);
|
|
|
|
|
// Handle token refresh failure (e.g., redirect to login)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return Promise.reject(error);
|
|
|
|
|
}
|
|
|
|
|
);
|
2024-06-20 12:09:48 +05:30
|
|
|
|
|
|
|
|
export const store = configureStore({
|
|
|
|
|
reducer: {
|
2024-07-11 11:46:48 +05:30
|
|
|
[sponserMaster.reducerPath]: sponserMaster.reducer,
|
|
|
|
|
[investmentType.reducerPath]: investmentType.reducer,
|
|
|
|
|
[exchangeRate.reducerPath]: exchangeRate.reducer,
|
|
|
|
|
[ioService.reducerPath]: ioService.reducer,
|
|
|
|
|
[investorDetails.reducerPath]: investorDetails.reducer,
|
|
|
|
|
[investorTransaction.reducerPath]: investorTransaction.reducer,
|
|
|
|
|
// Add other reducers as needed
|
2024-06-20 12:09:48 +05:30
|
|
|
},
|
|
|
|
|
middleware: (getDefaultMiddleware) =>
|
2024-07-11 11:46:48 +05:30
|
|
|
getDefaultMiddleware({
|
|
|
|
|
thunk: {
|
|
|
|
|
extraArgument: api, // Pass Axios instance as extra argument
|
|
|
|
|
},
|
|
|
|
|
}),
|
2024-06-20 12:09:48 +05:30
|
|
|
});
|
|
|
|
|
|
|
|
|
|
setupListeners(store.dispatch);
|
|
|
|
|
|
2024-07-11 11:46:48 +05:30
|
|
|
export default store;
|