Added cancelling handle
This commit is contained in:
parent
73511c72ca
commit
79e3321a85
4 changed files with 23 additions and 21 deletions
|
|
@ -1,4 +1,4 @@
|
||||||
import axios, { AxiosError, AxiosInstance } from "axios";
|
import axios, { Axios, AxiosError, AxiosInstance } from "axios";
|
||||||
import { useContext, useEffect } from "react";
|
import { useContext, useEffect } from "react";
|
||||||
import { Cookies } from "react-cookie";
|
import { Cookies } from "react-cookie";
|
||||||
import { renewAccessToken } from ".";
|
import { renewAccessToken } from ".";
|
||||||
|
|
@ -83,8 +83,10 @@ function ApiInterceptor() {
|
||||||
(config) => {
|
(config) => {
|
||||||
const checkRequest = checkDuplicateRequestAndStoreRequest(config);
|
const checkRequest = checkDuplicateRequestAndStoreRequest(config);
|
||||||
|
|
||||||
|
const controller = new AbortController();
|
||||||
|
|
||||||
if (!checkRequest) {
|
if (!checkRequest) {
|
||||||
return Promise.reject("Duplicate request.");
|
controller.abort("Duplicate Request");
|
||||||
}
|
}
|
||||||
|
|
||||||
const accessToken = cookies.get("access_token_lf");
|
const accessToken = cookies.get("access_token_lf");
|
||||||
|
|
@ -92,7 +94,10 @@ function ApiInterceptor() {
|
||||||
config.headers["Authorization"] = `Bearer ${accessToken}`;
|
config.headers["Authorization"] = `Bearer ${accessToken}`;
|
||||||
}
|
}
|
||||||
|
|
||||||
return config;
|
return {
|
||||||
|
...config,
|
||||||
|
signal: controller.signal,
|
||||||
|
};
|
||||||
},
|
},
|
||||||
(error) => {
|
(error) => {
|
||||||
return Promise.reject(error);
|
return Promise.reject(error);
|
||||||
|
|
|
||||||
|
|
@ -365,13 +365,9 @@ export async function uploadFile(
|
||||||
return await api.post(`${BASE_URL_API}files/upload/${id}`, formData);
|
return await api.post(`${BASE_URL_API}files/upload/${id}`, formData);
|
||||||
}
|
}
|
||||||
|
|
||||||
export async function getProfilePictures(
|
export async function getProfilePictures(): Promise<ProfilePicturesTypeAPI | null> {
|
||||||
abortSignal,
|
|
||||||
): Promise<ProfilePicturesTypeAPI | null> {
|
|
||||||
try {
|
try {
|
||||||
const res = await api.get(`${BASE_URL_API}images/list/profile_pictures`, {
|
const res = await api.get(`${BASE_URL_API}files/profile_pictures/list`);
|
||||||
signal: abortSignal,
|
|
||||||
});
|
|
||||||
|
|
||||||
if (res.status === 200) {
|
if (res.status === 200) {
|
||||||
return res.data;
|
return res.data;
|
||||||
|
|
|
||||||
|
|
@ -1,19 +1,22 @@
|
||||||
import { reject } from "lodash";
|
import { reject } from "lodash";
|
||||||
import { PROFILE_PICTURES_GET_ERROR_ALERT } from "../../../../../../../../../constants/alerts_constants";
|
import { PROFILE_PICTURES_GET_ERROR_ALERT } from "../../../../../../../../../constants/alerts_constants";
|
||||||
import { getProfilePictures } from "../../../../../../../../../controllers/API";
|
import { getProfilePictures } from "../../../../../../../../../controllers/API";
|
||||||
|
import axios from "axios";
|
||||||
|
|
||||||
const useGetProfilePictures = (setErrorData) => {
|
const useGetProfilePictures = (setErrorData) => {
|
||||||
const handleGetProfilePictures = async (abortSignal) => {
|
const handleGetProfilePictures = async () => {
|
||||||
try {
|
try {
|
||||||
const profilePictures = await getProfilePictures(abortSignal);
|
const profilePictures = await getProfilePictures();
|
||||||
return profilePictures!.files;
|
return profilePictures!.files;
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.log(error);
|
if (axios.isCancel(error)) {
|
||||||
|
console.warn("Request canceled: ", error.message);
|
||||||
|
} else {
|
||||||
setErrorData({
|
setErrorData({
|
||||||
title: PROFILE_PICTURES_GET_ERROR_ALERT,
|
title: PROFILE_PICTURES_GET_ERROR_ALERT,
|
||||||
list: [(error as any)?.response?.data?.detail],
|
list: [(error as any)?.response?.data?.detail],
|
||||||
});
|
});
|
||||||
throw error;
|
}
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -17,9 +17,7 @@ type ProfilePictureFormComponentProps = {
|
||||||
profilePicture: string;
|
profilePicture: string;
|
||||||
handleInput: (event: any) => void;
|
handleInput: (event: any) => void;
|
||||||
handlePatchProfilePicture: (gradient: string) => void;
|
handlePatchProfilePicture: (gradient: string) => void;
|
||||||
handleGetProfilePictures: (
|
handleGetProfilePictures: () => Promise<string[]>;
|
||||||
abortSignal: GenericAbortSignal | undefined,
|
|
||||||
) => Promise<string[]>;
|
|
||||||
userData: any;
|
userData: any;
|
||||||
};
|
};
|
||||||
const ProfilePictureFormComponent = ({
|
const ProfilePictureFormComponent = ({
|
||||||
|
|
@ -37,7 +35,7 @@ const ProfilePictureFormComponent = ({
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
const abortController = new AbortController();
|
const abortController = new AbortController();
|
||||||
|
|
||||||
handleGetProfilePictures(abortController.signal)
|
handleGetProfilePictures()
|
||||||
.then((data) => {
|
.then((data) => {
|
||||||
if (data) {
|
if (data) {
|
||||||
data.forEach((profile_picture) => {
|
data.forEach((profile_picture) => {
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue