|
| 1 | +import { useActiveView } from 'hooks'; |
| 2 | +import { |
| 3 | + closedReasons, |
| 4 | + stepStates, |
| 5 | + stepNames, |
| 6 | +} from '../constants'; |
1 | 7 | import * as data from './data';
|
2 | 8 |
|
3 | 9 | import * as types from '../types';
|
@@ -31,6 +37,8 @@ export const useAssessmentStepConfig = (): types.AssessmentStepConfig => (
|
31 | 37 | useORAConfigData().assessmentSteps
|
32 | 38 | );
|
33 | 39 |
|
| 40 | +export const useAssessmentStepOrder = () => useAssessmentStepConfig()?.order; |
| 41 | + |
34 | 42 | export const useRubricConfig = (): types.RubricConfig => useORAConfigData().rubric;
|
35 | 43 |
|
36 | 44 | export const useEmptyRubric = () => {
|
@@ -71,21 +79,108 @@ export const usePageData = (): types.PageData => data.usePageData()?.data;
|
71 | 79 |
|
72 | 80 | export const useProgressData = (): types.ProgressData => usePageData()?.progress;
|
73 | 81 |
|
| 82 | +export const useActiveStepName = (): string => useProgressData().activeStepName; |
| 83 | + |
74 | 84 | export const useSubmissionTeamInfo = (): types.SubmissionTeamData => usePageData()?.submission.teamInfo;
|
75 | 85 |
|
76 | 86 | export const useSubmissionStatus = (): types.SubmissionStatusData => {
|
77 | 87 | const {
|
78 | 88 | hasCancelled,
|
79 | 89 | hasReceivedGrade,
|
80 | 90 | hasSubmitted,
|
| 91 | + isClosed, |
| 92 | + closedReason, |
81 | 93 | } = usePageData().submission;
|
82 | 94 | return {
|
83 | 95 | hasCancelled,
|
84 | 96 | hasReceivedGrade,
|
85 | 97 | hasSubmitted,
|
| 98 | + isClosed, |
| 99 | + closedReason, |
86 | 100 | };
|
87 | 101 | };
|
88 | 102 |
|
| 103 | +export const useHasCancelled = () => useSubmissionStatus().hasCancelled; |
| 104 | +export const useHasReceivedFinalGrade = () => useProgressData().hasReceivedFinalGrade; |
| 105 | + |
| 106 | +export const useSubmissionData = () => usePageData().submission; |
| 107 | + |
89 | 108 | export const useSubmissionResponse = (): types.SubmissionResponseData => (
|
90 | 109 | usePageData().submission.response
|
91 | 110 | );
|
| 111 | + |
| 112 | +export const useStepIndex = ({ step }) => useAssessmentStepOrder().indexOf(step); |
| 113 | + |
| 114 | +export const useSubmissionState = () => { |
| 115 | + const subStatus = useSubmissionStatus(); |
| 116 | + const teamInfo = useSubmissionTeamInfo(); |
| 117 | + |
| 118 | + if (subStatus.hasCancelled) { |
| 119 | + return stepStates.cancelled; |
| 120 | + } |
| 121 | + |
| 122 | + if (subStatus.hasSubmitted) { |
| 123 | + return stepStates.completed; |
| 124 | + } |
| 125 | + if (subStatus.isClosed) { |
| 126 | + if (subStatus.closedReason === closedReasons.pastDue) { |
| 127 | + return stepStates.closed; |
| 128 | + } |
| 129 | + return stepStates.notAvailable; |
| 130 | + } |
| 131 | + if (!subStatus.hasSubmitted && teamInfo.hasSubmitted) { |
| 132 | + return stepStates.teamAlreadySubmitted; |
| 133 | + } |
| 134 | + return stepStates.inProgress; |
| 135 | +}; |
| 136 | + |
| 137 | +export const useStepInfo = () => useProgressData().stepInfo; |
| 138 | + |
| 139 | +export const useStepState = ({ step }) => { |
| 140 | + const hasCancelled = useHasCancelled(); |
| 141 | + const hasReceivedFinalGrade = useHasReceivedFinalGrade(); |
| 142 | + const stepInfo = useStepInfo(); |
| 143 | + const activeStepIndex = useStepIndex({ step: useActiveStepName() }); |
| 144 | + const stepIndex = useStepIndex({ step }); |
| 145 | + const subState = useSubmissionState(); |
| 146 | + |
| 147 | + if (step === stepNames.submission) { |
| 148 | + return subState; |
| 149 | + } |
| 150 | + |
| 151 | + // Cancelled submission affects all states |
| 152 | + if (hasCancelled) { return stepStates.cancelled; } |
| 153 | + |
| 154 | + if (step === stepNames.myGrades) { |
| 155 | + return hasReceivedFinalGrade ? stepStates.completed : stepStates.notAvailable; |
| 156 | + } |
| 157 | + |
| 158 | + // For Assessment steps |
| 159 | + if (stepIndex < activeStepIndex) { return stepStates.completed; } |
| 160 | + if (stepIndex > activeStepIndex) { return stepStates.notAvailable; } |
| 161 | + |
| 162 | + // only check for closed or not-available on active step |
| 163 | + if (stepInfo[step]?.isClosed) { |
| 164 | + return stepInfo[step].closedReason === closedReasons.pastDue |
| 165 | + ? stepStates.closed |
| 166 | + : stepStates.notAvailable; |
| 167 | + } |
| 168 | + return stepStates.inProgress; |
| 169 | +}; |
| 170 | + |
| 171 | +export const useXBlockState = () => { |
| 172 | + const activeStepState = useStepState({ step: useActiveStepName() }); |
| 173 | + if ([ |
| 174 | + stepStates.cancelled, |
| 175 | + stepStates.closed, |
| 176 | + stepStates.notAvailable, |
| 177 | + ].includes(activeStepState)) { |
| 178 | + return activeStepState; |
| 179 | + } |
| 180 | + return stepStates.inProgress; |
| 181 | +}; |
| 182 | + |
| 183 | +export const useEffectiveGrade = () => { |
| 184 | + const { assessments } = usePageData(); |
| 185 | + return assessments ? assessments[assessments.effectiveAssessmentType] : null; |
| 186 | +}; |
0 commit comments