Skip to content

Commit

Permalink
feat: admin setup and users page
Browse files Browse the repository at this point in the history
Co-authored-by: Mateus Maia <mateusmaiamaia@hotmail.com>
Co-authored-by: Matheus Monteiro <matheusyanmonteiro@gmail.com>
  • Loading branch information
3 people committed Dec 9, 2024
1 parent 61279c1 commit 2bb331d
Show file tree
Hide file tree
Showing 9 changed files with 26 additions and 11 deletions.
4 changes: 2 additions & 2 deletions index.html
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@
<html lang="en">
<head>
<meta charset="UTF-8" />
<link rel="icon" type="image/svg+xml" href="/vite.svg" />
<link rel="icon" type="image/svg+xml" href="/logo.png" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Vite + React + TS</title>
<title>Livro Livre Admin</title>
<link rel="preconnect" href="https://fonts.googleapis.com">
<link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
<link href="https://fonts.googleapis.com/css2?family=Poppins:ital,wght@0,100;0,200;0,300;0,400;0,500;0,600;0,700;0,800;0,900;1,100;1,200;1,300;1,400;1,500;1,600;1,700;1,800;1,900&display=swap" rel="stylesheet">
Expand Down
Binary file added public/logo.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1 change: 0 additions & 1 deletion public/vite.svg

This file was deleted.

1 change: 1 addition & 0 deletions src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ function App() {
<BrowserRouter>
<Routes>
<Route path="*" element={<NotFound />} />
<Route path="/" element={<PrivateRoute><Home /></PrivateRoute>} />
<Route path="/login" element={<SignIn />} />
<Route path="/inicio" element={<PrivateRoute><Home /></PrivateRoute>} />
<Route path="/usuarios" element={<PrivateRoute><Users /></PrivateRoute>} />
Expand Down
6 changes: 3 additions & 3 deletions src/PrivateRoute.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@ import { useEffect } from "react";

const PrivateRoute = ({ children }: { children: any }) => {
const navigate = useNavigate();
const { token } = useAuth();
const { isAuthenticated } = useAuth();

useEffect(() => {
if (token) return;
if (isAuthenticated) return;
navigate('/login');
})
}, [isAuthenticated])

return children;
}
Expand Down
5 changes: 4 additions & 1 deletion src/hooks/useApi/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ const useApi = () => {
getUsers: (data: {
perPage: number;
page: number
}): Promise<{ data: {
}, token: string | null): Promise<{ data: {
items: User[],
total: number,
} }> => {
Expand All @@ -55,6 +55,9 @@ const useApi = () => {
params: {
perPage: data.perPage,
page: data.page,
},
headers: {
Authorization: `Bearer ${token}`,
}
})
.then((res) => resolve(res))
Expand Down
6 changes: 3 additions & 3 deletions src/pages/SignIn/SignInForm/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ function SignInForm() {
formState: { errors, isValid },
} = useForm<FormValues>();

const { signIn, token } = useAuth();
const { signIn, isAuthenticated } = useAuth();

const onSubmit = handleSubmit(async (data: FormValues) => {
setLoading(true);
Expand All @@ -34,9 +34,9 @@ function SignInForm() {
})

useEffect(() => {
if (!token) return;
if (!isAuthenticated) return;
navigate('/inicio');
}, [token])
}, [isAuthenticated])

return (
<form onSubmit={onSubmit}>
Expand Down
10 changes: 10 additions & 0 deletions src/pages/SignIn/index.tsx
Original file line number Diff line number Diff line change
@@ -1,8 +1,18 @@
import { Box, Center, Stack } from '@chakra-ui/react';
import SignInForm from './SignInForm';
import SignInHeader from './SignInHeader';
import { useNavigate } from 'react-router';
import { useAuth } from '../../hooks/useAuth';
import { useEffect } from 'react';

function SignIn() {
const navigate = useNavigate();
const { isAuthenticated } = useAuth();

useEffect(() => {
if (isAuthenticated) navigate('/inicio');
}, [isAuthenticated])

return (
<Box padding='40px'>
<Center>
Expand Down
4 changes: 3 additions & 1 deletion src/pages/Users/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,18 @@ import {
PaginationPrevTrigger,
PaginationRoot,
} from "../../components/ui/pagination";
import { useAuth } from '../../hooks/useAuth';

function Users() {
const [users, setUsers] = useState<User[]>([]);
const [usersCount, setUsersCount] = useState(0);
const [page, setPage] = useState(1);

const { getUsers } = useApi();
const { token } = useAuth();

const fetchUsers = async () => {
const { data } = await getUsers({ perPage: 10, page: page - 1 });
const { data } = await getUsers({ perPage: 10, page: page - 1 }, token);
setUsers(data.items);
setUsersCount(data.total);
}
Expand Down

0 comments on commit 2bb331d

Please sign in to comment.