Skip to content

website: Implement random course picker button (#3735) #4039

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

Open
wants to merge 1 commit into
base: master
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
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import { ElasticSearchResult } from 'types/vendor/elastic-search';
import { ModuleInformation } from 'types/modules';

import ModuleFinderSidebar from 'views/modules/ModuleFinderSidebar';
import ModuleRandomButton from 'views/modules/ModuleRandomButton';
import ModuleSearchBox from 'views/modules/ModuleSearchBox';
import ModuleFinderNoHits from 'views/errors/ModuleFinderNoHits';
import ModuleFinderApiError from 'views/errors/ModuleFinderApiError';
Expand Down Expand Up @@ -65,6 +66,8 @@ const ModuleFinderContainer: React.FC = () => (

<ModuleSearchBox id="q" />

<ModuleRandomButton />

<div>
<HitsStats
component={({ hitsCount }: HitsStatsDisplayProps) => (
Expand Down
29 changes: 29 additions & 0 deletions website/src/views/modules/ModuleRandomButton.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import classNames from 'classnames';
import { Shuffle } from 'react-feather';
import { useSelector } from 'react-redux';
import { useHistory } from 'react-router-dom';
import type { State } from 'types/state';
import { modulePage } from 'views/routes/paths';

const ModuleRandomButton: React.FC = () => {
const modules = useSelector((state: State) => state.moduleBank).moduleList;
const history = useHistory();

Check warning on line 10 in website/src/views/modules/ModuleRandomButton.tsx

View check run for this annotation

Codecov / codecov/patch

website/src/views/modules/ModuleRandomButton.tsx#L8-L10

Added lines #L8 - L10 were not covered by tests

const handleClick = () => {
const randomMod = modules[Math.floor(Math.random() * modules.length)];
history.push(modulePage(randomMod.moduleCode));

Check warning on line 14 in website/src/views/modules/ModuleRandomButton.tsx

View check run for this annotation

Codecov / codecov/patch

website/src/views/modules/ModuleRandomButton.tsx#L12-L14

Added lines #L12 - L14 were not covered by tests
};

return (

Check warning on line 17 in website/src/views/modules/ModuleRandomButton.tsx

View check run for this annotation

Codecov / codecov/patch

website/src/views/modules/ModuleRandomButton.tsx#L17

Added line #L17 was not covered by tests
<button
type="button"
className={classNames('btn', 'btn-outline-primary', 'btn-block', 'btn-svg')}
onClick={handleClick}
>
<Shuffle className="svg" />
Random Course
</button>
);
};

export default ModuleRandomButton;