diff --git a/src/App.js b/src/App.js index 98bf316..e3394f6 100644 --- a/src/App.js +++ b/src/App.js @@ -1,26 +1,55 @@ import React, { useReducer } from 'react'; -import logo from './logo.svg'; import './App.css'; import { reducer, initialState } from './hooks/reducer'; import { BrowserRouter, Routes, Route, Navigate} from 'react-router-dom'; import Pokemon from './components/pages/pokemon'; import Details from './components/pages/details'; import SignIn from './components/pages/login'; -import {checkLogin} from './utils' -import {validUser, validPass } from './utils' + +const AuthPokemon = ({state, dispatch}) => { + if(state.login === 'true'){ + return + }else{ + return + } +} + +const AuthFavorites = ({state, dispatch}) => { + if(state.login === 'true'){ + return + }else{ + return + } +} + +const AuthDetails = ({state, dispatch}) => { + if(state.login === 'true'){ + return < Details searchValue={state.filterState.name} pokemon={state.detailPage.pokemon} currentType={state.filterState.type} dispatch={dispatch} /> + }else{ + return + } +} + +const AuthLogin = ({state, dispatch}) => { + if(state.login === 'true'){ + return + }else{ + return + } +} function App() { const [state, dispatch] = useReducer(reducer, initialState); - + return (
- : } /> - :
} /> - : } /> - } /> + } /> + } /> + } /> + } />
diff --git a/src/components/bottombar.jsx b/src/components/bottombar.jsx index 1ba66cc..0cbf64e 100644 --- a/src/components/bottombar.jsx +++ b/src/components/bottombar.jsx @@ -9,7 +9,7 @@ import StarIcon from '@mui/icons-material/Star'; import ExitToAppIcon from '@mui/icons-material/ExitToApp'; import { useNavigate, useLocation } from 'react-router-dom'; -export default function FixedBottomNavigation() { +export default function FixedBottomNavigation({dispatch}) { const navigateTo = useNavigate(); let location = useLocation(); /* console.log('Location', location) */ @@ -25,11 +25,8 @@ export default function FixedBottomNavigation() { } else if (newValue === '1') { navigateTo('/favorites'); } else if (newValue === '2') { - localStorage.removeItem('user') - localStorage.removeItem('pass') - navigateTo('/login') - // console.log(newValue); - + dispatch({type:'logout'}) + navigateTo('/login') } }} > diff --git a/src/components/filterbar.jsx b/src/components/filterbar.jsx index c699835..d72c9b1 100644 --- a/src/components/filterbar.jsx +++ b/src/components/filterbar.jsx @@ -17,7 +17,7 @@ export default function BasicMenu({ currentType, dispatch }) { }; const handleActionClose = (e) => { - console.log(e.target.getAttribute("value")) + //(e.target.getAttribute("value")) dispatch({ type: "filterType", value: e.target.getAttribute("value") }) return handleClose(e) } diff --git a/src/components/navbar.jsx b/src/components/navbar.jsx index 9ac57e8..5edea35 100644 --- a/src/components/navbar.jsx +++ b/src/components/navbar.jsx @@ -5,6 +5,7 @@ import Toolbar from '@mui/material/Toolbar'; import IconButton from '@mui/material/IconButton'; import Typography from '@mui/material/Typography'; import Menu from '@mui/material/Menu'; +import MenuIcon from '@mui/icons-material/Menu'; import Container from '@mui/material/Container'; import Button from '@mui/material/Button'; import Tooltip from '@mui/material/Tooltip'; @@ -60,15 +61,14 @@ const ResponsiveAppBar = ({ searchValue, currentType, dispatch }) => { }; const handleCloseUserMenuLogout = () => { - localStorage.removeItem('user') - localStorage.removeItem('pass') + dispatch({type:'logout'}) navigateTo('/login'); setAnchorElUser(null); }; return ( - + logo navigateTo('/')} /> @@ -107,6 +107,12 @@ const ResponsiveAppBar = ({ searchValue, currentType, dispatch }) => { ) } })()} + {/* + + + + + */} diff --git a/src/components/pages/details.jsx b/src/components/pages/details.jsx index bcd481d..6a36cf7 100644 --- a/src/components/pages/details.jsx +++ b/src/components/pages/details.jsx @@ -10,7 +10,7 @@ function Details({searchValue, currentType, pokemon, dispatch}) { <> - + ) } diff --git a/src/components/pages/login.jsx b/src/components/pages/login.jsx index fe0cb45..d9ac17f 100644 --- a/src/components/pages/login.jsx +++ b/src/components/pages/login.jsx @@ -8,10 +8,14 @@ import Link from '@mui/material/Link'; import Grid from '@mui/material/Grid'; import Box from '@mui/material/Box'; import CatchingPokemonIcon from '@mui/icons-material/CatchingPokemon'; + import Typography from '@mui/material/Typography'; import Container from '@mui/material/Container'; import { useNavigate } from 'react-router-dom'; +const realUser = 'ash_ketchum' +const realPass = 'pikachu123' + function Copyright(props) { return ( @@ -25,70 +29,74 @@ function Copyright(props) { ); } -export default function SignIn() { - +export default function SignIn({state, dispatch}) { const navigateTo = useNavigate(); - const handleSubmit = (event) => { + const handleSubmit = (event) => { event.preventDefault(); const data = new FormData(event.currentTarget); const user = { email: data.get('email'), password: data.get('password'), } - /* dispatch({type: 'signIn', value: user}) */ - localStorage.setItem('user', user.email) - localStorage.setItem('pass', user.password) - - navigateTo('/') - + + const currentUser = user.email + const currentPass = user.password + + if (((currentUser===realUser) && (currentPass===realPass))){ + dispatch({type:'login'}) + console.log('correct credentials') + navigateTo('/') + } else{ + console.log('bad credentials') + } }; return ( - - - - - - - - Sign in - - - - - - + + + + + + + + Sign in + + + + + - - - ); + + + +); } \ No newline at end of file diff --git a/src/components/pages/pokemon.jsx b/src/components/pages/pokemon.jsx index 7632d62..ebc222d 100644 --- a/src/components/pages/pokemon.jsx +++ b/src/components/pages/pokemon.jsx @@ -25,6 +25,8 @@ const typeFilter = (type, data) => { function Pokemon({favs, actionName, data, page, filterState, dispatch}) { + // checkLogin(useNavigate) + const pokeCount = 898 const pageSize = 20; let filteredData = data @@ -85,12 +87,11 @@ function Pokemon({favs, actionName, data, page, filterState, dispatch}) { return ( <> - - - - - - ) + + + + + ) } export default Pokemon \ No newline at end of file diff --git a/src/components/pokedetails.jsx b/src/components/pokedetails.jsx index 7f2d7f2..fc5918d 100644 --- a/src/components/pokedetails.jsx +++ b/src/components/pokedetails.jsx @@ -138,7 +138,7 @@ function Pokedetails({ pokemon, dispatch }) { try { const response = await axios.get(`https://pokeapi.co/api/v2/pokemon/${id}/`); const fulldata = response.data; - console.log('Pokemon: ', fulldata); + //console.log('Pokemon: ', fulldata); dispatch({ type: 'getPokemon', pokemon: fulldata }); } catch (error) { console.error(error); @@ -182,7 +182,7 @@ function Pokedetails({ pokemon, dispatch }) { {pokemon && pokemon.types.map((type_, i) => { const type_name = String(type_.type.name) - console.log('TYPE IS', type_name) + //console.log('TYPE IS', type_name) const TypeComponent = typeComponents[type_name]; return (} diff --git a/src/components/pokelist.jsx b/src/components/pokelist.jsx index 483df07..e12d1a0 100644 --- a/src/components/pokelist.jsx +++ b/src/components/pokelist.jsx @@ -8,7 +8,7 @@ import Button from '@mui/material/Button'; import Typography from '@mui/material/Typography'; import StarBorderSharpIcon from '@mui/icons-material/StarBorderSharp'; import StarIcon from '@mui/icons-material/Star'; -import List from '@mui/material/List'; +import { Stack } from '@mui/material'; import ListItem from '@mui/material/ListItem'; import ManageSearchIcon from '@mui/icons-material/ManageSearch'; import Chip from '@mui/material/Chip'; @@ -32,6 +32,7 @@ import { ReactComponent as Psychic } from '../img/psychic.svg' import { ReactComponent as Rock } from '../img/rock.svg' import { ReactComponent as Steel } from '../img/steel.svg' import { ReactComponent as Water } from '../img/water.svg' + const typeComponents = { 'loading': Grass, 'undefined': Grass, @@ -91,21 +92,9 @@ function Pokelist({ favs, data, page, pageSize, dispatch }) { } function handleClick(e) { const id = e.currentTarget.value - console.log('id', id) + //console.log('id', id) navigateTo(`/${id}`); } - /* let typeOneColor - let typeTwoColor - const highestStat = Math.max(...pokemon.stats.map((item) => item.base_stat)) - console.log(highestStat) - - if (pokemon.types.length == 2) { - typeOneColor = typeColors[String(pokemon.types[0].type.name)] - typeTwoColor = typeColors[String(pokemon.types[1].type.name)] - } else { - typeOneColor = typeColors[String(pokemon.types[0].type.name)] - typeTwoColor = typeColors[String(pokemon.types[0].type.name)] - } */ return (
{pageData && pageData.map((pokemon, i) => { @@ -120,15 +109,25 @@ function Pokelist({ favs, data, page, pageSize, dispatch }) { sx={{objectFit: 'contain'}} /> - - {upperFirstLetter(pokemon.name)} - - + + + {upperFirstLetter(pokemon.name)} + + + #{pokemon.id} + + + {pokemon.pokemon_v2_pokemontypes.map((type_, i) => { const type_name = String(type_.pokemon_v2_type.name) - console.log('TYPE IS', type_name) + // console.log('TYPE IS', type_name) const TypeComponent = typeComponents[type_name]; - return (} label={type_name} key={"type" + i} @@ -138,30 +137,49 @@ function Pokelist({ favs, data, page, pageSize, dispatch }) { } - - {(() => { + + {(() => { if (favs.includes(pokemon.id)) { return dispatch({ type: 'setFav', value: pokemon.id })} /> } else { return dispatch({ type: 'setFav', value: pokemon.id })} /> } })()} - + - + - - + + - + + + #{pokemon.id} + + {upperFirstLetter(pokemon.name)} - + + {pokemon.pokemon_v2_pokemontypes.map((type_, i) => { const type_name = String(type_.pokemon_v2_type.name) - console.log('TYPE IS', type_name) + //console.log('TYPE IS', type_name) const TypeComponent = typeComponents[type_name]; - return (} label={type_name} key={"type" + i} @@ -170,7 +188,14 @@ function Pokelist({ favs, data, page, pageSize, dispatch }) { ) } - + {(() => { if (favs.includes(pokemon.id)) { return dispatch({ type: 'setFav', value: pokemon.id })} /> @@ -178,12 +203,12 @@ function Pokelist({ favs, data, page, pageSize, dispatch }) { return dispatch({ type: 'setFav', value: pokemon.id })} /> } })()} - + diff --git a/src/components/searchbar.jsx b/src/components/searchbar.jsx index 58d8464..82b139b 100644 --- a/src/components/searchbar.jsx +++ b/src/components/searchbar.jsx @@ -47,7 +47,7 @@ const StyledInputBase = styled(InputBase)(({ theme }) => ({ })); export default function SearchAppBar({searchValue, dispatch}) { - console.log('search Value is', searchValue) + //console.log('search Value is', searchValue) return ( diff --git a/src/hooks/reducer.js b/src/hooks/reducer.js index dbc429f..d4d5f9e 100644 --- a/src/hooks/reducer.js +++ b/src/hooks/reducer.js @@ -2,11 +2,13 @@ import {getLocalStorageObj} from '../utils' const [favs,] = getLocalStorageObj('favs',[]) const [favIds,] = getLocalStorageObj('favIds',[]) +const login = localStorage.getItem('login') export const initialState = { data: [], favs: favs, favIds: favIds, + login: login, mainPage: { currentData:[], page: 1, @@ -50,7 +52,7 @@ export const reducer = (state, action) => { return {...state, favPage: {...state.favPage, page: action.page}} case 'getPokemon': const pokemon = action.pokemon; - console.log('El pokemon es: ', pokemon.name) + //console.log('El pokemon es: ', pokemon.name) return {...state, detailPage: {pokemon: pokemon}} case 'setFav': const id = action.value; @@ -79,11 +81,19 @@ export const reducer = (state, action) => { if (newType === state.filterState.type){ newType = 'all' } else if(types_list.includes(newType)){ - console.log(newType) + //console.log(newType) } else { newType = 'all' } return {...state, filterState: {...state.filterState, type: newType}} + case 'login': + //console.log('login got to the reducer') + localStorage.setItem('login', 'true') + return {...state, login:'true'} + case 'logout': + //console.log('logout got to the reducer') + localStorage.removeItem('login') + return {...state, login:false} // case 'gen': // return {...state, filterState: {gen: action.value}} // const generation = action.generation; diff --git a/src/index.js b/src/index.js index fc1ce98..b598861 100644 --- a/src/index.js +++ b/src/index.js @@ -5,10 +5,11 @@ import App from './App'; import reportWebVitals from './reportWebVitals'; import CssBaseline from '@mui/material/CssBaseline'; import { createTheme, ThemeProvider } from '@mui/material/styles'; -import { checkValue, validUser, validPass} from './utils' +import { checkValue } from './utils' const appName = 'pokedata' const version = '0.15' + checkValue('appName', appName) checkValue('version', version) localStorage.setItem('appName', appName) diff --git a/src/utils.js b/src/utils.js index 705321e..7651f1b 100644 --- a/src/utils.js +++ b/src/utils.js @@ -1,20 +1,17 @@ -export const validUser = 'ash_ketchum' -export const validPass = 'pikachu123' - export const getLocalStorageObj = (key, defaultVal) => { try{ const item = JSON.parse(localStorage.getItem(key)) if (item===null){ localStorage.setItem(key, JSON.stringify(defaultVal)) - console.log(`item ${key} is null, setting to ${defaultVal}`) + //console.log(`item ${key} is null, setting to ${defaultVal}`) return [defaultVal, false] }else{ - console.log(`item ${key} retrieved from localStorage`) + //console.log(`item ${key} retrieved from localStorage`) return [item, true] } } catch(error) { localStorage.setItem(key, JSON.stringify(defaultVal)) - console.log(error) + //console.log(error) console.log(`error getting item ${key} setting to ${defaultVal}`) return [defaultVal, false] } @@ -23,26 +20,17 @@ export const getLocalStorageObj = (key, defaultVal) => { export const checkValue = (key, expectedVal) => { try{ const item = localStorage.getItem(key) - console.log(item) + //console.log(item) if (item===expectedVal){ - console.log(`item ${key} matches the desired ${expectedVal} value`) + //console.log(`item ${key} matches the desired ${expectedVal} value`) }else{ - // localStorage.clear(); - console.log(`value ${key} did not match, clearing local storage`) + localStorage.clear(); + + //console.log(`value ${key} did not match, clearing local storage`) } } catch(error) { - // localStorage.clear(); - console.log(`value ${key} error, clearing local storage`) - console.log(error) - } -} - -export const checkLogin = (realUser, realPass) => { - const user = localStorage.getItem('user') - const pass = localStorage.getItem('pass') - if (((user===realUser) && (pass===realPass))){ - return true - } else{ - return false + localStorage.clear(); + //console.log(`value ${key} error, clearing local storage`) + //console.log(error) } } \ No newline at end of file