CalendarPage.jsx 1.78 KB
Newer Older
Zac Wood's avatar
Zac Wood committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
import React from 'react';
import Calendar from 'src/Calendar';
import Cart from 'src/Cart';
import SectionList from 'src/SectionList';
import QuickAdd from 'src/QuickAdd';
import moment from 'moment';

export default class CalendarPage extends React.Component {
    state = { events: [], sections: [] };

    constructor(props) {
        super(props);
        this.loadEvents();
    }

    loadEvents = async () => {
        const response = await fetch(`/schedule/events?crns=${Cart.crns.join(',')}`);
        const json = await response.json();
        this.setState({ ...json });
        Cart.crns = json.sections.map(s => s.crn);
    };

    events = () => {
        return this.state.events.filter(e => e.active).map(e => ({ ...e, start: moment(e.start).toDate(), end: moment(e.end).toDate() }));
    };

    toggleSection = crn => {
        const events = this.state.events.map(e => ({ ...e, active: e.crn == crn ? !e.active : e.active }));
        this.setState({ events });
    };

    removeAll = () => {
        Cart.crns = [];
        location.reload();
    };

    render() {
        return (
            <div>
                <Calendar events={this.events()} />
                {this.state.sections.length > 0 ? (
                    <div className="d-flex justify-content-between align-items-end">
                        <h2 className="mt-4">Your Schedule</h2>{' '}
                        <button type="button" onClick={this.removeAll} className="btn btn-danger mb-8">
                            Remove all sections
                        </button>
                    </div>
                ) : null}
                <SectionList onClick={this.toggleSection} sections={this.state.sections} expanded={true} />
                <QuickAdd loadCalendar={this.loadEvents} />
            </div>
        );
    }
}