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

random course picker button #3735

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
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
6 changes: 6 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions website/src/reducers/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,3 +48,7 @@ export default function reducers(state: State = defaultState, action: Actions):
};
return undoReducer(state, newState, action);
}




13 changes: 13 additions & 0 deletions website/src/utils/api.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import axios from 'axios';

const BASE_URL = 'https://api.nusmods.com/v2/2023-2024';

export const fetchModules = async () => {
try {
const response = await axios.get(`${BASE_URL}/moduleList.json`);
return response.data;
} catch (error) {
console.error('Error fetching modules:', error);
return [];
}
};
Comment on lines +5 to +13
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can I check if there's a reason you added fetchModules to explicitly fetch all modules?

The module list should be available in Redux store, see website/src/types/state.ts for the structure of the store.

13 changes: 13 additions & 0 deletions website/src/utils/randomCoursePicker.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { fetchModules } from './api';

export const getRandomCourse = async () => {
const courses = await fetchModules();
if (!courses || courses.length === 0) {
console.log('No courses available');
return null;
}
const randomIndex = Math.floor(Math.random() * courses.length);
const randomCourse = courses[randomIndex];
console.log('Random Course:', randomCourse);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We generally don't want to leave console.logs in production code. I think in this case, they aren't 100% necessary for the feature to work.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There's one higher up as well (oops)

return randomCourse;
};
22 changes: 21 additions & 1 deletion website/src/views/settings/SettingsContainer.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { Fragment, useCallback, useEffect, useState } from 'react';
import { connect } from 'react-redux';
import { connect, useSelector } from 'react-redux';
import classnames from 'classnames';
import { useHistory } from 'react-router-dom';
import { isEqual } from 'lodash';

import { ColorSchemePreference, ThemeId } from 'types/settings';
Expand Down Expand Up @@ -39,6 +40,8 @@ import previewTimetable from './previewTimetable';
import BetaToggle from './BetaToggle';
import RefreshPrompt from './RefreshPrompt';
import styles from './SettingsContainer.scss';
import { getRandomCourse } from 'utils/randomCoursePicker'; // Import the random course picker function


type Props = {
currentThemeId: string;
Expand Down Expand Up @@ -68,6 +71,8 @@ const SettingsContainer: React.FC<Props> = ({
...props
}) => {
const [allowTracking, setAllowTracking] = useState(true);
const history = useHistory();


const onToggleTracking = useCallback((isTrackingAllowed: boolean) => {
withTracker((tracker: Tracker) => {
Expand All @@ -89,6 +94,15 @@ const SettingsContainer: React.FC<Props> = ({

useScrollToTop();

const handleRandomCourseClick = async () => {
const randomCourse = await getRandomCourse(); // Fetch random course from API
if (randomCourse && randomCourse.moduleCode) {
history.push(`/courses/${randomCourse.moduleCode}`);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you "generate" the route to push using modulePage from website/src/views/routes/paths.ts?

} else {
console.error("No courses available or invalid course");
}
};

return (
<div className={classnames(styles.settingsPage, 'page-container')}>
<Title>Settings</Title>
Expand Down Expand Up @@ -289,6 +303,12 @@ const SettingsContainer: React.FC<Props> = ({
/>
</div>
</div>
{/* Add the Random Course Picker Button */}
<hr />
<h4 id="random-course-picker">Random Course Picker</h4>
<button className="btn btn-primary" onClick={handleRandomCourseClick}>
Pick a Random Course
</button>
</div>
);
};
Expand Down
4 changes: 4 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# THIS IS AN AUTOGENERATED FILE. DO NOT EDIT THIS FILE DIRECTLY.
# yarn lockfile v1