Skip to content

Commit d101399

Browse files
authored
Load project as needed (#2643)
Fixes #2642
1 parent e228d16 commit d101399

1 file changed

Lines changed: 20 additions & 6 deletions

File tree

src/components/HOCs/WithCurrentTask/WithCurrentTask.jsx

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import { connect } from "react-redux";
77
import AsCooperativeWork from "../../../interactions/Task/AsCooperativeWork";
88
import AsMappableBundle from "../../../interactions/TaskBundle/AsMappableBundle";
99
import { fetchChallenge, fetchParentProject } from "../../../services/Challenge/Challenge";
10+
import { fetchProject } from "../../../services/Project/Project";
1011
import { fetchChallengeActions } from "../../../services/Challenge/Challenge";
1112
import { CHALLENGE_STATUS_FINISHED } from "../../../services/Challenge/ChallengeStatus/ChallengeStatus";
1213
import AppErrors from "../../../services/Error/AppErrors";
@@ -66,7 +67,7 @@ const WithLoadedTask = function (WrappedComponent, forReview) {
6667
return class extends Component {
6768
loadNeededTask = (props) => {
6869
if (Number.isFinite(props.taskId)) {
69-
props.loadTask(props.taskId, props.task, forReview);
70+
props.loadTask(props.taskId, props?.projectId, props.task, forReview);
7071
}
7172
};
7273

@@ -98,9 +99,17 @@ export const mapStateToProps = (state, ownProps) => {
9899
const taskEntity = state.entities?.tasks?.[taskId];
99100

100101
if (taskEntity) {
102+
// Store
103+
const challengeId = taskEntity.parent;
104+
const projectId = state.entities?.challenges?.[challengeId]?.parent;
101105
// denormalize task so that parent challenge is embedded.
102106
mappedProps.task = denormalize(taskEntity, taskDenormalizationSchema(), state.entities);
103107

108+
// The above projection oblisterates parent identifiers, project them back in as needed
109+
if (Number.isFinite(projectId)) {
110+
mappedProps.projectId = projectId;
111+
}
112+
104113
mappedProps.challengeId = mappedProps.task?.parent?.id;
105114
}
106115
}
@@ -115,7 +124,7 @@ export const mapDispatchToProps = (dispatch, ownProps) => {
115124
*
116125
* @private
117126
*/
118-
loadTask: (taskId, existingTask = null, forReview = false) => {
127+
loadTask: (taskId, projectId, existingTask = null, forReview = false) => {
119128
dispatch(forReview ? fetchTaskForReview(taskId) : fetchTask(taskId))
120129
.then((normalizedResults) => {
121130
if (
@@ -132,20 +141,25 @@ export const mapDispatchToProps = (dispatch, ownProps) => {
132141
}
133142

134143
const loadedTask = normalizedResults.entities.tasks[normalizedResults.result];
144+
const existingChallenge = existingTask?.parent;
135145
// Load the parent challenge if missing or stale
136146
if (
137-
!_isPlainObject(existingTask?.parent) ||
138-
isStale(existingTask.parent, CHALLENGE_STALE)
147+
!_isPlainObject(existingChallenge) ||
148+
isStale(existingChallenge, CHALLENGE_STALE)
139149
) {
140150
dispatch(fetchChallenge(loadedTask.parent)).then((normalizedChallengeResults) => {
151+
const existingProject = existingChallenge?.parent;
141152
// Load the parent project if missing or stale
142153
if (
143-
!_isPlainObject(existingTask?.parent?.parent) ||
144-
isStale(existingTask.parent.parent, PROJECT_STALE)
154+
!_isPlainObject(existingProject) ||
155+
isStale(existingProject, PROJECT_STALE)
145156
) {
146157
fetchParentProject(dispatch, normalizedChallengeResults);
147158
}
148159
});
160+
} else if (!existingChallenge.parent && Number.isFinite(projectId)) {
161+
// Directly load the project by identifier
162+
dispatch(fetchProject(projectId));
149163
}
150164

151165
// Fetch the task comments and location data, but don't wait for them

0 commit comments

Comments
 (0)