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

Add ability to adjust credits in special classes #264

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open
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
3 changes: 3 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,13 @@
"private": true,
"homepage": "https://gt-scheduler.org/",
"dependencies": {
"@emotion/react": "^11.11.3",
"@emotion/styled": "^11.11.0",
"@fortawesome/fontawesome-svg-core": "^6.2.1",
"@fortawesome/free-brands-svg-icons": "^6.2.1",
"@fortawesome/free-solid-svg-icons": "^6.2.1",
"@fortawesome/react-fontawesome": "^0.2.0",
"@mui/material": "^5.15.1",
"@sentry/react": "^6.12.0",
"@sentry/tracing": "^6.12.0",
"@types/react-map-gl": "^6.1.3",
Expand Down
9 changes: 9 additions & 0 deletions src/components/AppDataLoader/index.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import produce, { Immutable, Draft, original, castDraft } from 'immer';
import React, { useCallback, useMemo } from 'react';
import useLocalStorageState from 'use-local-storage-state';

import {
ScheduleContextValue,
Expand Down Expand Up @@ -293,6 +294,10 @@ function ContextProvider({
currentVersion,
});

const [adjustedCredits, setAdjustedCredits] = useLocalStorageState<
Record<string, number>
>('adjustedCredits', { defaultValue: {} });

// Memoize the context values so that they are stable
const scheduleContextValue = useMemo<ScheduleContextValue>(
() => [
Expand All @@ -301,6 +306,7 @@ function ContextProvider({
oscar,
currentVersion,
allVersionNames,
adjustedCredits,
...castDraft(scheduleVersion.schedule),
},
{
Expand All @@ -312,13 +318,15 @@ function ContextProvider({
deleteVersion,
renameVersion,
cloneVersion,
setAdjustedCredits,
},
],
[
currentTerm,
oscar,
currentVersion,
allVersionNames,
adjustedCredits,
scheduleVersion.schedule,
setTerm,
patchSchedule,
Expand All @@ -328,6 +336,7 @@ function ContextProvider({
deleteVersion,
renameVersion,
cloneVersion,
setAdjustedCredits,
]
);

Expand Down
104 changes: 96 additions & 8 deletions src/components/Course/index.tsx
Original file line number Diff line number Diff line change
@@ -1,16 +1,19 @@
import React, { useCallback, useContext, useEffect, useState } from 'react';
import { FontAwesomeIcon } from '@fortawesome/react-fontawesome';
import {
faAngleDown,
faAngleUp,
faShareAlt,
faPalette,
faPencilAlt,
faPlus,
faTrash,
} from '@fortawesome/free-solid-svg-icons';
import { Tooltip as ReactTooltip } from 'react-tooltip';

import { classes, getContentClassName } from '../../utils/misc';
import Cancellable from '../../utils/cancellable';
import { ActionRow, Instructor, Palette, Prerequisite } from '..';
import { ActionRow, CreditSlider, Instructor, Palette, Prerequisite } from '..';
import { ScheduleContext } from '../../contexts';
import { Course as CourseBean, Section } from '../../data/beans';
import { CourseGpa, CrawlerPrerequisites } from '../../types';
Expand All @@ -33,10 +36,18 @@ export default function Course({
const [prereqOpen, setPrereqOpen] = useState<boolean>(false);
const [paletteShown, setPaletteShown] = useState<boolean>(false);
const [gpaMap, setGpaMap] = useState<CourseGpa | null>(null);
const [creditSliderShown, setCreditSliderShown] = useState<boolean>(false);
const isSearching = Boolean(onAddCourse);
const [
{ oscar, desiredCourses, pinnedCrns, excludedCrns, colorMap },
{ patchSchedule },
{
oscar,
desiredCourses,
pinnedCrns,
excludedCrns,
colorMap,
adjustedCredits,
},
{ patchSchedule, setAdjustedCredits },
] = useContext(ScheduleContext);

useEffect(() => {
Expand Down Expand Up @@ -132,28 +143,55 @@ export default function Course({

const prereqControl = (
nextPrereqOpen: boolean,
nextCreditSliderShown: boolean,
nextExpanded: boolean
): void => {
setPrereqOpen(nextPrereqOpen);
setCreditSliderShown(nextCreditSliderShown);
setExpanded(nextExpanded);
};
const prereqAction = {
icon: faShareAlt,
styling: { transform: 'rotate(90deg)' },
onClick: (): void => {
prereqControl(true, !prereqOpen ? true : !expanded);
prereqControl(
true,
false,
!prereqOpen || creditSliderShown ? true : !expanded
);
},
tooltip: 'View Prerequisites',
id: `${course.id}-prerequisites`,
};

const sliderControl = (
nextPrereqOpen: boolean,
nextCreditSliderShown: boolean,
nextExpanded: boolean
): void => {
setPrereqOpen(nextPrereqOpen);
setCreditSliderShown(nextCreditSliderShown);
setExpanded(nextExpanded);
};
const handleSliderChange = (newValue: number): void => {
const newAdjustedCredits = {
...adjustedCredits,
[`${course.id}-${course.term}`]: newValue,
};
setAdjustedCredits(newAdjustedCredits);
course.adjustedCredits = newValue;
};

const pinnedSections = course.sections.filter((section) =>
pinnedCrns.includes(section.crn)
);
const totalCredits = pinnedSections.reduce(
(credits, section) => credits + section.credits,
0
);
const adjustableCredits = pinnedSections.some(
(section) => section.adjustableCredits
);

return (
<div
Expand All @@ -172,7 +210,7 @@ export default function Course({
: [
{
icon: expanded ? faAngleUp : faAngleDown,
onClick: (): void => prereqControl(false, !expanded),
onClick: (): void => prereqControl(false, false, !expanded),
},
prereqAction,
{
Expand Down Expand Up @@ -207,7 +245,44 @@ export default function Course({
: 'N/A'}
</span>
{totalCredits > 0 && (
<span className="credits">{totalCredits} Credits</span>
<div
style={{
display: 'flex',
alignItems: 'center',
backgroundColor: color,
}}
>
<span className="credits">
{adjustableCredits
? adjustedCredits[`${course.id}-${course.term}`] ?? 1
: totalCredits}{' '}
Credits
</span>
{adjustableCredits && (
<div className="adjustable-credits">
<FontAwesomeIcon
className="pencil-icon"
icon={faPencilAlt}
id={`${course.id}-credits`}
onClick={(): void => {
sliderControl(
false,
true,
prereqOpen || !creditSliderShown ? true : !expanded
);
}}
/>
<ReactTooltip
anchorId={`${course.id}-credits`}
variant="dark"
place="left"
style={{ fontSize: '110%' }}
>
Adjust Credits
</ReactTooltip>
</div>
)}
</div>
)}
</div>
)}
Expand All @@ -222,7 +297,7 @@ export default function Course({
/>
)}
</ActionRow>
{expanded && !prereqOpen && (
{expanded && !prereqOpen && !creditSliderShown && (
<div className={classes('hover-container', 'nested')}>
{includedInstructors.map((name) => {
let instructorGpa: number | undefined = 0;
Expand Down Expand Up @@ -264,9 +339,22 @@ export default function Course({
)}
</div>
)}
{expanded && prereqOpen && prereqs !== null && (
{expanded && !creditSliderShown && prereqOpen && prereqs !== null && (
<Prerequisite course={course} prereqs={prereqs} />
)}
{expanded && creditSliderShown && !prereqOpen && (
<div
className={classes('hover-container', 'nested')}
style={{ padding: '2px 15px 2px 15px' }}
>
<CreditSlider
value={adjustedCredits[`${course.id}-${course.term}`] ?? 1}
onChange={(_: Event, newValue: number | number[]): void => {
handleSliderChange(newValue as number);
}}
/>
</div>
)}
</div>
);
}
16 changes: 16 additions & 0 deletions src/components/Course/stylesheet.scss
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,22 @@
}
}

.adjustable-credits {
z-index: 0;
opacity: 0.6;
cursor: pointer;

&:hover {
opacity: 1;
}

.pencil-icon {
padding: 0px 0px 0px 6px;
outline: none;
border: none;
}
}

.palette {
position: absolute;
top: 0;
Expand Down
26 changes: 26 additions & 0 deletions src/components/CreditSlider/index.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import React from 'react';
import Slider from '@mui/material/Slider';

import './stylesheet.scss';

export type CreditSliderProps = {
value: number;
onChange: (event: Event, newValue: number | number[]) => void;
};

export default function CreditSlider({
value,
onChange,
}: CreditSliderProps): React.ReactElement {
return (
<Slider
className="slider"
min={1}
max={12}
value={value}
valueLabelDisplay="auto"
onChange={onChange}
marks
/>
);
}
21 changes: 21 additions & 0 deletions src/components/CreditSlider/stylesheet.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
.slider {
color: white !important;

.MuiSlider-mark {
background-color: transparent;
}

.MuiSlider-thumb {
width: 15px;
height: 15px;

&:hover {
box-shadow: 0px 0px 0px 8px rgba(255, 255, 255, 0.35);
}
}

.MuiSlider-valueLabel {
background-color: #222;
opacity: 0.9;
}
}
27 changes: 24 additions & 3 deletions src/components/Header/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,14 @@ export default function Header({
captureRef,
}: HeaderProps): React.ReactElement {
const [
{ term, oscar, pinnedCrns, allVersionNames, currentVersion },
{
term,
oscar,
pinnedCrns,
allVersionNames,
currentVersion,
adjustedCredits,
},
{
setTerm,
setCurrentVersion,
Expand All @@ -40,11 +47,25 @@ export default function Header({
const terms = useContext(TermsContext);

const totalCredits = useMemo(() => {
const adjustedCourses = new Set();
return pinnedCrns.reduce((credits, crn) => {
const crnSection = oscar.findSection(crn);
return credits + (crnSection != null ? crnSection.credits : 0);
if (
crnSection !== undefined &&
crnSection.adjustableCredits &&
!adjustedCourses.has(`${crnSection.course.id}-${term}`)
) {
adjustedCourses.add(`${crnSection.course.id}-${term}`);
return (
credits + (adjustedCredits[`${crnSection.course.id}-${term}`] ?? 1)
);
}
if (!crnSection?.adjustableCredits) {
return credits + (crnSection != null ? crnSection.credits : 0);
}
return credits;
}, 0);
}, [pinnedCrns, oscar]);
}, [pinnedCrns, oscar, adjustedCredits, term]);

const headerActionBarProps = useHeaderActionBarProps(captureRef);

Expand Down
1 change: 1 addition & 0 deletions src/components/HeaderDisplay/stylesheet.scss
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
padding: 12px;
display: flex;
align-items: center;
min-width: 115px;
}
}

Expand Down
1 change: 1 addition & 0 deletions src/components/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,3 +27,4 @@ export { default as EventBlocks } from './EventBlocks';
export { default as Attribution } from './Attribution';
export { default as Event } from './Event';
export { default as CourseNavMenu } from './CourseNavMenu';
export { default as CreditSlider } from './CreditSlider';
Loading