diff --git a/src/Components/Header.js b/src/Components/Header.js index b607b2013755d9a6c2d975980e937ab3b2dbfb42..f721a372ad0f1794c5a0416bb7fea097b0e3f8b9 100644 --- a/src/Components/Header.js +++ b/src/Components/Header.js @@ -18,7 +18,7 @@ along with Plataforma Integrada MEC. If not, see <http://www.gnu.org/licenses/> import React, { useContext, useState, useEffect } from 'react' import axios from 'axios' -import {apiUrl} from '../env' +import { apiUrl } from '../env' import MenuBar from './MenuBar' import SearchBar from './SearchBar' import SignUpModal from './SignUpModal' @@ -27,7 +27,7 @@ import { Store } from '../Store'; import ColaborarModal from './ColaborarModal.js' import Snackbar from '@material-ui/core/Snackbar'; import MuiAlert from '@material-ui/lab/Alert'; -import {useLocation} from 'react-router-dom' +import { useLocation } from 'react-router-dom' import MenuBarMobile from './MenuBarMobile.js' //const StyledButton = styled(Button)` @@ -35,149 +35,149 @@ import MenuBarMobile from './MenuBarMobile.js' // boxShadow :none; //` function Alert(props) { - return <MuiAlert elevation={6} variant="filled" {...props} />; + return <MuiAlert elevation={6} variant="filled" {...props} />; } -export default function Header(props){ - const { state, dispatch } = useContext(Store) - const [signUpOpen, setSignUp] = useState(false) - const [loginOpen, setLogin] = useState(false) - const [successfulLoginOpen, handleSuccessfulLogin] = useState(false) - const [modalColaborar, setModalColaborar] = useState(false) - - const validateToken = (config) => { - axios.get(`${apiUrl}/auth/validate_token/`, config).then( - (response) => { - console.log(response.data) - if ( response.headers['access-token'] ) { - sessionStorage.setItem('@portalmec/accessToken', response.headers['access-token']) - } - dispatch ({ - type: "USER_LOGGED_IN", - userLoggedIn: !state.userIsLoggedIn, - login: response.data.data - } - ) - - sessionStorage.setItem('@portalmec/id', response.data.data.id) - sessionStorage.setItem('@portalmec/username', response.data.data.name) - sessionStorage.setItem('@portalmec/uid', response.data.data.uid) - - - let headers = { - client:response.headers.client, - "access-token":response.headers['access-token'], - "token-type": "Bearer", - expiry:response.headers.expiry, - uid:response.data.data.uid - } - sessionStorage.setItem('@portalmec/auth_headers', JSON.stringify(headers)) - }, - (err) => { - console.log(err) - } - ) - } - - useEffect( () => { - if (sessionStorage.getItem('@portalmec/auth_headers')) { - let config = { headers : JSON.parse(sessionStorage.getItem('@portalmec/auth_headers'))} - - validateToken(config) - } - }, []) - - useEffect ( () => { - if (state.currentUser.askTeacherQuestion === true) { - dispatch({ - type: "TOGGLE_MODAL_COLABORAR_PLATAFORMA", - modalColaborarPlataformaOpen: true - }); - } - }, [state.currentUser.askTeacherQuestion]) - - const redirect = () => { - props.history.push('/') - } - - let loc = useLocation() - useEffect(() => { - let query = new URLSearchParams(loc.search) - - if(query.get("auth_token")) { - let config = { - headers : { - "access-token" : query.get("auth_token"), - "client" : query.get("client_id"), - "uid" : query.get("uid"), - "expiry" : query.get("expiry"), - "token-type" : 'Bearer' - } - } - validateToken(config) - redirect() - } - - }, [loc]) - - const toggleSnackbar = (event, reason) => { - if (reason === 'clickaway') { - return; - } - - handleSuccessfulLogin(false); - } - - const handleSignUp = () => { - setSignUp(!signUpOpen) - } - - const handleLogin = () => { - setLogin(!loginOpen) - } - - const handleClickSearch = (open) => { - dispatch({ - type: "HANDLE_SEARCH_BAR", - opened: !state.searchOpen - }) - } - - let windowWidth = window.innerWidth - - return ( - <React.Fragment> - {/* +export default function Header(props) { + const { state, dispatch } = useContext(Store) + const [signUpOpen, setSignUp] = useState(false) + const [loginOpen, setLogin] = useState(false) + const [successfulLoginOpen, handleSuccessfulLogin] = useState(false) + const [modalColaborar, setModalColaborar] = useState(false) + + const validateToken = (config) => { + axios.get(`${apiUrl}/auth/validate_token/`, config).then( + (response) => { + console.log(response.data) + if (response.headers['access-token']) { + sessionStorage.setItem('@portalmec/accessToken', response.headers['access-token']) + } + dispatch({ + type: "USER_LOGGED_IN", + userLoggedIn: !state.userIsLoggedIn, + login: response.data.data + } + ) + + sessionStorage.setItem('@portalmec/id', response.data.data.id) + sessionStorage.setItem('@portalmec/username', response.data.data.name) + sessionStorage.setItem('@portalmec/uid', response.data.data.uid) + + + let headers = { + client: response.headers.client, + "access-token": response.headers['access-token'], + "token-type": "Bearer", + expiry: response.headers.expiry, + uid: response.data.data.uid + } + sessionStorage.setItem('@portalmec/auth_headers', JSON.stringify(headers)) + }, + (err) => { + console.log(err) + } + ) + } + + useEffect(() => { + if (sessionStorage.getItem('@portalmec/auth_headers')) { + let config = { headers: JSON.parse(sessionStorage.getItem('@portalmec/auth_headers')) } + + validateToken(config) + } + }, []) + + useEffect(() => { + if (state.currentUser.askTeacherQuestion === true) { + dispatch({ + type: "TOGGLE_MODAL_COLABORAR_PLATAFORMA", + modalColaborarPlataformaOpen: true + }); + } + }, [state.currentUser.askTeacherQuestion]) + + const redirect = () => { + props.history.push('/') + } + + let loc = useLocation() + useEffect(() => { + let query = new URLSearchParams(loc.search) + + if (query.get("auth_token")) { + let config = { + headers: { + "access-token": query.get("auth_token"), + "client": query.get("client_id"), + "uid": query.get("uid"), + "expiry": query.get("expiry"), + "token-type": 'Bearer' + } + } + validateToken(config) + redirect() + } + + }, [loc]) + + const toggleSnackbar = (event, reason) => { + if (reason === 'clickaway') { + return; + } + + handleSuccessfulLogin(false); + } + + const handleSignUp = () => { + setSignUp(!signUpOpen) + } + + const handleLogin = () => { + setLogin(!loginOpen) + } + + const handleClickSearch = (open) => { + dispatch({ + type: "HANDLE_SEARCH_BAR", + opened: !state.searchOpen + }) + } + + let windowWidth = window.innerWidth + + return ( + <React.Fragment> + {/* windowWidth > 990 && <AcessibilityBar/> */} - <Snackbar open={successfulLoginOpen} autoHideDuration={1000} onClose={toggleSnackbar} - anchorOrigin = {{ vertical:'top', horizontal:'center' }} - > - <Alert severity="success" style={{backgroundColor:"#00acc1"}}>Você está conectado(a)!</Alert> - </Snackbar> - { - windowWidth > 990 ? - ( - <React.Fragment> - <MenuBar openSearchBar={handleClickSearch} openSignUp = {handleSignUp} openLogin = {handleLogin}/> - { - state.searchOpen && - <SearchBar/> - } - </React.Fragment> - - ) - : - ( - <MenuBarMobile openSignUp = {handleSignUp} openLogin = {handleLogin}/> - ) - } - <SignUpModal open={signUpOpen} handleClose={handleSignUp} openLogin={handleLogin}/> - <LoginModal open={loginOpen} handleClose={() => setLogin(false)} openSignUp={handleSignUp} - openSnackbar={() => {handleSuccessfulLogin(true)}}/> - <ColaborarModal open={modalColaborar} handleClose={() => {setModalColaborar(!modalColaborar)}} /> - </React.Fragment> - ) + <Snackbar open={successfulLoginOpen} autoHideDuration={1000} onClose={toggleSnackbar} + anchorOrigin={{ vertical: 'top', horizontal: 'center' }} + > + <Alert severity="success" style={{ backgroundColor: "#00acc1" }}>Você está conectado(a)!</Alert> + </Snackbar> + { + windowWidth > 990 ? + ( + <React.Fragment> + <MenuBar openSearchBar={handleClickSearch} openSignUp={handleSignUp} openLogin={handleLogin} /> + { + state.searchOpen && + <SearchBar /> + } + </React.Fragment> + + ) + : + ( + <MenuBarMobile openSignUp={handleSignUp} openLogin={handleLogin} /> + ) + } + <SignUpModal open={signUpOpen} handleClose={handleSignUp} openLogin={handleLogin} /> + <LoginModal open={loginOpen} handleClose={() => setLogin(false)} openSignUp={handleSignUp} + openSnackbar={() => { handleSuccessfulLogin(true) }} /> + <ColaborarModal open={modalColaborar} handleClose={() => { setModalColaborar(!modalColaborar) }} /> + </React.Fragment> + ) }