Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/keyboard shortcuts #1052

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions semesterly/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import re
import shutil
import socket
import time
from contextlib import contextmanager
from importlib import import_module

Expand Down Expand Up @@ -522,6 +523,7 @@ def remove_course_from_course_modal(self, n_slots_expected=None):
(By.CLASS_NAME, "fa-check"), root=modal_header, clickable=True
)
remove.click()
time.sleep(1)
self.assert_loader_completes()
self.assert_invisibility((By.CLASS_NAME, "course-modal"))
self.assert_n_elements_found(
Expand Down
85 changes: 85 additions & 0 deletions static/css/timetable/partials/course_modal.scss
Original file line number Diff line number Diff line change
Expand Up @@ -534,6 +534,91 @@
}
}

.modal-section-hover {
@include theme() {
background-color: t("lecture-section-background-hover");
}
cursor: pointer;
float: left;
padding: 7px;
position: relative;
transition: background-color 0.2s;
width: 50%;


&:nth-child(2n + 0) {
clear: both;
}

h4 {
@include theme() {
color: t("text-color");
}
font-size: 16px;
font-weight: 400;
line-height: 20px;
margin: 0;

span {
display: inline-block;
margin-right: 3px;
vertical-align: middle;
}

i {
display: inline-block;
font-size: 14px;
line-height: 20px;
vertical-align: middle;
}
}

h5 {
@include theme() {
color: t("text-color-thin");
}
font-size: 13px;
font-weight: 400;
line-height: 16px;
margin: 0;
}

h6 {
@include theme() {
color: t("text-color-thin");
}
font-size: 10px;
font-weight: 400;
line-height: 14px;
margin: 0;

.green {
color: $green;
}

.yellow {
color: $yellow;
}

.red {
color: $red;
}
}

.fa-lock {
bottom: 7px;
font-size: 14px;
line-height: 20px;
opacity: 0;
position: absolute;
right: 7px;
text-align: center;
transition: opacity 0.3s, visibility 0.3s;
visibility: hidden;
width: 20px;
}
}

.hidden-modal {
color: rgba(52, 73, 94, 0);
}
Expand Down
7 changes: 6 additions & 1 deletion static/js/redux/ui/CourseModalSection.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ type CourseModalSectionProps = {
lockOrUnlock: Function;
hoverSection: Function;
unHoverSection: Function;
isHovered: boolean;
};

// A. MadooeiP. Simari -> A. Madooei, P. Simari
Expand Down Expand Up @@ -65,11 +66,15 @@ const CourseModalSection = (props: CourseModalSectionProps) => {
} else if (seats < sizeDisplay / 10) {
benchmark = "yellow";
}

const style = props.isHovered ? "modal-section-hover" : "modal-section";

return (
<div
className={classnames("modal-section", {
className={classnames(style, {
locked: props.locked,
"on-active-timetable": props.isOnActiveTimetable,
"bg-lecture-section-background-hover": props.isHovered,
})}
onMouseDown={() => props.lockOrUnlock()}
onMouseEnter={() => props.hoverSection()}
Expand Down
77 changes: 76 additions & 1 deletion static/js/redux/ui/modals/CourseModalBody.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React, { useState, useEffect } from "react";
import React, { useState, useEffect, useCallback } from "react";
import isEmpty from "lodash/isEmpty";
import Reaction from "../reaction";
import REACTION_MAP from "../../constants/reactions";
Expand Down Expand Up @@ -87,6 +87,77 @@ const CourseModalBody = (props: CourseModalBodyProps) => {
});
}, []);

// Code to detect if arrow keys were hit. When detected, will hover over section times
// Current goal: just use left/right keys to index forwards or backwards

// State to store information about which section is being hovered by arrow keys
const [currentHoveredSection, setCurrentHoveredSection] = useState(-1);
const [currentHoveredSectionObj, setCurrentHoveredSectionObj] =
useState<Section | null>(null);

// Stores all available sections into one list
const sectionList: Section[] = [];
Object.keys(sectionTypeToSections)
.sort()
.forEach((sType) => {
const sections = sectionTypeToSections[sType];
sections.forEach((currentSection: Section) => {
sectionList.push(currentSection);
});
});

const handleKeyPress = useCallback(
(e) => {
if (e.key === "ArrowRight") {
setCurrentHoveredSection((prevSection) =>
prevSection < sectionList.length - 1 ? prevSection + 1 : prevSection
);
} else if (e.key === "ArrowLeft") {
setCurrentHoveredSection((prevSection) =>
prevSection >= 0 ? prevSection - 1 : prevSection
);
} else if (e.key === "Enter") {
if (currentHoveredSection >= 0 && currentHoveredSection < sectionList.length) {
dispatch(
addOrRemoveCourse(
props.course?.id,
sectionList[currentHoveredSection].meeting_section
)
);
props.hideModal();
}
} else if (e.key === "Escape") {
setCurrentHoveredSection(-1);
props.hideModal();
}
},
[currentHoveredSection]
);

// detects change in currentHoveredSection and (for now) just logs the hovered index
useEffect(() => {
if (currentHoveredSection !== -1) {
dispatch(
timetablesActions.hoverSection({
course: props.course,
section: sectionList[currentHoveredSection],
})
);
setCurrentHoveredSectionObj(sectionList[currentHoveredSection]);
} else {
dispatch(timetablesActions.unhoverSection());
setCurrentHoveredSectionObj(null);
}
}, [currentHoveredSection]);

// attaches/unattaches event listener to document
useEffect(() => {
document.addEventListener("keydown", handleKeyPress);
return () => {
document.removeEventListener("keydown", handleKeyPress);
};
}, [handleKeyPress]);

const dispatch = useAppDispatch();

const sendReact = (cid: number, title: string) => {
Expand Down Expand Up @@ -132,6 +203,10 @@ const CourseModalBody = (props: CourseModalBodyProps) => {
dispatch(timetablesActions.hoverSection({ course: props.course, section }))
}
unHoverSection={() => dispatch(timetablesActions.unhoverSection())}
isHovered={
currentHoveredSectionObj &&
currentHoveredSectionObj.course_section_id === section.course_section_id
}
/>
));

Expand Down
Loading