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

2u/cosmonauts/refactor store #136

Merged
merged 1 commit into from
Feb 8, 2024
Merged

Conversation

alangsto
Copy link
Contributor

@alangsto alangsto commented Feb 8, 2024

COSMO-85

In order to gate chat visibility in the learning MFE by whether or not a learner is viewing an exam or has an active exam attempt, the exam state from frontend-lib-special-exams needed to be merged with the learning MFE store.

This PR is the culmination of several smaller commits that were all previously reviewed. Each commit contains code changes to refactor this library from using a HOC to pass state to child components to instead use selectors to read directly from the store.

@MichaelRoytman and I both tested this locally, and were able to successfully complete a timed exam.

Copy link

codecov bot commented Feb 8, 2024

Codecov Report

Attention: 15 lines in your changes are missing coverage. Please review.

Comparison is base (7f6f442) 94.14% compared to head (434bbf2) 93.23%.

❗ Current head 434bbf2 differs from pull request most recent head dc90390. Consider uploading reports for the commit dc90390 to get more accurate results

Files Patch % Lines
src/timer/TimerProvider.jsx 71.42% 2 Missing ⚠️
src/core/SequenceExamWrapper.jsx 0.00% 1 Missing ⚠️
src/exam/Exam.jsx 80.00% 1 Missing ⚠️
...arding_exam/EntranceOnboardingExamInstructions.jsx 66.66% 1 Missing ⚠️
...nboarding_exam/ErrorOnboardingExamInstructions.jsx 50.00% 1 Missing ⚠️
...arding_exam/RejectedOnboardingExamInstructions.jsx 66.66% 1 Missing ⚠️
...rding_exam/SubmittedOnboardingExamInstructions.jsx 66.66% 1 Missing ⚠️
...practice_exam/EntrancePracticeExamInstructions.jsx 50.00% 1 Missing ⚠️
...ns/practice_exam/ErrorPracticeExamInstructions.jsx 50.00% 1 Missing ⚠️
...ractice_exam/SubmittedPracticeExamInstructions.jsx 50.00% 1 Missing ⚠️
... and 4 more
Additional details and impacted files
@@            Coverage Diff             @@
##             main     #136      +/-   ##
==========================================
- Coverage   94.14%   93.23%   -0.91%     
==========================================
  Files          71       68       -3     
  Lines        1059     1050       -9     
  Branches      290      291       +1     
==========================================
- Hits          997      979      -18     
- Misses         57       66       +9     
  Partials        5        5              

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@alangsto alangsto force-pushed the 2u/cosmonauts/refactor-store branch from 96dd8c8 to 434bbf2 Compare February 8, 2024 16:47
Copy link
Member

@varshamenon4 varshamenon4 left a comment

Choose a reason for hiding this comment

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

Woah, this is incredible! Nice! lgtm!

import { useExamAccessToken, useFetchExamAccessToken, useIsExam } from './api';
import { initializeTestStore, render } from './setupTest';

/**
Copy link
Member

Choose a reason for hiding this comment

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

Nice docs!

const examDuration = attempt.total_time ? attempt.total_time : exam.total_time;
const platformName = getConfig().SITE_NAME;
const rulesUrl = getConfig().PROCTORED_EXAM_RULES_URL;
const [beginExamClicked, setBeginExamClicked] = useState(false);

useEffect(() => {
getExamReviewPolicy();
dispatch(getExamReviewPolicy());
// eslint-disable-next-line react-hooks/exhaustive-deps
}, []);
Copy link
Contributor

Choose a reason for hiding this comment

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

I found it odd we didn't get a lint error here for missing dispatch. I did a little reading, and it seems like the reference to dispatch is very unlikely to change. Maybe some linters have caught up to this and no longer lint on this? Where did you see that linting error originally?

I ask because I'd prefer we're consistent about whether we have dispatch in the dependency array.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I believe the reason we're not getting a lint error is because of the eslint-disable comment on the next line. I agree that we should be consistent, so I can add dispatch to the dependency array.

… useSelector hooks API. (#131)

* feat: rename examStore to specialExams in preparation for use in frontend-app-learning

This commit renames the examStore to specialExams. This is in preparation for the use of the exam reducer in the frontend-app-learning React application.

* refactor: replace use of context with thunks and Redux store in instructions components

This commit replaces the use of the ExamStateContext with the use of thunks and the Redux store in the instructions components.

The original pattern was to use the withExamStore higher-order component to provide context to the instructions components. This context contained provided the Redux store state and action creators as props by using the connect API. This posed a problem for our need to merge the frontend-app-learning and frontend-lib-special-exams stores, because the special exams store is initialized in this repository and used by the higher-order component. In order to eventually be able to remove the creation of the store in this repository, we have to remove references to the store by interfacing with the Redux more directly by using the useDispatch and useSelector hooks.

* test: remove references to ExamStateProvider from tests for instructions components

This commit removes references to the ExamStateProvider from tests for instructions components. Because these components have been refactored to no longe rely on the ExamStateProvider, they are not required in the component tree.

refactor: replace use of withExamStore higher-order component in TimerServiceProvider (#133)

This commit replaces the use of the withExamStore higher-order component with the useDispatch and useSelector hooks in TimerServiceProvider.

This commit also refactors components that use the TimerServiceProvider so that they no longer need to pass state and action creators via props. The TimerServiceProvider now gets whatever state it needs directly from the Redux store with a useSelector hook and imports and dispatches thunks directly by importing them from the data directory.

The original pattern was to use the withExamStore higher-order component to provide context to the TimerServiceProvider and its children. This context contained provided the Redux store state and action creators as props by using the connect API. This posed a problem for our need to merge the frontend-app-learning and frontend-lib-special-exams stores, because the special exams store is initialized in this repository and used by the higher-order component. In order to eventually be able to remove the creation of the store in this repository, we have to remove references to the store by interfacing with the Redux more directly by using the useDispatch and useSelector hooks.

feat: refactor non instruction components (#132)

Refactor public API functions to no longer import and use store. (#134)

* test: remove asynchronicity from initializing test store

The initializeTestStore test utility function used async...await keywords for initializing the test store, which isn't necessary. This commit removes the async...await keywords and refactors any tests that use that function.

* test: fix mocking of getExamAttemptsData function

* feat: refactor public API to use hooks instead of references to exported store

This commit refactors the public API, used by the frontend-app-learning application to interact with the frontend-lib-special-exams state, to export a series of hooks. Originally, the public API imported the frontend-lib-special-exams store directly and operated on it in a series of exported functions. This posed a problem for our need to merge the frontend-app-learning and frontend-lib-special-exams stores, because the special exams store is initialized in this repository and used by public API. In order to eventually be able to remove the creation of the store in this repository, we have to remove references to the store by interfacing with the Redux more directly by using the useDispatch and useSelector hooks.

This commit also exports the root reducer from this library. This root reducer will be imported by the frontend-app-learning application and used to configure its store.

fix: patch bugs found during bash (#135)
@alangsto alangsto force-pushed the 2u/cosmonauts/refactor-store branch from 434bbf2 to dc90390 Compare February 8, 2024 17:55
@alangsto alangsto merged commit 00bd39b into main Feb 8, 2024
5 checks passed
@alangsto alangsto deleted the 2u/cosmonauts/refactor-store branch February 8, 2024 18:01
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants