-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #25 from atlp-rwanda/ft-Intercept-Axios-#187790848
#187790848 Set up Axios interceptors for Network errors, 401 Errors and redirections
- Loading branch information
Showing
9 changed files
with
263 additions
and
48 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
import { configureStore } from "@reduxjs/toolkit"; | ||
|
||
import productsReducer, { | ||
fetchProducts, | ||
deleteProduct, | ||
handleSearchProduct, | ||
} from "../redux/reducers/productsSlice"; | ||
import api from "../redux/api/api"; | ||
|
||
jest.mock("../redux/api/api"); | ||
|
||
describe("products slice", () => { | ||
let store; | ||
|
||
beforeEach(() => { | ||
store = configureStore({ | ||
reducer: { | ||
products: productsReducer, | ||
}, | ||
}); | ||
}); | ||
|
||
it("should handle fetchProducts", async () => { | ||
const mockProducts = [ | ||
{ id: 1, name: "Product 1", price: 100 }, | ||
{ id: 2, name: "Product 2", price: 200 }, | ||
]; | ||
|
||
(api.get as jest.Mock).mockResolvedValueOnce({ | ||
data: { products: mockProducts }, | ||
}); | ||
|
||
await store.dispatch(fetchProducts()); | ||
|
||
expect(store.getState().products.data).toEqual(mockProducts); | ||
expect(store.getState().products.loading).toBe(false); | ||
}); | ||
|
||
it("should handle deleteProduct", async () => { | ||
const productId = 1; | ||
|
||
(api.delete as jest.Mock).mockResolvedValueOnce({}); | ||
|
||
await store.dispatch(deleteProduct(productId)); | ||
|
||
expect(store.getState().products.data).not.toContainEqual({ | ||
id: productId, | ||
}); | ||
}); | ||
|
||
it("should handle handleSearchProduct", async () => { | ||
const mockProducts = [ | ||
{ id: 1, name: "Product 1", price: 100 }, | ||
{ id: 2, name: "Product 2", price: 200 }, | ||
]; | ||
|
||
(api.get as jest.Mock).mockResolvedValueOnce({ data: mockProducts }); | ||
|
||
await store.dispatch(handleSearchProduct({ name: "Product" })); | ||
|
||
expect(store.getState().products.data).toEqual(mockProducts); | ||
expect(store.getState().products.loading).toBe(false); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
import React, { useState, useEffect } from "react"; | ||
import { set } from "react-hook-form"; | ||
|
||
import api from "../../../redux/api/action"; | ||
// import { useSelector, useDispatch } from "react-redux"; | ||
|
||
// import { clearNetworkError } from "../../../redux/reducers/networkErrorSlice"; | ||
|
||
const Popup = () => { | ||
const [showPopup, setShowPopup] = useState(false); | ||
useEffect(() => { | ||
const response = api | ||
.get("/") | ||
.then(() => { | ||
setShowPopup(false); | ||
}) | ||
.catch(() => { | ||
setShowPopup(true); | ||
}); | ||
}, []); | ||
|
||
if (!showPopup) return null; | ||
|
||
return ( | ||
<div className="fixed inset-0 z-40 flex items-center justify-center bg-[#D0D0D0] bg-opacity-50"> | ||
<div className="relative bg-white rounded-lg p-10 w-[90%] md:w-[65%] lg:w-[55%] xl:w-[50%] duration-75 animate-fadeIn"> | ||
<p> | ||
📵 | ||
<b>Network Error </b> | ||
<br /> | ||
Please check your internet connection. | ||
</p> | ||
<button | ||
onClick={() => setShowPopup(false)} | ||
className="mt-10 bg-transparent text-primary border border-[#DB4444] px-4 py-2 rounded" | ||
> | ||
Close | ||
</button> | ||
</div> | ||
</div> | ||
); | ||
}; | ||
export default Popup; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
import axios from "axios"; | ||
|
||
const api = axios.create({ | ||
baseURL: process.env.VITE_BASE_URL, | ||
headers: { | ||
"Content-Type": "application/json", | ||
}, | ||
}); | ||
export default api; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,41 @@ | ||
import axios from "axios"; | ||
import { set } from "react-hook-form"; | ||
import { useState, useEffect } from "react"; | ||
import { toast } from "react-toastify"; | ||
|
||
const api = axios.create({ | ||
baseURL: process.env.VITE_BASE_URL, | ||
headers: { | ||
"Content-Type": "application/json", | ||
import store from "../store"; | ||
|
||
import api from "./action"; | ||
|
||
let navigateFunction = null; | ||
|
||
export const setNavigateFunction = (navigate) => { | ||
navigateFunction = navigate; | ||
}; | ||
const redirectToLogin = (navigate) => { | ||
setTimeout(() => { | ||
navigate("/login"); | ||
}, 2000); | ||
}; | ||
|
||
api.interceptors.response.use( | ||
(response) => response, | ||
(error) => { | ||
const excludeRoute = "/"; | ||
|
||
if ( | ||
error.response | ||
&& error.response.status === 401 | ||
&& window.location.pathname !== excludeRoute | ||
) { | ||
if (navigateFunction) { | ||
toast("Login is Required for this action \n Redirecting to Login \n "); | ||
setTimeout(() => { | ||
redirectToLogin(navigateFunction); | ||
}, 2000); | ||
} | ||
} | ||
return Promise.reject(error); | ||
}, | ||
}); | ||
); | ||
|
||
export default api; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters