ScheduleList.tsx 2.05 KB
Newer Older
David Haynes's avatar
David Haynes committed
1
import * as React from 'react';
David Haynes's avatar
David Haynes committed
2
import { CourseSection } from '../util/CourseSection';
David Haynes's avatar
David Haynes committed
3
4

interface Props {
David Haynes's avatar
David Haynes committed
5
    courses: CourseSection[];
David Haynes's avatar
David Haynes committed
6
    selectCourseCallback?: (courseSection: CourseSection) => void;
David Haynes's avatar
David Haynes committed
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
}

export default class ScheduleList extends React.Component<Props, any> {
    constructor(props: Props) {
        super(props);
    }

    render() {
        return (
            <table>
                <tbody>
                    <tr>
                        <th>Course</th>
                        <th>course Name</th>
                        <th>CRN</th>
                        <th>Days</th>
                        <th>Instructor</th>
                        <th>Location</th>
                        <th>Time</th>
                        <th />
                    </tr>
                    {this.renderRowsForCourses(this.props.courses)}
                </tbody>
            </table>
        );
    }

David Haynes's avatar
David Haynes committed
34
    renderRowsForCourses(courses: CourseSection[]): JSX.Element[] {
David Haynes's avatar
David Haynes committed
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
        return courses.map(course => {
            return (
                <tr key={course.id}>
                    <td>{course.name}</td>
                    <td>{course.title}</td>
                    <td>{course.crn}</td>
                    <td>{course.days}</td>
                    <td>{course.instructor}</td>
                    <td>{course.location}</td>
                    <td>{[course.startTime, course.endTime].join(' - ')}</td>
                    {this.renderSelectCourseColumn(course.crn)}
                </tr>
            );
        });
    }

    renderSelectCourseColumn(rowCRN: string): JSX.Element {
        if (this.props.selectCourseCallback) {
            const courseWithCRN = this.getcourseWithCRN(rowCRN);
            return (
                <td>
                    <button onClick={() => this.props.selectCourseCallback(courseWithCRN)}>"Add to schedule"</button>
                </td>
            );
        } else {
            return <td />;
        }
    }

David Haynes's avatar
David Haynes committed
64
    getcourseWithCRN(crn: string): CourseSection {
David Haynes's avatar
David Haynes committed
65
66
67
        return this.props.courses.find(course => course.crn === crn);
    }
}