You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
171 lines
4.4 KiB
171 lines
4.4 KiB
4 years ago
|
import React, { useState, useEffect } from 'react';
|
||
|
import { BrowserRouter as Router, Switch, Route, Link, useParams, useHistory } from 'react-router-dom';
|
||
4 years ago
|
import * as Datetime from 'react-datetime';
|
||
|
import 'react-datetime/css/react-datetime.css';
|
||
|
import moment from 'moment';
|
||
4 years ago
|
import './light.css';
|
||
|
import { Button, Container, Checkbox, Divider, Dropdown, Form, Grid, Header, Icon, Image, Label, Menu, Message, Segment, Table } from 'semantic-ui-react';
|
||
|
import { BasicTable, staticUrl, requester } from './utils.js';
|
||
|
|
||
4 years ago
|
function InstructorClassEditor(props) {
|
||
|
const { input, setInput, error, editing } = props;
|
||
4 years ago
|
|
||
|
const handleValues = (e, v) => setInput({ ...input, [v.name]: v.value });
|
||
|
const handleUpload = (e, v) => setInput({ ...input, [v.name]: e.target.files[0] });
|
||
|
const handleChange = (e) => handleValues(e, e.currentTarget);
|
||
|
const handleCheck = (e, v) => setInput({ ...input, [v.name]: v.checked });
|
||
4 years ago
|
const handleDatetime = (v) => setInput({ ...input, datetime: v.utc().format() });
|
||
4 years ago
|
|
||
|
const makeProps = (name) => ({
|
||
|
name: name,
|
||
|
onChange: handleChange,
|
||
|
value: input[name] || '',
|
||
|
error: error[name],
|
||
|
});
|
||
|
|
||
|
return (
|
||
4 years ago
|
<div className='class-editor'>
|
||
4 years ago
|
<Form.Field>
|
||
4 years ago
|
<label>Time and Date</label>
|
||
|
<Datetime
|
||
|
timeConstraints={{ minutes: { step: 15 } }}
|
||
|
value={ input.datetime ? moment.utc(input.datetime).local() : (new Date()).setMinutes(0) }
|
||
|
onChange={handleDatetime}
|
||
4 years ago
|
/>
|
||
4 years ago
|
{error.datetime &&
|
||
4 years ago
|
<Label pointing prompt>
|
||
4 years ago
|
{error.datetime}
|
||
4 years ago
|
</Label>
|
||
|
}
|
||
|
</Form.Field>
|
||
4 years ago
|
|
||
|
<Form.Input
|
||
|
label='Cost ($)'
|
||
|
{...makeProps('cost')}
|
||
|
/>
|
||
|
|
||
|
<Form.Input
|
||
|
label='Max Number of Students'
|
||
|
{...makeProps('max_students')}
|
||
|
/>
|
||
|
|
||
|
{editing && <Form.Field>
|
||
|
<label>Is the class cancelled?</label>
|
||
|
<Checkbox
|
||
|
label='Yes'
|
||
|
name='is_cancelled'
|
||
|
onChange={handleCheck}
|
||
|
checked={input.is_cancelled}
|
||
|
/>
|
||
|
</Form.Field>}
|
||
|
|
||
4 years ago
|
</div>
|
||
|
);
|
||
|
}
|
||
|
|
||
4 years ago
|
export function InstructorClassDetail(props) {
|
||
|
const { clazz, setClass, token } = props;
|
||
4 years ago
|
const [open, setOpen] = useState(false);
|
||
4 years ago
|
const [input, setInput] = useState(clazz);
|
||
4 years ago
|
const [error, setError] = useState(false);
|
||
|
const [loading, setLoading] = useState(false);
|
||
|
const [success, setSuccess] = useState(false);
|
||
4 years ago
|
const { id } = useParams();
|
||
4 years ago
|
|
||
|
const handleSubmit = (e) => {
|
||
|
setLoading(true);
|
||
|
setSuccess(false);
|
||
4 years ago
|
requester('/sessions/'+id+'/', 'PUT', token, input)
|
||
4 years ago
|
.then(res => {
|
||
|
setSuccess(true);
|
||
4 years ago
|
setLoading(false);
|
||
4 years ago
|
setError(false);
|
||
4 years ago
|
setOpen(false);
|
||
4 years ago
|
setClass(res);
|
||
4 years ago
|
})
|
||
|
.catch(err => {
|
||
|
setLoading(false);
|
||
|
console.log(err);
|
||
|
setError(err.data);
|
||
|
});
|
||
|
};
|
||
|
|
||
4 years ago
|
return (
|
||
|
<div>
|
||
|
<Header size='medium'>Instructor Panel</Header>
|
||
4 years ago
|
|
||
4 years ago
|
{!open && success && <p>Saved!</p>}
|
||
4 years ago
|
|
||
4 years ago
|
{open ?
|
||
4 years ago
|
<Form onSubmit={handleSubmit}>
|
||
4 years ago
|
<Header size='small'>Edit Class</Header>
|
||
4 years ago
|
|
||
4 years ago
|
<InstructorClassEditor editing input={input} setInput={setInput} error={error} />
|
||
4 years ago
|
|
||
|
<Form.Button loading={loading} error={error.non_field_errors}>
|
||
|
Submit
|
||
|
</Form.Button>
|
||
4 years ago
|
</Form>
|
||
4 years ago
|
:
|
||
|
<Button onClick={() => setOpen(true)}>
|
||
4 years ago
|
Edit Class
|
||
4 years ago
|
</Button>
|
||
|
}
|
||
|
</div>
|
||
4 years ago
|
);
|
||
|
};
|
||
|
|
||
4 years ago
|
export function InstructorClassList(props) {
|
||
|
const { course, setCourse, token, user } = props;
|
||
4 years ago
|
const [open, setOpen] = useState(false);
|
||
|
const [input, setInput] = useState({});
|
||
|
const [error, setError] = useState(false);
|
||
|
const [loading, setLoading] = useState(false);
|
||
|
const [success, setSuccess] = useState(false);
|
||
|
|
||
|
const handleSubmit = (e) => {
|
||
|
setLoading(true);
|
||
|
setSuccess(false);
|
||
4 years ago
|
const data = { ...input, instructor: user.id, course: course.id };
|
||
|
requester('/sessions/', 'POST', token, data)
|
||
4 years ago
|
.then(res => {
|
||
|
setSuccess(res.id);
|
||
|
setInput({});
|
||
|
setLoading(false);
|
||
|
setError(false);
|
||
|
setOpen(false);
|
||
4 years ago
|
setCourse({ ...course, sessions: [ res, ...course.sessions ] });
|
||
4 years ago
|
})
|
||
|
.catch(err => {
|
||
|
setLoading(false);
|
||
|
console.log(err);
|
||
|
setError(err.data);
|
||
|
});
|
||
|
};
|
||
|
|
||
|
return (
|
||
|
<div>
|
||
|
<Header size='medium'>Instructor Panel</Header>
|
||
|
|
||
4 years ago
|
{!open && success && <p>Added! <Link to={'/classes/'+success}>View the class.</Link></p>}
|
||
4 years ago
|
|
||
|
{open ?
|
||
|
<Form onSubmit={handleSubmit}>
|
||
4 years ago
|
<Header size='small'>Add a Class</Header>
|
||
4 years ago
|
|
||
4 years ago
|
<InstructorClassEditor input={input} setInput={setInput} error={error} />
|
||
4 years ago
|
|
||
|
<Form.Button loading={loading} error={error.non_field_errors}>
|
||
|
Submit
|
||
|
</Form.Button>
|
||
|
</Form>
|
||
|
:
|
||
|
<Button onClick={() => setOpen(true)}>
|
||
4 years ago
|
Add a Class
|
||
4 years ago
|
</Button>
|
||
|
}
|
||
|
</div>
|
||
|
);
|
||
|
};
|
||
|
|