Skip to content

Commit

Permalink
fix client for teachers
Browse files Browse the repository at this point in the history
  • Loading branch information
TuuKeZu committed Jan 7, 2025
1 parent e46de39 commit 8df3575
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 9 deletions.
15 changes: 15 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
"jest": "^27.4.3",
"jest-resolve": "^27.4.2",
"jest-watch-typeahead": "^1.0.0",
"jwt-decode": "^4.0.0",
"mini-css-extract-plugin": "^2.4.5",
"miragejs": "^0.1.40",
"npm": "^9.1.2",
Expand Down
24 changes: 19 additions & 5 deletions src/compontents/Frontpage/Frontpage.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,9 +63,12 @@ export default function Frontpage() {
const initialize = () => {
dispatch(getMessages({auth: auth.token, path: 'inbox'}))
dispatch(getNews({auth: auth.token, path: 'current'}))
dispatch(getGradebook({auth: auth.token}))
dispatch(getYOResults({auth: auth.token}))
dispatch(getMonth({auth: auth.token}))

if (auth.isStudent) {
dispatch(getGradebook({auth: auth.token}))
dispatch(getYOResults({auth: auth.token}))
}

loadSchedule();
}
Expand Down Expand Up @@ -100,7 +103,12 @@ export default function Frontpage() {
<div className={styles['side-bar']}>
<div className={styles['side-bar-content']}>
<h5 onClick={() => loadSchedule()} className={category == 'schedule' ? styles['selected'] : null}>Työjärjestys</h5>
<h5 onClick={() => loadHomework()} className={category == 'homework' ? styles['selected'] : null}>Kotitehtävät</h5>
{
auth.isStudent ?
<h5 onClick={() => loadHomework()} className={category == 'homework' ? styles['selected'] : null}>Kotitehtävät</h5>
:
null
}
</div>
</div>
<div className={styles['schedule']} onMouseEnter={() => setHover(true)} onMouseLeave={() => setHover(false)} onDoubleClick={() => setOpen(true)}>
Expand All @@ -119,10 +127,16 @@ export default function Frontpage() {
</div>
<div className={styles['bottom-container']}>
<div className={styles['left']}>
<div className={styles['grades']}>
{
auth.isStudent ?
<div className={styles['grades']}>
<YoResultsObject />
<GradeList />
</div>
</div>
:
null
}

</div>
<div className={styles['middle']}>
<div className={styles["links"]}>
Expand Down
9 changes: 6 additions & 3 deletions src/compontents/Navbar/Navbar.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { Link, useNavigate } from 'react-router-dom';
import { useSelector, useDispatch } from 'react-redux';
import { getConfig, useConfig } from '../../features/themes/configSlice';
import { useGrades } from '../../features/grades/gradeSlice';
import { logoutFromWilma } from '../../features/authentication/authSlice';
import { logoutFromWilma, useAuth } from '../../features/authentication/authSlice';

import styles from './Navbar.module.css'

Expand All @@ -12,6 +12,7 @@ export default function Navbar() {
const [expanded, setExpanded] = useState(false);
const config = useSelector(useConfig);
const grades = useSelector(useGrades);
const auth = useSelector(useAuth);
const dispatch = useDispatch();
const navigate = useNavigate();

Expand All @@ -22,7 +23,6 @@ export default function Navbar() {
'/friends': 'Kaverit',
'/maps': 'Kartat',
'/news': 'Tiedotteet',
'/teachers': 'Opettajat',
'/settings': 'Asetukset'
}

Expand Down Expand Up @@ -77,7 +77,7 @@ export default function Navbar() {
<div className={styles['user-info']}>
<div className={styles['user-data']}>
<h1>{config.value ? username(config.value['username']) : '...'}</h1>
<h2>Opiskelija</h2>
<h2>{auth.isStudent ? 'Opiskelija' : auth.isTeacher ? 'Opettaja' : '?'}</h2>
<div className={styles['logout']} id="logout">
<a onClick={() => dispatch(logoutFromWilma())}>Kirjaudu ulos</a>
</div>
Expand All @@ -87,6 +87,9 @@ export default function Navbar() {
<Link className={styles['logo-text']} to={'/'} onClick={onRedirect}><h1>OtaWilma</h1></Link>
{grades.yoResults.length > 0 ? <Link to={'/yo-results'} ><h5>Ylioppilaskirjoitukset</h5></Link> : null}
{Object.keys(links).slice(0, count).map(href => {
if (auth.isTeacher && href == '/grades' || href == '/tray' || href == '/friends') {
return null;
}
return <Link onClick={onRedirect} to={href}><h5>{links[href]}</h5></Link>
})}
{(expanded || count < 9) ? <button onClick={onExpand} className={styles['expand']}>{<h1>...</h1>}</button> : null}
Expand Down
12 changes: 11 additions & 1 deletion src/features/authentication/authSlice.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import { handleError } from '../errors/errorSlice';
import crypto from 'crypto-js';

import config from '../../config.json';
import { jwtDecode } from 'jwt-decode';
const { signature } = config;

export const loginToWilma = createAsyncThunk(
Expand Down Expand Up @@ -66,6 +67,12 @@ const getToken = () => {
return getCookie('token') ? (getCookie('token') === 'null' ? null : getCookie('token')) : null;
}

const getTokenType = () => {
const token = getToken();
const payload = jwtDecode(token);
return payload.isTeacher ?? false;
}

export const getAgreement = () => {
const agreement = window.localStorage.getItem('agreement');
if(!agreement) window.localStorage.setItem('agreement', false);
Expand Down Expand Up @@ -110,6 +117,7 @@ export const authSlice = createSlice({
name: 'auth',
initialState: {
token: getToken(),
isTeacher: getTokenType(),
loggedIn: !(!getToken()),
loginError: null,
isLoading: false,
Expand Down Expand Up @@ -161,7 +169,9 @@ export const useAuth = (state) => ({
token: state.auth.token,
loggedIn: state.auth.loggedIn,
loginError: state.auth.loginError,
isLoading: state.auth.isLoading
isLoading: state.auth.isLoading,
isTeacher: state.auth.isTeacher,
isStudent: !state.auth.isTeacher,
});

export default authSlice.reducer;

0 comments on commit 8df3575

Please sign in to comment.