2020-01-18 01:27:56 +00:00
|
|
|
import React, { useState, useEffect, useReducer } from 'react';
|
2020-01-10 06:24:37 +00:00
|
|
|
import { BrowserRouter as Router, Switch, Route, Link, useParams } from 'react-router-dom';
|
|
|
|
import './light.css';
|
2020-01-16 09:09:45 +00:00
|
|
|
import { Button, Container, Divider, Dropdown, Form, Grid, Header, Icon, Image, Menu, Message, Segment, Table } from 'semantic-ui-react';
|
2020-02-24 04:07:18 +00:00
|
|
|
import moment from 'moment-timezone';
|
2020-01-16 09:09:45 +00:00
|
|
|
import { isAdmin, isInstructor, BasicTable, requester } from './utils.js';
|
2020-01-10 06:24:37 +00:00
|
|
|
import { NotFound, PleaseLogin } from './Misc.js';
|
2020-01-16 09:09:45 +00:00
|
|
|
import { InstructorClassDetail, InstructorClassAttendance } from './InstructorClasses.js';
|
2020-02-21 06:10:52 +00:00
|
|
|
import { PayPalPayNow } from './PayPal.js';
|
2020-01-10 06:24:37 +00:00
|
|
|
|
2020-01-10 07:39:05 +00:00
|
|
|
function ClassTable(props) {
|
|
|
|
const { classes } = props;
|
|
|
|
|
|
|
|
return (
|
|
|
|
<Table basic='very'>
|
|
|
|
<Table.Header>
|
|
|
|
<Table.Row>
|
|
|
|
<Table.HeaderCell>Name</Table.HeaderCell>
|
|
|
|
<Table.HeaderCell>Date</Table.HeaderCell>
|
2020-01-10 23:56:10 +00:00
|
|
|
<Table.HeaderCell>Time</Table.HeaderCell>
|
2020-01-10 07:39:05 +00:00
|
|
|
<Table.HeaderCell>Instructor</Table.HeaderCell>
|
|
|
|
<Table.HeaderCell>Cost</Table.HeaderCell>
|
|
|
|
<Table.HeaderCell>Students</Table.HeaderCell>
|
|
|
|
</Table.Row>
|
|
|
|
</Table.Header>
|
|
|
|
|
|
|
|
<Table.Body>
|
|
|
|
{classes.length ?
|
2020-01-18 01:36:53 +00:00
|
|
|
classes.map(x =>
|
|
|
|
<Table.Row key={x.id}>
|
2020-01-15 07:51:00 +00:00
|
|
|
<Table.Cell>{x.course_name}</Table.Cell>
|
2020-01-10 07:39:05 +00:00
|
|
|
<Table.Cell>
|
|
|
|
<Link to={'/classes/'+x.id}>
|
2020-02-24 04:07:18 +00:00
|
|
|
{moment.utc(x.datetime).tz('America/Edmonton').format('ll')}
|
2020-01-10 07:39:05 +00:00
|
|
|
</Link>
|
|
|
|
</Table.Cell>
|
2020-02-24 04:07:18 +00:00
|
|
|
<Table.Cell>{x.is_cancelled ? 'Cancelled' : moment.utc(x.datetime).tz('America/Edmonton').format('LT')}</Table.Cell>
|
2020-01-15 07:51:00 +00:00
|
|
|
<Table.Cell>{x.instructor_name}</Table.Cell>
|
2020-01-10 07:39:05 +00:00
|
|
|
<Table.Cell>{x.cost === '0.00' ? 'Free' : '$'+x.cost}</Table.Cell>
|
2020-02-14 22:35:24 +00:00
|
|
|
<Table.Cell>{x.student_count} {!!x.max_students && '/ '+x.max_students}</Table.Cell>
|
2020-01-10 07:39:05 +00:00
|
|
|
</Table.Row>
|
|
|
|
)
|
|
|
|
:
|
2020-01-19 08:59:16 +00:00
|
|
|
<Table.Row><Table.Cell>None</Table.Cell></Table.Row>
|
2020-01-10 07:39:05 +00:00
|
|
|
}
|
|
|
|
</Table.Body>
|
|
|
|
</Table>
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
2020-02-17 00:19:44 +00:00
|
|
|
let classesCache = false;
|
|
|
|
|
2020-01-10 06:24:37 +00:00
|
|
|
export function Classes(props) {
|
2020-02-17 00:19:44 +00:00
|
|
|
const [classes, setClasses] = useState(classesCache);
|
2020-01-10 06:24:37 +00:00
|
|
|
const { token } = props;
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
requester('/sessions/', 'GET', token)
|
|
|
|
.then(res => {
|
|
|
|
setClasses(res.results);
|
2020-02-17 00:19:44 +00:00
|
|
|
classesCache = res.results;
|
2020-01-10 06:24:37 +00:00
|
|
|
})
|
|
|
|
.catch(err => {
|
|
|
|
console.log(err);
|
|
|
|
});
|
|
|
|
}, []);
|
|
|
|
|
2020-01-10 07:39:05 +00:00
|
|
|
const now = new Date().toISOString();
|
2020-01-10 06:24:37 +00:00
|
|
|
|
|
|
|
return (
|
|
|
|
<Container>
|
2020-01-10 07:39:05 +00:00
|
|
|
<Header size='large'>Class List</Header>
|
2020-01-18 01:27:56 +00:00
|
|
|
|
|
|
|
<Header size='medium'>Upcoming</Header>
|
2020-01-10 06:24:37 +00:00
|
|
|
{classes ?
|
2020-01-10 07:39:05 +00:00
|
|
|
<ClassTable classes={classes.filter(x => x.datetime > now)} />
|
|
|
|
:
|
|
|
|
<p>Loading...</p>
|
|
|
|
}
|
|
|
|
|
|
|
|
<Header size='medium'>Recent</Header>
|
|
|
|
{classes ?
|
|
|
|
<ClassTable classes={classes.filter(x => x.datetime < now)} />
|
2020-01-10 06:24:37 +00:00
|
|
|
:
|
|
|
|
<p>Loading...</p>
|
|
|
|
}
|
|
|
|
</Container>
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
export function ClassDetail(props) {
|
|
|
|
const [clazz, setClass] = useState(false);
|
2020-01-18 01:27:56 +00:00
|
|
|
const [refreshCount, refreshClass] = useReducer(x => x + 1, 0);
|
2020-01-10 06:24:37 +00:00
|
|
|
const [error, setError] = useState(false);
|
2020-01-18 01:27:56 +00:00
|
|
|
const { token, user, refreshUser } = props;
|
2020-01-10 06:24:37 +00:00
|
|
|
const { id } = useParams();
|
2020-02-07 01:11:09 +00:00
|
|
|
const userTraining = clazz && clazz.students.find(x => x.user == user.id);
|
2020-01-10 06:24:37 +00:00
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
requester('/sessions/'+id+'/', 'GET', token)
|
|
|
|
.then(res => {
|
|
|
|
setClass(res);
|
|
|
|
})
|
|
|
|
.catch(err => {
|
|
|
|
console.log(err);
|
|
|
|
setError(true);
|
|
|
|
});
|
2020-01-18 01:27:56 +00:00
|
|
|
}, [refreshCount]);
|
2020-01-10 06:24:37 +00:00
|
|
|
|
2020-01-16 09:09:45 +00:00
|
|
|
const handleSignup = () => {
|
2020-02-07 01:11:09 +00:00
|
|
|
const data = { attendance_status: 'Waiting for payment', session: id };
|
2020-01-16 09:09:45 +00:00
|
|
|
requester('/training/', 'POST', token, data)
|
|
|
|
.then(res => {
|
2020-01-18 01:27:56 +00:00
|
|
|
refreshClass();
|
|
|
|
refreshUser();
|
2020-01-16 09:09:45 +00:00
|
|
|
})
|
|
|
|
.catch(err => {
|
|
|
|
console.log(err);
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
const handleToggle = (newStatus) => {
|
|
|
|
const data = { attendance_status: newStatus, session: id };
|
|
|
|
requester('/training/'+userTraining.id+'/', 'PUT', token, data)
|
|
|
|
.then(res => {
|
2020-01-18 01:27:56 +00:00
|
|
|
refreshClass();
|
|
|
|
refreshUser();
|
2020-01-16 09:09:45 +00:00
|
|
|
})
|
|
|
|
.catch(err => {
|
|
|
|
console.log(err);
|
|
|
|
setError(true);
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
// TODO: calculate yesterday and lock signups
|
|
|
|
|
2020-01-10 06:24:37 +00:00
|
|
|
return (
|
|
|
|
<Container>
|
|
|
|
{!error ?
|
|
|
|
clazz ?
|
|
|
|
<div>
|
2020-01-10 07:39:05 +00:00
|
|
|
<Header size='large'>Class Details</Header>
|
2020-01-10 06:24:37 +00:00
|
|
|
|
2020-02-16 07:17:44 +00:00
|
|
|
{(isAdmin(user) || clazz.instructor === user.id) &&
|
|
|
|
<Segment padded>
|
|
|
|
<InstructorClassDetail clazz={clazz} setClass={setClass} {...props} />
|
|
|
|
</Segment>
|
|
|
|
}
|
2020-01-16 01:32:52 +00:00
|
|
|
|
2020-01-11 23:53:42 +00:00
|
|
|
<BasicTable>
|
2020-01-10 06:24:37 +00:00
|
|
|
<Table.Body>
|
|
|
|
<Table.Row>
|
|
|
|
<Table.Cell>Name:</Table.Cell>
|
|
|
|
<Table.Cell>
|
2020-01-15 07:51:00 +00:00
|
|
|
<Link to={'/courses/'+clazz.course}>
|
|
|
|
{clazz.course_name}
|
2020-01-10 06:24:37 +00:00
|
|
|
</Link>
|
|
|
|
</Table.Cell>
|
|
|
|
</Table.Row>
|
|
|
|
<Table.Row>
|
|
|
|
<Table.Cell>Date:</Table.Cell>
|
|
|
|
<Table.Cell>
|
2020-02-24 04:07:18 +00:00
|
|
|
{moment.utc(clazz.datetime).tz('America/Edmonton').format('ll')}
|
2020-01-10 06:24:37 +00:00
|
|
|
</Table.Cell>
|
|
|
|
</Table.Row>
|
|
|
|
<Table.Row>
|
|
|
|
<Table.Cell>Time:</Table.Cell>
|
|
|
|
<Table.Cell>
|
2020-02-24 04:07:18 +00:00
|
|
|
{clazz.is_cancelled ? 'Cancelled' : moment.utc(clazz.datetime).tz('America/Edmonton').format('LT')}
|
2020-01-10 06:24:37 +00:00
|
|
|
</Table.Cell>
|
|
|
|
</Table.Row>
|
|
|
|
<Table.Row>
|
|
|
|
<Table.Cell>Instructor:</Table.Cell>
|
2020-01-15 07:51:00 +00:00
|
|
|
<Table.Cell>{clazz.instructor_name}</Table.Cell>
|
2020-01-10 06:24:37 +00:00
|
|
|
</Table.Row>
|
|
|
|
<Table.Row>
|
|
|
|
<Table.Cell>Cost:</Table.Cell>
|
|
|
|
<Table.Cell>{clazz.cost === '0.00' ? 'Free' : '$'+clazz.cost}</Table.Cell>
|
|
|
|
</Table.Row>
|
|
|
|
<Table.Row>
|
|
|
|
<Table.Cell>Students:</Table.Cell>
|
2020-02-14 22:35:24 +00:00
|
|
|
<Table.Cell>{clazz.student_count} {!!clazz.max_students && '/ '+clazz.max_students}</Table.Cell>
|
2020-01-10 06:24:37 +00:00
|
|
|
</Table.Row>
|
|
|
|
</Table.Body>
|
2020-01-11 23:53:42 +00:00
|
|
|
</BasicTable>
|
2020-01-10 06:24:37 +00:00
|
|
|
|
|
|
|
<Header size='medium'>Attendance</Header>
|
2020-01-16 09:09:45 +00:00
|
|
|
|
|
|
|
{(isAdmin(user) || clazz.instructor === user.id) &&
|
|
|
|
<Segment padded>
|
2020-01-18 01:36:53 +00:00
|
|
|
<InstructorClassAttendance clazz={clazz} refreshClass={refreshClass} {...props} />
|
2020-01-16 09:09:45 +00:00
|
|
|
</Segment>
|
|
|
|
}
|
|
|
|
|
|
|
|
{clazz.instructor != user.id &&
|
|
|
|
(userTraining ?
|
|
|
|
<div>
|
|
|
|
<p>Status: {userTraining.attendance_status}</p>
|
2020-02-09 06:59:22 +00:00
|
|
|
<p>
|
|
|
|
{userTraining.attendance_status === 'Withdrawn' ?
|
|
|
|
<Button onClick={() => handleToggle('Waiting for payment')}>
|
|
|
|
Sign back up
|
|
|
|
</Button>
|
|
|
|
:
|
|
|
|
<Button onClick={() => handleToggle('Withdrawn')}>
|
|
|
|
Withdraw from Class
|
|
|
|
</Button>
|
|
|
|
}
|
|
|
|
</p>
|
|
|
|
|
|
|
|
{userTraining.attendance_status === 'Waiting for payment' &&
|
|
|
|
<div>
|
|
|
|
<p>Please pay the course fee of ${clazz.cost} to confirm your attendance.</p>
|
2020-02-21 06:10:52 +00:00
|
|
|
<PayPalPayNow
|
2020-02-09 06:59:22 +00:00
|
|
|
amount={clazz.cost}
|
|
|
|
name={clazz.course_name}
|
|
|
|
custom={JSON.stringify({ training: userTraining.id })}
|
|
|
|
/>
|
|
|
|
</div>
|
2020-01-16 09:09:45 +00:00
|
|
|
}
|
|
|
|
</div>
|
|
|
|
:
|
2020-03-08 01:12:44 +00:00
|
|
|
(clazz.is_cancelled ?
|
|
|
|
<p>The class is cancelled.</p>
|
2020-01-16 09:09:45 +00:00
|
|
|
:
|
2020-03-08 01:12:44 +00:00
|
|
|
((clazz.max_students && clazz.student_count >= clazz.max_students) ?
|
|
|
|
<p>The class is full.</p>
|
|
|
|
:
|
|
|
|
<Button onClick={handleSignup}>
|
|
|
|
Sign me up!
|
|
|
|
</Button>
|
|
|
|
)
|
2020-01-16 09:09:45 +00:00
|
|
|
)
|
|
|
|
)
|
|
|
|
}
|
2020-01-10 06:24:37 +00:00
|
|
|
</div>
|
|
|
|
:
|
|
|
|
<p>Loading...</p>
|
|
|
|
:
|
|
|
|
<NotFound />
|
|
|
|
}
|
|
|
|
</Container>
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|