|
@@ -2,6 +2,7 @@ import axios from 'axios';
|
|
|
import { API_URL, APP_VERSION } from '../constants';
|
|
|
import { Platform } from 'react-native';
|
|
|
import { showBanner } from './bannerUtils';
|
|
|
+import { useErrorStore } from 'src/stores/errorStore';
|
|
|
|
|
|
export const request = axios.create({
|
|
|
baseURL: API_URL,
|
|
@@ -11,7 +12,7 @@ export const request = axios.create({
|
|
|
export const setupInterceptors = ({
|
|
|
showError
|
|
|
}: {
|
|
|
- showError: (message: string, loginNeeded: boolean) => void;
|
|
|
+ showError: (message: string, loginNeeded: boolean, authNeeded: boolean) => void;
|
|
|
}) => {
|
|
|
request.interceptors.request.use(
|
|
|
(config) => {
|
|
@@ -31,22 +32,40 @@ export const setupInterceptors = ({
|
|
|
|
|
|
request.interceptors.response.use(
|
|
|
(response) => {
|
|
|
+ const { isErrorShown, setErrorShown } = useErrorStore.getState();
|
|
|
+
|
|
|
if (response.data.result === 'ERROR' && response.data.result_description) {
|
|
|
- const showErrorWithDelay = (message: string, requiresLogin: boolean) => {
|
|
|
- setTimeout(() => {
|
|
|
- showError(message, requiresLogin);
|
|
|
- }, 1000);
|
|
|
+ const showErrorWithDelay = (
|
|
|
+ message: string,
|
|
|
+ requiresLogin: boolean,
|
|
|
+ requiresAuth: boolean
|
|
|
+ ) => {
|
|
|
+ if (!isErrorShown) {
|
|
|
+ setErrorShown(true);
|
|
|
+ setTimeout(() => {
|
|
|
+ showError(message, requiresLogin, requiresAuth);
|
|
|
+ }, 1000);
|
|
|
+ }
|
|
|
};
|
|
|
|
|
|
if (response.data?.login_needed && response.data.login_needed === 1) {
|
|
|
- showErrorWithDelay(response.data.result_description, true);
|
|
|
+ showErrorWithDelay(response.data.result_description, true, false);
|
|
|
+ return response;
|
|
|
+ } else if (
|
|
|
+ // todo: response.data.auth_needed === 1
|
|
|
+ response.data.result_description ===
|
|
|
+ 'Only authenticated users are allowed to send messages.'
|
|
|
+ ) {
|
|
|
+ showErrorWithDelay(response.data.result_description, false, true);
|
|
|
return response;
|
|
|
}
|
|
|
- showErrorWithDelay(response.data.result_description, false);
|
|
|
+ showErrorWithDelay(response.data.result_description, false, false);
|
|
|
}
|
|
|
return response;
|
|
|
},
|
|
|
(error) => {
|
|
|
+ const { isErrorShown, setErrorShown } = useErrorStore.getState();
|
|
|
+
|
|
|
if (error.code === 'ECONNABORTED') {
|
|
|
error.isTimeout = true;
|
|
|
showBanner('Slow internet connection!');
|
|
@@ -60,7 +79,10 @@ export const setupInterceptors = ({
|
|
|
? error.config.url?.split('/')?.filter(Boolean)?.pop()
|
|
|
: 'Unknown URL';
|
|
|
|
|
|
- showError(`${error.message} (${shortUrl})`, false);
|
|
|
+ if (!isErrorShown) {
|
|
|
+ setErrorShown(true);
|
|
|
+ showError(`${error.message} (${shortUrl})`, false, false);
|
|
|
+ }
|
|
|
|
|
|
return Promise.reject(error);
|
|
|
}
|