Skip to content

Commit

Permalink
feat: update exam index
Browse files Browse the repository at this point in the history
  • Loading branch information
varshamenon4 committed Jul 7, 2023
1 parent 91101a6 commit da5a6fd
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 26 deletions.
33 changes: 16 additions & 17 deletions src/pages/ExamsPage/components/ExamSelection.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,27 +4,26 @@ import {
SelectMenu,
} from '@edx/paragon';
import PropTypes from 'prop-types';
import { useUpdateCurrentExamIndex } from '../hooks';

// todo: implement substring search
// const searchByExamName = (exams, examName) => {
// const isMatchingExam = (exam) => exam === examName;
// const match = exams.filter(isMatchingExam);
// console.log('log');
// };

const updateCurrentExamIndex = (exam) => 'hello';
const ExamSelection = ({ exams }) => {
const { updateSelectedExam } = useUpdateCurrentExamIndex();

const ExamSelection = ({ exams }) => (
<div data-testid="exam_selection">
<SelectMenu defaultMessage="Select an exam...">
<MenuItem as={SearchField} />
{exams.map(exam => (
// todo: update redux state
<MenuItem onClick={() => console.log(exam.id)}>{exam.name}</MenuItem>
))}
</SelectMenu>
</div>
);
return (
<div data-testid="exam_selection">
<SelectMenu defaultMessage="Select an exam...">
<MenuItem as={SearchField} />
{exams.map(exam => (
// todo: update redux state
// <MenuItem onClick={() => console.log(exam.id)}>{exam.name}</MenuItem>
<MenuItem onClick={() => updateSelectedExam(exam.id)}>{exam.name}</MenuItem>
))}
</SelectMenu>
</div>
);
};

ExamSelection.propTypes = {
exams: PropTypes.arrayOf(PropTypes.object).isRequired, // eslint-disable-line react/forbid-prop-types
Expand Down
10 changes: 4 additions & 6 deletions src/pages/ExamsPage/data/reducer.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,12 +35,10 @@ const slice = createSlice({
deleteExamAttempt: (state, attemptId) => ({
...state,
attemptsList: state.attemptsList.filter(attempt => attempt.attempt_id !== attemptId.payload),
})
,
// todo: setCurrentExam
loadCurrentExamIndex: (state, { payload }) => ({
}),
setCurrentExamIndex: (state, examId) => ({
...state,
currentExamIndex: 1,
currentExamIndex: state.examsList.findIndex(exam => exam.id === examId.payload),
}),
},
});
Expand All @@ -49,7 +47,7 @@ export const {
loadExams,
loadExamAttempts,
deleteExamAttempt,
loadCurrentExamIndex,
setCurrentExamIndex,
} = slice.actions;

export const {
Expand Down
9 changes: 6 additions & 3 deletions src/pages/ExamsPage/hooks.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,16 +57,19 @@ export const useInitializeExamsPage = (courseId) => {
React.useEffect(() => { fetchCourseExams(courseId); }, []); // eslint-disable-line react-hooks/exhaustive-deps
};

export const useUpdateCurrentExamIndex = () => {
const dispatch = useDispatch();
return (examId) => () => dispatch(reducer.setCurrentExamIndex(examId));
};

export const useExamsData = () => {
const [exampleValue, setExampleValue] = state.exampleValue(0);
const examsList = useSelector(selectors.courseExamsList);
// todo: add setter here
const [currentExam, setCurrentExam] = useSelector(selectors.currentExam);
const currentExam = useSelector(selectors.currentExam);
const isLoading = reduxHooks.useRequestIsPending(RequestKeys.fetchCourseExams);

return {
currentExam,
setCurrentExam,
examsList,
isLoading,
exampleValue,
Expand Down

0 comments on commit da5a6fd

Please sign in to comment.