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

feat: switch button to get only my courses #134

Merged
merged 1 commit into from
Dec 6, 2024
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
29 changes: 25 additions & 4 deletions src/features/Courses/CoursesFilters/index.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,19 @@ import { useDispatch, useSelector } from 'react-redux';
import { Col, Form } from '@edx/paragon';
import { Select } from 'react-paragon-topaz';

import { updateFilters, updateCurrentPage } from 'features/Courses/data/slice';
import { updateFilters, updateCurrentPage, updateShowAllCourses } from 'features/Courses/data/slice';
import { fetchCoursesData, fetchCoursesOptionsData } from 'features/Courses/data/thunks';

import { initialPage, styleFirstOption, allResultsOption } from 'features/constants';
import {
initialPage, styleFirstOption, allResultsOption, RequestStatus,
} from 'features/constants';

const CoursesFilters = () => {
const dispatch = useDispatch();
const selectedInstitution = useSelector((state) => state.main.selectedInstitution);
const courses = useSelector((state) => state.courses.selectOptions);
const showAllCourses = useSelector((state) => state.courses.showAllCourses);
const coursesRequest = useSelector((state) => state.courses.table.status);
const [courseOptions, setCourseOptions] = useState([]);
const [courseSelected, setCourseSelected] = useState(null);
const [inputCourse, setInputCourse] = useState('');
Expand All @@ -35,6 +39,8 @@ const CoursesFilters = () => {
}
};

const handleToggleChange = e => dispatch(updateShowAllCourses(e.target.checked));

useEffect(() => {
if (Object.keys(selectedInstitution).length > 0) {
setCourseSelected(null);
Expand All @@ -56,7 +62,9 @@ const CoursesFilters = () => {

useEffect(() => {
if (Object.keys(selectedInstitution).length > 0) {
const params = {};
const params = {
has_classes: showAllCourses,
};

if (courseSelected) {
params.course_name = courseSelected.value === defaultOption.value
Expand All @@ -68,8 +76,11 @@ const CoursesFilters = () => {
dispatch(updateFilters(params));
dispatch(updateCurrentPage(initialPage));
}

// eslint-disable-next-line react-hooks/exhaustive-deps
}, [courseSelected, selectedInstitution]);
}, [courseSelected, selectedInstitution, showAllCourses]);

const isLoading = coursesRequest === RequestStatus.LOADING;

return (
<div className="filter-container justify-content-center row">
Expand All @@ -95,6 +106,16 @@ const CoursesFilters = () => {
/>
</Form.Group>
</Form.Row>
<Form.Row className="w-100 mt-2">
<Form.Switch
checked={showAllCourses}
onChange={handleToggleChange}
className="ml-3"
disabled={isLoading}
>
Show my courses
</Form.Switch>
</Form.Row>
</Form>
</div>
</div>
Expand Down
5 changes: 5 additions & 0 deletions src/features/Courses/data/slice.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ const initialState = {
data: null,
},
notificationMessage: '',
showAllCourses: false,
};

export const coursesSlice = createSlice({
Expand Down Expand Up @@ -67,6 +68,9 @@ export const coursesSlice = createSlice({
updateNotificationMsg: (state, { payload }) => {
state.notificationMessage = payload;
},
updateShowAllCourses: (state, { payload }) => {
state.showAllCourses = payload;
},
},
});

Expand All @@ -83,6 +87,7 @@ export const {
newClassFailed,
resetClassState,
updateNotificationMsg,
updateShowAllCourses,
} = coursesSlice.actions;

export const { reducer } = coursesSlice;
Loading