Skip to content

Commit ee91cbd

Browse files
committed
fix: API Requests
1 parent 4ddc170 commit ee91cbd

File tree

6 files changed

+48
-21
lines changed

6 files changed

+48
-21
lines changed

src/pages/auth/login-page.tsx

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
1-
import { Link, useNavigate } from 'react-router';
1+
import { Link, Navigate, useNavigate } from 'react-router';
2+
import { useDispatch, useSelector } from 'react-redux';
23
import { useForm } from 'react-hook-form';
3-
import { useDispatch } from 'react-redux';
44
import { clsx } from 'clsx';
55

6+
import { userSelectors } from '@/shared/store/features/user/user-selectors.ts';
67
import { loginAction } from '@/shared/store/features/user/user-slices';
78
import { LoginQuery } from '@/shared/services/user-api/types';
89
import { useLoginMutation } from '@/shared/services/user-api';
@@ -13,6 +14,11 @@ export function LoginPage() {
1314
const navigate = useNavigate();
1415
const dispatch = useDispatch();
1516
const [loginRequest, { isLoading }] = useLoginMutation();
17+
const { isAuthenticated } = useSelector(userSelectors.userInfo);
18+
19+
if (isAuthenticated) {
20+
return <Navigate to={APP_ROUTES.main} replace />;
21+
}
1622

1723
const { register, handleSubmit } = useForm<LoginQuery>({
1824
defaultValues: {
@@ -72,7 +78,7 @@ export function LoginPage() {
7278
{isLoading ? 'Loading...' : 'Login'}
7379
</button>
7480

75-
<Link to={APP_ROUTES.landing}>Return Home</Link>
81+
<Link to={APP_ROUTES.landing}>[ Return Home ]</Link>
7682
</form>
7783
</div>
7884
);

src/pages/landing-page.tsx

Lines changed: 27 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,42 @@
1-
import { Link, Navigate } from 'react-router';
2-
import { useSelector } from 'react-redux';
1+
import { useDispatch, useSelector } from 'react-redux';
2+
import { Link } from 'react-router';
33

44
import { userSelectors } from '@/shared/store/features/user/user-selectors';
5+
import { logoutAction } from '@/shared/store/features/user/user-slices';
56
import { APP_ROUTES } from '@/shared/constants';
67

78
export function LandingPage() {
9+
const dispatch = useDispatch();
810
const { isAuthenticated } = useSelector(userSelectors.userInfo);
911

10-
if (isAuthenticated) {
11-
return <Navigate to={APP_ROUTES.main} replace />;
12-
}
12+
const handleLogout = () => {
13+
dispatch(logoutAction());
14+
};
1315

1416
return (
1517
<main className='flex flex-col items-center justify-center h-dvh gap-6'>
16-
<h3>Landing Page</h3>
18+
<h3 className='text-2xl'>Landing Page</h3>
19+
{isAuthenticated && <p className='text-slate-400'>You Are Logged In Now</p>}
1720
<div className='flex items-center justify-center gap-4 flex-wrap'>
18-
<Link className='border py-3 px-4 rounded' to={APP_ROUTES.login}>
19-
Login Sample
20-
</Link>
21-
<Link className='border py-3 px-4 rounded' to={APP_ROUTES.main}>
22-
Protected Route
21+
{isAuthenticated ? (
22+
<button className='border py-3 px-4 rounded' onClick={handleLogout}>
23+
Logout
24+
</button>
25+
) : (
26+
<Link className='border py-3 px-4 rounded' to={APP_ROUTES.login}>
27+
Login Page
28+
</Link>
29+
)}
30+
31+
<Link className='flex gap-2 border py-3 px-4 rounded' to={APP_ROUTES.main}>
32+
Main Page
33+
{isAuthenticated ? (
34+
<span className='text-green-500'>( Allowed )</span>
35+
) : (
36+
<span className='text-red-500'>( Protected )</span>
37+
)}
2338
</Link>
39+
2440
<Link className='border py-3 px-4 rounded' to={APP_ROUTES.notFound}>
2541
Not Found Page
2642
</Link>

src/pages/main/main-page.tsx

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ import { useDispatch } from 'react-redux';
22

33
import { logoutAction } from '@/shared/store/features/user/user-slices';
44
import { useGetMainQuery } from '@/shared/services/main-api';
5+
import { APP_ROUTES } from '@/shared/constants';
6+
import { Link } from 'react-router';
57

68
export function MainPage() {
79
const dispatch = useDispatch();
@@ -13,23 +15,26 @@ export function MainPage() {
1315

1416
return (
1517
<main className='flex w-full flex-col items-center justify-center h-dvh gap-6'>
16-
<h3>Main Page ( Protected )</h3>
18+
<h3 className='flex gap-1'>
19+
Main Page <span className='text-red-500'>( Protected )</span>
20+
</h3>
1721
<button className='border py-3 px-4 rounded' onClick={handleLogout}>
1822
Logout
1923
</button>
24+
<Link to={APP_ROUTES.landing}>[ Return Home ]</Link>
2025

2126
{isFetching && <p>Loading...</p>}
2227
{data && (
23-
<div className='flex flex-col gap-2'>
24-
<div className='border-b py-2 px-4 flex items-center justify-between mb-4'>
28+
<div className='w-full flex flex-col gap-2'>
29+
<div className='w-full border-b py-2 px-4 flex items-center justify-between mb-4'>
2530
<span>Name</span>
2631
<span>Age</span>
2732
</div>
2833

2934
{data.data.map((item) => (
3035
<div
3136
key={item.id}
32-
className='border py-2 px-4 rounded flex items-center justify-between'
37+
className='w-full border py-2 px-4 rounded flex items-center justify-between'
3338
>
3439
<span>{item.name}</span>
3540
<span>{item.age}</span>

src/pages/not-found-page.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ export function NotFoundPage() {
1212
)}
1313
>
1414
<h3>404 | Not Found Page</h3>
15-
<Link to={APP_ROUTES.landing}>Return Home</Link>
15+
<Link to={APP_ROUTES.landing}>[ Return Home ]</Link>
1616
</main>
1717
);
1818
}
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
export const ENDPOINT = {
2-
main: 'react-sample'
2+
main: 'react-sample/'
33
} as const;
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
export const ENDPOINT = {
2-
login: 'react-sample'
2+
login: 'react-sample/'
33
} as const;

0 commit comments

Comments
 (0)