Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix/axios #74

Merged
merged 4 commits into from
Dec 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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,
mehdi-torabiv marked this conversation as resolved.
Show resolved Hide resolved
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();
mehdi-torabiv marked this conversation as resolved.
Show resolved Hide resolved

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
Loading