import React, { useState, useEffect, useReducer } from 'react';
import { BrowserRouter as Router, Switch, Route, Link, useParams } from 'react-router-dom';
import './light.css';
import { Button, Container, Divider, Dropdown, Form, Grid, Header, Icon, Image, Menu, Message, Segment, Table } from 'semantic-ui-react';
import moment from 'moment-timezone';
import { isAdmin, isInstructor, getInstructor, BasicTable, requester } from './utils.js';
import { NotFound, PleaseLogin } from './Misc.js';
import { InstructorClassDetail, InstructorClassAttendance } from './InstructorClasses.js';
import { PayPalPayNow } from './PayPal.js';
function ClassTable(props) {
const { classes } = props;
return (
Name
Date
Time
Instructor
Cost
Students
{classes.length ?
classes.map(x =>
{x.course_name}
{moment.utc(x.datetime).tz('America/Edmonton').format('ll')}
{x.is_cancelled ? 'Cancelled' : moment.utc(x.datetime).tz('America/Edmonton').format('LT')}
{getInstructor(x)}
{x.cost === '0.00' ? 'Free' : '$'+x.cost}
{x.student_count} {!!x.max_students && '/ '+x.max_students}
)
:
None
}
);
};
let classesCache = false;
export function Classes(props) {
const [classes, setClasses] = useState(classesCache);
const { token } = props;
useEffect(() => {
requester('/sessions/', 'GET', token)
.then(res => {
setClasses(res.results);
classesCache = res.results;
})
.catch(err => {
console.log(err);
});
}, []);
const now = new Date().toISOString();
return (
Ordered by nearest date.
{classes ?
x.datetime > now).sort((a, b) => a.datetime > b.datetime ? 1 : -1)} />
:
Loading...
}
Ordered by nearest date.
{classes ?
x.datetime < now)} />
:
Loading...
}
);
};
export function ClassDetail(props) {
const [clazz, setClass] = useState(false);
const [refreshCount, refreshClass] = useReducer(x => x + 1, 0);
const [error, setError] = useState(false);
const [loading, setLoading] = useState(false);
const { token, user, refreshUser } = props;
const { id } = useParams();
const userTraining = clazz && clazz.students.find(x => x.user == user.id);
useEffect(() => {
requester('/sessions/'+id+'/', 'GET', token)
.then(res => {
setClass(res);
})
.catch(err => {
console.log(err);
setError(true);
});
}, [refreshCount]);
const handleSignup = () => {
if (loading) return;
setLoading(true);
const data = { attendance_status: 'Waiting for payment', session: id };
requester('/training/', 'POST', token, data)
.then(res => {
refreshClass();
refreshUser();
})
.catch(err => {
console.log(err);
});
};
const handleToggle = (newStatus) => {
if (loading) return;
setLoading(true);
const data = { attendance_status: newStatus, session: id };
requester('/training/'+userTraining.id+'/', 'PUT', token, data)
.then(res => {
refreshClass();
refreshUser();
})
.catch(err => {
console.log(err);
setError(true);
});
};
useEffect(() => {
setLoading(false);
}, [userTraining]);
// TODO: calculate yesterday and lock signups
return (
{!error ?
clazz ?
{(isAdmin(user) || clazz.instructor === user.id) &&
}
Name:
{clazz.course_name}
Date:
{moment.utc(clazz.datetime).tz('America/Edmonton').format('ll')}
Time:
{clazz.is_cancelled ? 'Cancelled' : moment.utc(clazz.datetime).tz('America/Edmonton').format('LT')}
Instructor:
{getInstructor(clazz)}
Cost:
{clazz.cost === '0.00' ? 'Free' : '$'+clazz.cost}
Students:
{clazz.student_count} {!!clazz.max_students && '/ '+clazz.max_students}
{(isAdmin(user) || clazz.instructor === user.id) &&
}
{clazz.instructor != user.id &&
(userTraining ?
Status: {userTraining.attendance_status}
{userTraining.attendance_status === 'Withdrawn' ?
:
}
{userTraining.attendance_status === 'Waiting for payment' &&
Please pay the course fee of ${clazz.cost} to confirm your attendance.
}
:
(clazz.is_cancelled ?
The class is cancelled.
:
((clazz.max_students && clazz.student_count >= clazz.max_students) ?
The class is full.
:
)
)
)
}
:
Loading...
:
}
);
};