Skip to content

Commit

Permalink
ch: remove extra axios file
Browse files Browse the repository at this point in the history
  • Loading branch information
khalifan-kfan committed May 23, 2024
1 parent 014fcc6 commit 3979fe0
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 52 deletions.
6 changes: 3 additions & 3 deletions src/apis/apis.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import axios from "../axios";
import activityLoggerAxios from "./userActivityLoggerAxios";
import axios,{userActivityLoggerAxios} from "../axios";


export const handlePostRequestWithDataObject = (data, endpoint) => {
return new Promise((resolve, reject) => {
Expand Down Expand Up @@ -43,7 +43,7 @@ export const handleGetRequest = (endpoint) => {

export const handleUserActivitiesGetRequest = (endpoint) => {
return new Promise((resolve, reject) => {
activityLoggerAxios
userActivityLoggerAxios
.get(endpoint)
.then((response) => {
resolve(response);
Expand Down
28 changes: 0 additions & 28 deletions src/apis/userActivityLoggerAxios.js

This file was deleted.

53 changes: 34 additions & 19 deletions src/axios.js
Original file line number Diff line number Diff line change
@@ -1,28 +1,43 @@
import axios from "axios";
import { API_BASE_URL } from "./config";
import { API_BASE_URL, ACTIVITY_LOGS_API_URL } from "./config";

// Create activity axios instance
const userActivityLoggerAxios = axios.create({
baseURL: ACTIVITY_LOGS_API_URL,
});

const instance = axios.create({
baseURL: API_BASE_URL,
});

instance.defaults.headers.Authorization = `Bearer ${localStorage.getItem(
"token"
)}`;

instance.interceptors.response.use(
(response) => response,
(error) => {
let e = error;
// this checks for when a token is not verified and logs you out
if (error.response.status === 401 || error.response.status === 422) {
localStorage.clear();
window.location.href = "/";
} else if(error.response.status === 502){
e = "Your request too long possible Server Error."
window.location.href = "/projects";
}
return Promise.reject(e);
// Set default headers
const token = localStorage.getItem("token");
instance.defaults.headers.Authorization = `Bearer ${token}`;
userActivityLoggerAxios.defaults.headers.Authorization = `Bearer ${token}`;

// Define response interceptor function
const responseInterceptor = (response) => response;

const errorInterceptor = (error) => {
let e = error;
// this checks for when a token is not verified and logs you out
if (error.response.status === 401 || error.response.status === 422) {
localStorage.clear();
window.location.href = "/";
} else if (error.response.status === 502) {
e = "Your request took too long, possibly a Server Error.";
window.location.href = "/projects";
}
return Promise.reject(e);
};

// Add response interceptors to both instances
instance.interceptors.response.use(responseInterceptor, errorInterceptor);

userActivityLoggerAxios.interceptors.response.use(
responseInterceptor,
errorInterceptor
);

export default instance;
export { instance, userActivityLoggerAxios };
export default instance;
4 changes: 2 additions & 2 deletions src/redux/actions/getUserActivities.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import activityLoggerAxiosInstance from "../../apis/userActivityLoggerAxios";
import {userActivityLoggerAxios} from "../../axios";
import {
GETTING_USER_ACTIVITIES,
USER_ACTIVITIES_SUCCESS,
Expand Down Expand Up @@ -33,7 +33,7 @@ const getUserActivities = (qeuryParams, currentPage) => (dispatch) => {
link = `/activities?page=${currentPage}`;
}
console.log(ACTIVITY_LOGS_API_URL)
return activityLoggerAxiosInstance
return userActivityLoggerAxios
.get(link)
.then((response) => {
dispatch(userActivitiesSuccess(response));
Expand Down

0 comments on commit 3979fe0

Please sign in to comment.