From 14f3e4658606f1a61cbf471c8e6cb8ec2393f341 Mon Sep 17 00:00:00 2001 From: Tanner Collin Date: Thu, 27 Jan 2022 22:46:10 +0000 Subject: [PATCH] Revamp Class list --- apiserver/apiserver/api/serializers.py | 7 ++ webclient/src/App.js | 2 +- webclient/src/Classes.js | 143 ++++++++++++++++++++++--- 3 files changed, 134 insertions(+), 18 deletions(-) diff --git a/apiserver/apiserver/api/serializers.py b/apiserver/apiserver/api/serializers.py index a79771c..966afbd 100644 --- a/apiserver/apiserver/api/serializers.py +++ b/apiserver/apiserver/api/serializers.py @@ -455,6 +455,7 @@ class SessionSerializer(serializers.ModelSerializer): student_count = serializers.SerializerMethodField() course_data = serializers.SerializerMethodField() instructor_name = serializers.SerializerMethodField() + instructor_id = serializers.SerializerMethodField() datetime = serializers.DateTimeField() course = serializers.PrimaryKeyRelatedField(queryset=models.Course.objects.all()) students = TrainingSerializer(many=True, read_only=True) @@ -479,6 +480,12 @@ class SessionSerializer(serializers.ModelSerializer): name = 'Unknown' return obj.old_instructor or name + def get_instructor_id(self, obj): + if obj.instructor and hasattr(obj.instructor, 'member'): + return obj.instructor.member.id + else: + return None + class SessionListSerializer(SessionSerializer): students = None diff --git a/webclient/src/App.js b/webclient/src/App.js index bf03fb6..87d38b3 100644 --- a/webclient/src/App.js +++ b/webclient/src/App.js @@ -250,7 +250,7 @@ function App() { - + {user && user.member.set_details ? diff --git a/webclient/src/Classes.js b/webclient/src/Classes.js index dd9d105..0109656 100644 --- a/webclient/src/Classes.js +++ b/webclient/src/Classes.js @@ -11,6 +11,8 @@ import { PayPalPayNow } from './PayPal.js'; function ClassTable(props) { const { classes } = props; + const now = new Date().toISOString(); + return ( @@ -27,8 +29,8 @@ function ClassTable(props) { {classes.length ? classes.map(x => - - {x.course_data.name} + +  {x.course_data.name} {moment.utc(x.datetime).tz('America/Edmonton').format('ll')} @@ -37,7 +39,16 @@ function ClassTable(props) { {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} + + {!!x.max_students ? + x.max_students <= x.student_count ? + 'Full' + : + x.student_count + ' / ' + x.max_students + : + x.student_count + } + ) : @@ -48,7 +59,87 @@ function ClassTable(props) { ); }; +function NewClassTable(props) { + const { classes } = props; + + let sortedClasses = []; + if (classes.length) { + for (const clazz of classes) { + const course_data = clazz.course_data; + const course = sortedClasses.find(x => x?.course?.id === course_data?.id); + + if (course) { + course.classes.push(clazz); + } else { + sortedClasses.push({ + course: course_data, + classes: [clazz], + }); + } + } + } + + const now = new Date().toISOString(); + + return ( + <> +
+ {sortedClasses.map(x => + +
+ + {x.course.name} + +
+ +
+ + + Date + Time + Cost + Students + + + + + {x.classes.map(x => + + + +  {moment.utc(x.datetime).tz('America/Edmonton').format('MMM Do')} + + + + + {x.is_cancelled ? 'Cancelled' : moment.utc(x.datetime).tz('America/Edmonton').format('LT')} + + + {x.cost === '0.00' ? 'Free' : '$'+x.cost} + + + {!!x.max_students ? + x.max_students <= x.student_count ? + 'Full' + : + x.student_count + ' / ' + x.max_students + : + x.student_count + } + + + )} + +
+ + )} + + + ); +}; + let classesCache = false; +let sortCache = true; export function ClassFeed(props) { const [classes, setClasses] = useState(classesCache); @@ -89,7 +180,8 @@ export function ClassFeed(props) { export function Classes(props) { const [classes, setClasses] = useState(classesCache); - const { token } = props; + const [sortByCourse, setSortByCourse] = useState(sortCache); + const { token, user } = props; useEffect(() => { requester('/sessions/', 'GET', token) @@ -102,7 +194,8 @@ export function Classes(props) { }); }, []); - const now = new Date().toISOString(); + const isTeaching = (x) => x.instructor_id === user.member.id; + const sortDate = (a, b) => a.datetime > b.datetime ? 1 : -1; return ( @@ -110,22 +203,38 @@ export function Classes(props) {

Click here to view the list of all courses.

-
Upcoming
- -

Ordered by nearest date.

- - {classes ? - x.datetime > now).sort((a, b) => a.datetime > b.datetime ? 1 : -1)} /> - : -

Loading...

+ {!!user && !!classes.length && !!classes.filter(isTeaching).length && + <> +
Classes You're Teaching
+ + } -
Recent
+ -

Ordered by nearest date.

+ - {classes ? - x.datetime < now)} /> + {classes.length ? + sortByCourse ? + + : + :

Loading...

}