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

new Error Dialog for login #11

Merged
merged 1 commit into from
Oct 11, 2023
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
2 changes: 0 additions & 2 deletions frontend/src/Router.jsx
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
import React from 'react';
import { render } from "react-dom";
import {
Routes,
Route,
useNavigate,
} from 'react-router-dom';

import Site from './page/Site';
Expand Down
43 changes: 31 additions & 12 deletions frontend/src/page/Login.jsx
Original file line number Diff line number Diff line change
@@ -1,20 +1,15 @@
import React from 'react';
import PropTypes from 'prop-types';
import Avatar from '@material-ui/core/Avatar';
import Button from '@material-ui/core/Button';
import CssBaseline from '@material-ui/core/CssBaseline';
import Radio from '@mui/material/Radio';
import RadioGroup from '@mui/material/RadioGroup';
import FormControlLabel from '@mui/material/FormControlLabel';
import FormControl from '@mui/material/FormControl';

import { Dialog, DialogTitle, DialogContent, DialogContentText, DialogActions} from '@mui/material';
import FormLabel from '@mui/material/FormLabel';
import Checkbox from '@material-ui/core/Checkbox';
import Input from '@material-ui/core/Input';
import MenuItem from '@mui/material/MenuItem';
import Select from '@material-ui/core/Select';
import InputLabel from '@material-ui/core/InputLabel';
import LockOutlinedIcon from '@material-ui/icons/LockOutlined';
import Paper from '@material-ui/core/Paper';
import Typography from '@material-ui/core/Typography';
import withStyles from '@material-ui/core/styles/withStyles';
Expand All @@ -25,7 +20,6 @@ import { useCookies } from 'react-cookie';
import AppLoad from '../component/AppLoad';
import { apiCall } from '../util/api';
import config from '../config';
import makePage from '../component/makePage';
import { Context, useContext } from '../context';

/* Sourced https://github.com/mui-org/material-ui/blob/v3.x/docs/src/pages/getting-started/page-layout-examples/sign-in/SignIn.js */
Expand Down Expand Up @@ -72,13 +66,25 @@ const styles = theme => ({
});

const SignIn = (props) => {
const { getters, setters } = useContext(Context);
// dialog state
const [open, setOpen] = React.useState(false);
const { getters } = useContext(Context);
const [zid, setZid] = React.useState('');
const [zpass, setZpass] = React.useState('');
const [term, setTerm] = React.useState(['sample']);
const [cookies, setCookie] = useCookies();
const [cookies] = useCookies();
const [loading, setLoading] = React.useState(false);
const navigate = useNavigate();
const [errorMessage, setErrorMessage] = React.useState('');

const handleClickOpen = (message) => {
setErrorMessage(message); // Set the error message
setOpen(true);
};

const handleClose = () => {
setOpen(false);
};

React.useEffect(() => {
if (cookies.eckles_loggedin) {
Expand All @@ -92,7 +98,7 @@ const SignIn = (props) => {

const login = (zid, zpass, term) => {
setLoading(true);
apiCall('login', { term, zid, zpass })
apiCall('login', { term, zid, zpass }, 'POST', handleClickOpen)
.then(_ => {
localStorage.removeItem('eckles_content');
localStorage.removeItem('eckles_expiry');
Expand All @@ -106,12 +112,12 @@ const SignIn = (props) => {
if (loading) {
return <AppLoad />;
}

return (
<main className={classes.main}>
<CssBaseline />
<Paper className={classes.paper}>
<img className={classes.logo} src={mainlogo} />
<img className={classes.logo} src={mainlogo} alt='mainlogo'/>
<Typography component="h1" variant="h5">
Sign in to COMP6080
</Typography>
Expand Down Expand Up @@ -157,6 +163,19 @@ const SignIn = (props) => {
</Button>
</form>
</Paper>
<Dialog open={open} onClose={handleClose}>
<DialogTitle>{"Error"}</DialogTitle>
<DialogContent>
<DialogContentText>
{errorMessage}
</DialogContentText>
</DialogContent>
<DialogActions>
<Button onClick={handleClose} color="primary">
Close
</Button>
</DialogActions>
</Dialog>
</main>
);
}
Expand Down
5 changes: 3 additions & 2 deletions frontend/src/util/api.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import config from '../config';

export const apiCall = (path, data, type) => {
export const apiCall = (path, data, type, callback) => {
return new Promise((resolve, reject) => {
fetch(`${config.BASE_URL}/api/${path}`, {
method: type ?? 'POST',
Expand All @@ -15,7 +15,8 @@ export const apiCall = (path, data, type) => {
return response.json().then(resolve);
} else if (response.status === 400) {
return response.json().then(obj => {
alert(obj.err);
// alert(obj.err);
callback(obj.err);
reject(obj.err);
});
} else {
Expand Down
Loading