2023-05-04 12:09:06 +03:00
|
|
|
import React, { useEffect, useState } from 'react';
|
2023-06-04 12:23:09 +03:00
|
|
|
import { Navigate, Route, Routes } from 'react-router-dom';
|
2023-06-05 23:20:55 +03:00
|
|
|
import { ThemeProvider } from '@emotion/react';
|
|
|
|
|
|
|
|
import { browserPrefersDarkMode } from '@/utils/utils';
|
2023-06-04 12:23:09 +03:00
|
|
|
|
|
|
|
import { RequireAuth } from './modules/auth/components/RequireAuth';
|
|
|
|
import { getUserIdFromToken } from './modules/auth/services/AuthService';
|
|
|
|
import { AppLayout } from './modules/ui/layout/AppLayout';
|
2023-06-05 23:20:55 +03:00
|
|
|
import { darkTheme, lightTheme } from './modules/ui/layout/styles/themes';
|
2023-06-04 12:23:09 +03:00
|
|
|
import { mapToUser, User } from './modules/users/interfaces/user.interface';
|
|
|
|
import { useGetCurrentUserQuery } from './modules/users/services';
|
2023-04-20 19:46:27 +03:00
|
|
|
import AuthCallback from './pages/auth/Callback';
|
2023-06-04 12:23:09 +03:00
|
|
|
import { Login } from './pages/auth/Login';
|
|
|
|
import { Companies } from './pages/companies/Companies';
|
|
|
|
import { Opportunities } from './pages/opportunities/Opportunities';
|
|
|
|
import { People } from './pages/people/People';
|
2022-12-01 17:58:08 +03:00
|
|
|
|
2023-06-05 23:20:55 +03:00
|
|
|
type AppProps = {
|
|
|
|
themeEnabled?: boolean;
|
|
|
|
};
|
|
|
|
|
|
|
|
export function App({ themeEnabled = true }: AppProps) {
|
2023-05-04 12:09:06 +03:00
|
|
|
const [user, setUser] = useState<User | undefined>(undefined);
|
2023-01-27 14:12:04 +03:00
|
|
|
|
2023-05-12 10:02:39 +03:00
|
|
|
const userIdFromToken = getUserIdFromToken();
|
|
|
|
const { data } = useGetCurrentUserQuery(userIdFromToken);
|
2023-05-04 12:09:06 +03:00
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
if (data?.users[0]) {
|
2023-05-17 15:50:49 +03:00
|
|
|
setUser(mapToUser(data?.users[0]));
|
2023-05-04 12:09:06 +03:00
|
|
|
}
|
|
|
|
}, [data]);
|
2023-04-09 17:43:43 +03:00
|
|
|
|
2023-06-05 23:20:55 +03:00
|
|
|
const defaultTheme = browserPrefersDarkMode() ? darkTheme : lightTheme;
|
|
|
|
|
|
|
|
const app = (
|
2023-05-30 22:03:50 +03:00
|
|
|
<>
|
2023-03-06 18:15:46 +03:00
|
|
|
{
|
2023-05-04 12:09:06 +03:00
|
|
|
<AppLayout user={user}>
|
2023-03-06 18:15:46 +03:00
|
|
|
<Routes>
|
2023-04-21 16:36:10 +03:00
|
|
|
<Route
|
|
|
|
path="/"
|
|
|
|
element={
|
|
|
|
<RequireAuth>
|
|
|
|
<Navigate to="/people" replace />
|
|
|
|
</RequireAuth>
|
|
|
|
}
|
|
|
|
/>
|
|
|
|
<Route
|
|
|
|
path="/people"
|
|
|
|
element={
|
|
|
|
<RequireAuth>
|
|
|
|
<People />
|
|
|
|
</RequireAuth>
|
|
|
|
}
|
|
|
|
/>
|
|
|
|
<Route
|
|
|
|
path="/companies"
|
|
|
|
element={
|
|
|
|
<RequireAuth>
|
|
|
|
<Companies />
|
|
|
|
</RequireAuth>
|
|
|
|
}
|
|
|
|
/>
|
2023-04-26 19:00:06 +03:00
|
|
|
<Route
|
|
|
|
path="/opportunities"
|
|
|
|
element={
|
|
|
|
<RequireAuth>
|
|
|
|
<Opportunities />
|
|
|
|
</RequireAuth>
|
|
|
|
}
|
|
|
|
/>
|
2023-04-20 19:46:27 +03:00
|
|
|
<Route path="/auth/callback" element={<AuthCallback />} />
|
|
|
|
<Route path="/auth/login" element={<Login />} />
|
2023-03-06 18:15:46 +03:00
|
|
|
</Routes>
|
|
|
|
</AppLayout>
|
|
|
|
}
|
2023-05-30 22:03:50 +03:00
|
|
|
</>
|
2022-12-01 17:58:08 +03:00
|
|
|
);
|
2023-06-05 23:20:55 +03:00
|
|
|
|
|
|
|
return (
|
|
|
|
<>
|
|
|
|
{themeEnabled ? (
|
|
|
|
<ThemeProvider theme={defaultTheme}>{app}</ThemeProvider>
|
|
|
|
) : (
|
|
|
|
app
|
|
|
|
)}
|
|
|
|
</>
|
|
|
|
);
|
2022-12-01 17:58:08 +03:00
|
|
|
}
|