Skip to content

Commit

Permalink
Merge pull request #3 from ktvtk/module3-task1
Browse files Browse the repository at this point in the history
  • Loading branch information
keksobot authored Nov 3, 2024
2 parents e0b8274 + c9df39e commit 0d7a172
Show file tree
Hide file tree
Showing 4 changed files with 80 additions and 1 deletion.
38 changes: 37 additions & 1 deletion src/components/app/app.tsx
Original file line number Diff line number Diff line change
@@ -1,9 +1,45 @@
import React from 'react';
import {BrowserRouter, Route, Routes} from 'react-router-dom';
import {MainScreen} from '../../pages/main-screen/main-screen.tsx';
import {MainScreenProps} from '../../props/main-screen-props.ts';
import {LoginScreen} from '../../pages/login-screen/login-screen.tsx';
import {OfferScreen} from '../../pages/offer-screen/offer-screen.tsx';
import {NotFoundScreen} from '../../pages/not-found-screen/not-found-screen.tsx';
import {AppRoute, AuthorizationStatus} from '../../const.ts';
import {PrivateRoute} from '../private-route/private-route.tsx';
import {FavoriteScreen} from '../../pages/favorites-screen/favorite-screen.tsx';

export function App({placeCount}: MainScreenProps): React.JSX.Element {
return (
<MainScreen placeCount={placeCount}/>
<BrowserRouter>
<Routes>
<Route
path={AppRoute.Main}
element={<MainScreen placeCount={placeCount}/>}
/>
<Route
path={AppRoute.Login}
element={<LoginScreen />}
/>
<Route
path={AppRoute.Offer}
element={<OfferScreen />}
/>
<Route
path={AppRoute.Favorites}
element={
<PrivateRoute
authorizationStatus={AuthorizationStatus.NoAuth}
>
<FavoriteScreen />
</PrivateRoute>
}
/>
<Route
path='*'
element={<NotFoundScreen />}
/>
</Routes>
</BrowserRouter>
);
}
18 changes: 18 additions & 0 deletions src/components/private-route/private-route.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import {JSX} from 'react';
import {Navigate} from 'react-router-dom';
import {AppRoute, AuthorizationStatus} from '../../const';

type PrivateRouteProps = {
authorizationStatus: AuthorizationStatus;
children: JSX.Element;
}

export function PrivateRoute(props: PrivateRouteProps): JSX.Element {
const {authorizationStatus, children} = props;

return (
authorizationStatus === AuthorizationStatus.Auth
? children
: <Navigate to={AppRoute.Login} />
);
}
12 changes: 12 additions & 0 deletions src/const.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
export enum AppRoute {
Main = '/',
Login = '/login',
Favorites = '/favorites',
Offer = '/offer/:id'
}

export enum AuthorizationStatus {
Auth = 'AUTH',
NoAuth = 'NO_AUTH',
Unknown = 'UNKNOWN',
}
13 changes: 13 additions & 0 deletions src/pages/not-found-screen/not-found-screen.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import {JSX} from 'react';

export function NotFoundScreen() : JSX.Element {
return (
<body>
<div className="page-404">
<h1>404 Not Found</h1>
<p>Извините, страница не найдена.</p>
<a href="/">На главную</a>
</div>
</body>
);
}

0 comments on commit 0d7a172

Please sign in to comment.