Skip to content

Commit

Permalink
Fix/axios (#74)
Browse files Browse the repository at this point in the history
* fix interceptors

* update rainbowkit config

* remove log

* fix
  • Loading branch information
mehdi-torabiv authored Dec 5, 2024
1 parent ff20fde commit e71e426
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 18 deletions.
2 changes: 2 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 4 additions & 2 deletions src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import {
} from '@rainbow-me/rainbowkit';
import { QueryClient, QueryClientProvider } from '@tanstack/react-query';
import { Navigate, Route, Routes } from 'react-router-dom';
import { arbitrum } from 'viem/chains';
import { arbitrum, baseSepolia } from 'viem/chains';
import { useAccount } from 'wagmi';

import MobileScreensContainer from './components/layouts/MobileScreensContainer';
Expand Down Expand Up @@ -38,6 +38,8 @@ const queryClient = new QueryClient({
const App: React.FC = () => {
const isSmallScreen = useMediaQuery(theme.breakpoints.down('md'));

const isProduction = import.meta.env.VITE_IS_MAINNET === 'true';

const { authStatus, authenticationAdapter } = useSiweAuth();
const { chainId } = useAccount();

Expand All @@ -54,7 +56,7 @@ const App: React.FC = () => {
status={authStatus}
>
<RainbowKitProvider
initialChain={chainId ?? arbitrum}
initialChain={chainId ?? (isProduction ? arbitrum : baseSepolia)}
theme={lightTheme({
accentColor: '#4200FF',
})}
Expand Down
11 changes: 8 additions & 3 deletions src/main.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { CssBaseline } from '@mui/material';
import { getDefaultConfig } from '@rainbow-me/rainbowkit';
import ReactDOM from 'react-dom/client';
import { BrowserRouter } from 'react-router-dom';
import { arbitrum, baseSepolia, optimismSepolia } from 'viem/chains';
import { arbitrum, baseSepolia, Chain } from 'viem/chains';
import { http, WagmiProvider } from 'wagmi';

import App from './App';
Expand All @@ -17,12 +17,17 @@ if (!import.meta.env.VITE_PROJECT_ID) {
}
const projectID = import.meta.env.VITE_PROJECT_ID;

export const SUPPORTED_CHAINS = [baseSepolia, optimismSepolia, arbitrum];
const isProduction = import.meta.env.VITE_IS_MAINNET === 'true';

export const SUPPORTED_CHAINS: Chain[] = isProduction
? [arbitrum]
: [baseSepolia];

const config = getDefaultConfig({
appName: 'Identity on chain platform',
projectId: projectID,
chains: [optimismSepolia, baseSepolia, arbitrum],
// eslint-disable-next-line @typescript-eslint/no-explicit-any
chains: SUPPORTED_CHAINS as any,
transports: SUPPORTED_CHAINS.reduce(
(obj, chain) => ({ ...obj, [chain.id]: http() }),
{}
Expand Down
35 changes: 22 additions & 13 deletions src/services/api/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import axios from 'axios';
import { useNavigate } from 'react-router-dom';

import useSnackbarStore from '../../store/useSnackbarStore';

Expand Down Expand Up @@ -41,22 +40,32 @@ apiInstance.interceptors.request.use(
apiInstance.interceptors.response.use(
(response) => response,
(error) => {
const navigate = useNavigate();
const { showSnackbar } = useSnackbarStore();
if (error.response?.status === 400) {
showSnackbar('Bad Request', {
severity: 'error',
});
}
const { showSnackbar } = useSnackbarStore.getState();

if (error.response?.status === 401) {
// Show the snackbar message
localStorage.removeItem('OCI_TOKEN');
showSnackbar('Session expired. Please log in again.', {
severity: 'warning',
severity: 'error',
duration: 5000,
position: { vertical: 'bottom', horizontal: 'right' },
});
window.location.href = '/auth/login';
} else if (error.response?.status === 400) {
showSnackbar(
`${error.response?.data?.message?.message?.[0] || 'Bad request'}`,
{
severity: 'warning',
duration: 5000,
position: { vertical: 'bottom', horizontal: 'right' },
}
);
window.location.href = '/auth/login';
} else {
showSnackbar('An unexpected error occurred.', {
severity: 'error',
duration: 5000,
position: { vertical: 'bottom', horizontal: 'right' },
});

// Redirect the user to the login page
navigate('/auth/login');
}

return Promise.reject(error);
Expand Down

0 comments on commit e71e426

Please sign in to comment.