-
Notifications
You must be signed in to change notification settings - Fork 322
Expand file tree
/
Copy pathuseExamAccess.js
More file actions
47 lines (39 loc) · 1.57 KB
/
useExamAccess.js
File metadata and controls
47 lines (39 loc) · 1.57 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
import React from 'react';
import { logError } from '@edx/frontend-platform/logging';
import { StrictDict, useKeyedState } from '@edx/react-unit-test-utils';
import { useExamAccessToken, useFetchExamAccessToken, useIsExam } from '@edx/frontend-lib-special-exams';
export const stateKeys = StrictDict({
accessToken: 'accessToken',
blockAccess: 'blockAccess',
});
const useExamAccess = ({
id,
}) => {
const isExam = useIsExam();
const [blockAccess, setBlockAccess] = useKeyedState(stateKeys.blockAccess, isExam);
const fetchExamAccessToken = useFetchExamAccessToken();
// NOTE: We cannot use this hook in the useEffect hook below to grab the updated exam access token in the finally
// block, due to the rules of hooks. Instead, we get the value of the exam access token from a call to
// the hook below.
// When the fetchExamAccessToken call completes, the useExamAccess hook will re-run
// (due to a change to the Redux store, and, thus, a change to the context), at which point the updated
// exam access token will be fetched via the useExamAccessToken hook call below.
// The important detail is that there should never be a return value (false, '').
const examAccessToken = useExamAccessToken();
React.useEffect(() => {
if (isExam) {
fetchExamAccessToken()
.finally(() => {
setBlockAccess(false);
})
.catch((error) => {
logError(error);
});
}
}, [id, isExam]);
return {
blockAccess,
accessToken: examAccessToken,
};
};
export default useExamAccess;