Skip to content

Commit

Permalink
fix: imports, pages and redux.
Browse files Browse the repository at this point in the history
  • Loading branch information
demosalexas committed Jun 27, 2022
1 parent 8fabdfb commit b98948d
Show file tree
Hide file tree
Showing 17 changed files with 140 additions and 23 deletions.
1 change: 1 addition & 0 deletions .env.example
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
VITE_API_URL=
16 changes: 13 additions & 3 deletions src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,11 @@ import { Routes, Route } from 'react-router-dom';
import { ThemeProvider } from 'styled-components';
import { Provider } from 'react-redux';

import { Login } from '@pages';
import { Login, Shows, Profiles, Home } from '@pages';
import {
HOME_URL, LOGIN_URL, MOVIES_URL, MY_LIST_URL, PROFILES_URL, SHOWS_URL, TV_SHOWS_URL,
} from '@url-paths';
import { Guard } from '@components';
import GlobalStyles from './themes/main/global-styles';
import theme from './themes/main/theme';
import store from './store/store/store';
Expand All @@ -13,8 +17,14 @@ function App(): JSX.Element {
<GlobalStyles />
<ThemeProvider theme={theme}>
<Routes>
<Route element={<Login />} path="/login" />
<Route element={<Login />} path="/sign-up" />
<Route element={<Home />} path={HOME_URL} />
<Route element={<h1>Olá</h1>} path={HOME_URL} />
<Route element={<Login />} path={LOGIN_URL} />
<Route element={<Guard><Profiles /></Guard>} path={PROFILES_URL} />
<Route element={<Guard><Shows /></Guard>} path={SHOWS_URL} />
<Route element={<Guard><Shows /></Guard>} path={MOVIES_URL} />
<Route element={<Guard><Shows /></Guard>} path={TV_SHOWS_URL} />
<Route element={<Guard><Shows /></Guard>} path={MY_LIST_URL} />
</Routes>
</ThemeProvider>
</Provider>
Expand Down
9 changes: 9 additions & 0 deletions src/components/footer/footer.component.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
function Footer() {
return (
<footer>
footer
</footer>
);
}

export default Footer;
3 changes: 1 addition & 2 deletions src/components/guard/guard.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,12 @@ import { useSelector } from 'react-redux';
import { useLocation, useNavigate } from 'react-router';

import { Props } from './guard.type';
import { tokenSelector } from 'store/user/user.selector';
import { tokenSelector } from '../../store/user/user.selector';

function Guard({ children }: Props) {
const token = useSelector(tokenSelector);
const navigate = useNavigate();
const from = useLocation();

useEffect(() => {
if (!token) {
navigate('/login', {
Expand Down
10 changes: 5 additions & 5 deletions src/components/guard/guard.type.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
type Props {

}
type Props = {
children: JSX.Element
};

export type {
Props
}
Props,
};
9 changes: 9 additions & 0 deletions src/components/header/header.component.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
function Header() {
return (
<header>
Ola
</header>
);
}

export default Header;
3 changes: 3 additions & 0 deletions src/components/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,6 @@ export { default as FormError } from './form-error/form-error';
export { default as NetflixLogo } from './netflix-logo/netflix-logo';
export { default as ShowCard } from './show-card/show-card.component';
export { default as Guard } from './guard/guard';
export { default as Header } from './header/header.component';
export { default as Footer } from './footer/footer.component';
export { default as ShowsRow } from './shows-row/shows-row.component';
7 changes: 7 additions & 0 deletions src/components/shows-row/shows-row.component.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
function ShowsRow({ shows, title }: { shows: [], title: string }) {
return (
<h1>ShowsRow</h1>
);
}

export default ShowsRow;
2 changes: 1 addition & 1 deletion src/modules/axios/axios.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import axios from 'axios';

const axiosInstance = axios.create({
baseURL: 'http://localhost:3000',
baseURL: import.meta.env.VITE_API_URL,
});

export default axiosInstance;
4 changes: 1 addition & 3 deletions src/pages/home/home.page.tsx
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
import { ShowCard } from '@components';

function Home() {
return (
<ShowCard />
<h1>Home</h1>
);
}

Expand Down
4 changes: 3 additions & 1 deletion src/pages/index.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
export { default as Login } from './login/login';
export { default as Login } from './login/login.page';
export { default as Home } from './home/home.page';
export { default as Shows } from './shows/shows.page';
export { default as Profiles } from './profiles/profiles.page';
37 changes: 30 additions & 7 deletions src/pages/login/login.tsx → src/pages/login/login.page.tsx
Original file line number Diff line number Diff line change
@@ -1,19 +1,24 @@
import { useState, useCallback, ChangeEvent } from 'react';
import { useState, useCallback, ChangeEvent, useEffect } from 'react';
import * as yup from 'yup';
import { useDispatch, useSelector } from 'react-redux';
import { Grid } from '@mui/material';
import { useLocation, useNavigate } from 'react-router-dom';

import {
Button, Input, FormError, NetflixLogo,
} from '@components';
import { Wrapper } from './login.styled';
import { authenticated } from '../../store/user/user.selector';
import userSlice from '../../store/user/user.slice';
import { Error } from '../../types/yup';
// import { Error } from '../../types/yup';
import { USER_TOKEN_COOKIE } from '../../store/user/user.type';
import { errorSelector, tokenSelector } from '../../store/user/user.selector';

function Login(): JSX.Element {
const dispatch = useDispatch();
const userAuthenticated = useSelector(authenticated);
const token = useSelector(tokenSelector);
const userError = useSelector(errorSelector);
const navigate = useNavigate();
const from = useLocation();

const [data, setData] = useState<object>({
email: '',
Expand Down Expand Up @@ -47,12 +52,30 @@ function Login(): JSX.Element {

resetError('');

dispatch(userSlice.actions.setData({}));
dispatch(userSlice.actions.authentication(data));
} catch (yupError: unknown) {
setError((yupError as Error).erros[0]);
setError((yupError as Error).errors[0]);
}
}, [data]);

useEffect(() => {
if (token) {
navigate('/', {
state: { from },
});
}
}, [token]);

useEffect(() => {
const localToken = localStorage.getItem(USER_TOKEN_COOKIE);

if (localToken) {
dispatch(userSlice.actions.setData({
token: localToken,
}));
}
}, []);

return (
<Wrapper
container
Expand All @@ -73,7 +96,7 @@ function Login(): JSX.Element {
placeholder="Senha"
onChange={handleChange}
/>
<FormError message={error} />
<FormError message={error || userError} />
<Button onClick={handleSend}>
Entrar
</Button>
Expand Down
7 changes: 7 additions & 0 deletions src/pages/profiles/profiles.page.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
function Profiles() {
return (
<h1>Profiles</h1>
);
}

export default Profiles;
31 changes: 31 additions & 0 deletions src/pages/shows/shows.page.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import React, { useEffect } from 'react';
import { useDispatch, useSelector } from 'react-redux';

import { showsActions } from 'store/shows/shows.slice';
import { Header, Footer, ShowsRow } from '@components';
import { listSelector, myListSelector } from 'store/shows/shows.selector';

function Shows() {
const dispatch = useDispatch();
const shows = useSelector(listSelector);
const myList = useSelector(myListSelector);
const movies = shows.MOVIE;
const tvShows = shows.TV_SHOWS;

useEffect(() => {
dispatch(showsActions.getList());
dispatch(showsActions.getMyList());
}, []);

return (
<>
<Header />
{ myList && <ShowsRow shows={myList} title="Minha Lista" />}
{ movies && <ShowsRow shows={movies} title="Filmes" />}
{ tvShows && <ShowsRow shows={tvShows} title="Séries" />}
<Footer />
</>
);
}

export default Shows;
2 changes: 1 addition & 1 deletion src/services/user/user.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import axiosInstance from '../../modules/axios/axios';
import { AuthPayload } from './user.type';

const userService = () => {
const auth = (payload: AuthPayload) => axiosInstance.post('/auth', payload);
const auth = (payload: AuthPayload) => axiosInstance.post('/login', payload);

return {
auth,
Expand Down
17 changes: 17 additions & 0 deletions src/url-paths/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
const HOME_URL = '/';
const SHOWS_URL = '/shows';
const LOGIN_URL = '/login';
const PROFILES_URL = '/profiles';
const MOVIES_URL = '/movies';
const TV_SHOWS_URL = '/tv-shows';
const MY_LIST_URL = '/my-list';

export {
HOME_URL,
SHOWS_URL,
LOGIN_URL,
PROFILES_URL,
MOVIES_URL,
TV_SHOWS_URL,
MY_LIST_URL,
};
1 change: 1 addition & 0 deletions tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
"paths": {
"@components": ["./src/components/index"],
"@pages": ["./src/pages/index"],
"@url-paths": ["./src/url-paths/index"],
"store/*": ["./src/store/*"]
}
},
Expand Down

0 comments on commit b98948d

Please sign in to comment.