Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

#135 Tela inicial de aluno #21

Merged
merged 6 commits into from
May 4, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions app/src/components/LayoutApp/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@ import { BtnEdition, Container, End, Main, ProfessorSearchStyle } from './styles
import Menu from '../Menu';
import Input from '../Input';
import Button from '../Button';
import {isStudent, logOut } from '../../services/Auth';
import {logOut } from '../../services/Auth';
import Users from '../../services/Users';
import { useHistory } from 'react-router';
import MenuOptions from '../MenuOptions';

Expand Down Expand Up @@ -32,7 +33,7 @@ export default function LayoutApp({ children }) {
if (menuOptions === "") {
return (setMenuOptions(
<MenuOptions>
<Button type='button' backColor='#FFD54F' text='CONFIGURAR' padding='3px' onClick={() => {setMenuOptions(""); if(isStudent()) history.push('/user/profile')}}/>
<Button type='button' backColor='#FFD54F' text='CONFIGURAR' padding='3px' onClick={() => {setMenuOptions(""); if(Users.STUDENT.isAuthenticated()) history.push('/user/profile')}}/>
<Button backColor='#FFD54F' text='SOBRE' padding='3px' onClick={() => console.log("sobre")} />
<Button backColor='#FFD54F' text='SAIR' padding='3px' onClick={() => { logOut(); history.push('/') }} />
</MenuOptions>
Expand Down
6 changes: 4 additions & 2 deletions app/src/components/Menu/index.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
import React from 'react';
import Name_Logo from "../../assets/images/Name_Logo.png";
import { MenuBar, Logo, ImageLogo } from './styles.js';

import Users from '../../services/Users'
import { useHistory } from 'react-router';

export default function Menu({children, background}) {
const history = useHistory();
return (
<MenuBar background={background}>
<Logo>
<ImageLogo src={Name_Logo} alt="logo" />
<ImageLogo src={Name_Logo} alt="logo" onClick={() => history.push(Users.whoAuthenticated().homePath)}/>
</Logo>
{children}
</MenuBar>
Expand Down
10 changes: 5 additions & 5 deletions app/src/components/Post/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import { PostStyle, HeaderPost, InfoStudent, Name, Rating, ContentPost, Feedback
import api from '../../services/Api'
import Report from '../Report';
import Popup from '../Popup';
import { isProfessor, isStudent} from '../../services/Auth';
import Users from '../../services/Users';

const Header = ({ post, onClickReport }) => {
return (
Expand Down Expand Up @@ -39,21 +39,21 @@ const Content = ({ children }) => {


const Feedbacks = ({ post, Onclick }) => {
const isAgreed = post.feedbacks.is_agreed || isProfessor();
const isDisagreed = post.feedbacks.is_disagreed || isProfessor();
const isAgreed = post.feedbacks.is_agreed || Users.PROFESSOR.isAuthenticated();
const isDisagreed = post.feedbacks.is_disagreed || Users.PROFESSOR.isAuthenticated();
const countAgrees = post.feedbacks.agrees;
const countDisagrees = post.feedbacks.disagrees;

function clickAgree() {
if(isStudent()){
if(Users.STUDENT.isAuthenticated()){
const body = { 'id_post': post.id_post }
api.post("/post/agree", body)
.then(res => Onclick(res.data))
}
}

function clickDisagree() {
if(isStudent()){
if(Users.STUDENT.isAuthenticated()){
const body = { 'id_post': post.id_post }
api.post("/post/disagree", body)
.then(res => Onclick(res.data))
Expand Down
16 changes: 10 additions & 6 deletions app/src/routes/routes.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,13 @@ import RegisterStudent from '../views/RegisterStudent';
import Login from '../views/Login';
import RegisterProfessor from '../views/RegisterProfessor';
import ProfessorSearch from '../views/ProfessorSearch';
import { isAuthenticated} from '../services/Auth';
import LayoutAutentication from '../components/LayoutAutentication';
import LayoutApp from '../components/LayoutApp';
import Profile from '../views/Profile';
import Home from '../views/Home';
import StudentHome from '../views/StudentHome';
import Users from '../services/Users';

import {
BrowserRouter as Router,
Switch,
Expand All @@ -15,12 +17,13 @@ import {
} from "react-router-dom";



const PrivateRoute = ({ component: Component, authFunction, ...rest }) => (
<Route {...rest} render={props => (
authFunction() ? (
<Component {...props} />
) : (
<Redirect to={{ pathname: '/visitant/login', state: { from: props.location } }} />
<Redirect to={{ pathname: Users.whoAuthenticated().homePath, state: { from: props.location } }} />
)
)} />
);
Expand All @@ -29,8 +32,8 @@ const User = () => {
return (
<LayoutApp>
<Switch>
<Route path="/user/professor/search/:professorName" component={ProfessorSearch} authFunction={isAuthenticated} />
<Route path="/user/profile" component={Profile} authFunction={isAuthenticated} />
<Route path="/user/professor/search/:professorName" component={ProfessorSearch} authFunction={Users.isAuthenticated} />
<Route path="/user/profile" component={Profile} authFunction={Users.isAuthenticated} />
</Switch>
</LayoutApp>
)
Expand All @@ -53,6 +56,7 @@ const Student = () => {
<LayoutApp>
<Switch>
{/* <PrivateRouteStudent path="/student/home" component={...} /> */}
<PrivateRoute path="/student/" authFunction={Users.STUDENT.isAuthenticated} component={StudentHome} />
</Switch>
</LayoutApp>
)
Expand All @@ -71,8 +75,8 @@ const Routes = () => (
<Switch>
<Route exact path='/' component={Home} />
<Route path="/visitant/"><Visitant /></Route>
<PrivateRoute path="/user/" authFunction={isAuthenticated} component={User} />
<Route path="/student/"><Student /></Route>
<PrivateRoute path="/user/" authFunction={Users.isAuthenticated} component={User} />
<PrivateRoute path="/student/" authFunction={Users.STUDENT.isAuthenticated} ><Student /></PrivateRoute>
<Route path="/professor/"><Professor /></Route>
</Switch>
</Router>
Expand Down
25 changes: 5 additions & 20 deletions app/src/services/Auth.js
Original file line number Diff line number Diff line change
@@ -1,20 +1,5 @@
import api from './Api'

export function isAuthenticated(){
return getToken() !== undefined;
}

export function isProfessor(){
if(getToken()){
return localStorage.getItem('professor') ? true : false;
}
}

export function isStudent(){
if(getToken()){
return localStorage.getItem('student') ? true : false;
}
}
import users from './Users'

export function getToken() {
return (localStorage.getItem('access_token'));
Expand All @@ -29,9 +14,9 @@ export async function sendLogin(email, password, callback, errorCallback) {
function writeUser(data){
localStorage.setItem('access_token', data.access_token);
if (data.user.reg_student)
localStorage.setItem('student', JSON.stringify(data.user));
localStorage.setItem(users.STUDENT.localStorageName, JSON.stringify(data.user));
else
localStorage.setItem('professor', JSON.stringify(data.user));
localStorage.setItem(users.PROFESSOR.localStorageName, JSON.stringify(data.user));
}

api.post("/login", body)
Expand All @@ -48,6 +33,6 @@ export async function sendLogin(email, password, callback, errorCallback) {

export function logOut() {
localStorage.removeItem('access_token');
localStorage.removeItem('student');
localStorage.removeItem('professor');
Object.keys(users).forEach((userKey) => {
localStorage.removeItem(users[userKey]?.localStorageName)})
}
27 changes: 27 additions & 0 deletions app/src/services/Users.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import { getToken } from './Auth';

const createUser = (localStorageName, homePath) => {
return {
localStorageName: localStorageName,
homePath: homePath,
isAuthenticated: () => getToken() ? localStorage.getItem(localStorageName) ? true : false : false
}
}

const Users = {
PROFESSOR: createUser("professor", '/professor/'),
STUDENT: createUser('student', '/student/'),
VISITANT: createUser('visitant', '/'),
isAuthenticated: () => getToken() !== null,
whoAuthenticated: function () {
if (getToken()) {
const userName = Object.keys(this).find((user) =>
localStorage.getItem(this[user]?.localStorageName) !== null
)
return this[userName];
}
return this.VISITANT;
}
}

export default Users;
5 changes: 3 additions & 2 deletions app/src/views/Login/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ import schema from "./validations"
import Button from "../../components/Button";
import Form from "../../components/Form";
import Input from "../../components/Input";
import { sendLogin, logOut, getToken, isProfessor } from '../../services/Auth';
import { sendLogin, logOut, getToken} from '../../services/Auth';
import Users from '../../services/Users';
import {Content, Conteiner, Erro} from './styles';

const Header = ({ children }) => {
Expand Down Expand Up @@ -35,7 +36,7 @@ export default function Login() {
setCursor("wait");
sendLogin(data.email, data.password, () => {
if (getToken()){
let home = isProfessor() ? 'professor' : 'student';
let home = Users.PROFESSOR.isAuthenticated() ? 'professor' : 'student';
history.push("/" + home);
}
}, () => {
Expand Down
4 changes: 2 additions & 2 deletions app/src/views/ProfessorSearch/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import api from '../../services/Api';
import Loading from '../../components/Loading';
import { Container, FoundDiv, Img, FoundHeader, Name, Discipline, LoadingBox } from './styles'
import Btn_options from '../../assets/images/Btn_options.png'
import { isStudent } from '../../services/Auth';
import Users from '../../services/Users';

function ProfessorSearch() {
const { professorName } = useParams();
Expand Down Expand Up @@ -72,7 +72,7 @@ function ProfessorSearch() {
}

<Feed title={professor ? `${professor.name}` : "Sem Resultados"} radius="0px 0px 10px 10px">
<Feed.Header professor={professor} feedbacks={feedbacks} canAvaliate={isStudent()} onNewAvaliation={() => setNewAvaliationState(!newAvaliationState)}/>
<Feed.Header professor={professor} feedbacks={feedbacks} canAvaliate={Users.STUDENT.isAuthenticated()} onNewAvaliation={() => setNewAvaliationState(!newAvaliationState)}/>
{professors.length > 0 && <Feed.Title backColor="#26A69A" >{posts.length === 0 && !loading ? "Sem Avaliações Ainda" : "Avaliações"}</Feed.Title>}
{!loading && <Feed.PostsBox posts={posts} key={posts.length}/>}
{loading && professors.length > 0 && <LoadingBox><Loading /></LoadingBox>}
Expand Down
21 changes: 21 additions & 0 deletions app/src/views/StudentHome/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import React from 'react';
import Feed from '../../components/Feed'
import api from '../../services/Api';

export default function StudentHome() {
const [posts, setPosts] = React.useState([])

React.useEffect(() => {
api.get("/post")
.then(response => {
if(response.status == 200)
setPosts(response.data)
})
}, [])

return (
<Feed title="Avaliações Feitas Por Você">
<Feed.PostsBox posts={posts} canReport={true} />
</Feed>
);
}
Empty file.