-
Notifications
You must be signed in to change notification settings - Fork 319
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
base: master
Are you sure you want to change the base?
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
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 []; | ||
} | ||
}; | ||
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); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We generally don't want to leave There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There's one higher up as well (oops) |
||
return randomCourse; | ||
}; |
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'; | ||
|
@@ -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; | ||
|
@@ -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) => { | ||
|
@@ -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}`); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Could you "generate" the route to push using |
||
} else { | ||
console.error("No courses available or invalid course"); | ||
} | ||
}; | ||
|
||
return ( | ||
<div className={classnames(styles.settingsPage, 'page-container')}> | ||
<Title>Settings</Title> | ||
|
@@ -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> | ||
); | ||
}; | ||
|
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 | ||
|
||
|
There was a problem hiding this comment.
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.