|
1
|
|
|
import React from "react"; |
|
2
|
|
|
import { WrappedComponentProps, injectIntl } from "react-intl"; |
|
3
|
|
|
import { connect } from "react-redux"; |
|
4
|
|
|
import ReactDOM from "react-dom"; |
|
5
|
|
|
import { JobPosterKeyTask, Job, Criteria } from "../../../models/types"; |
|
6
|
|
|
import { NUM_OF_TASKS, isJobBuilderComplete } from "../jobBuilderHelpers"; |
|
7
|
|
|
import JobTasks from "./JobTasks"; |
|
8
|
|
|
import { |
|
9
|
|
|
jobBuilderSkills, |
|
10
|
|
|
jobBuilderImpact, |
|
11
|
|
|
jobBuilderReview, |
|
12
|
|
|
} from "../../../helpers/routes"; |
|
13
|
|
|
import { RootState } from "../../../store/store"; |
|
14
|
|
|
import { |
|
15
|
|
|
getJob, |
|
16
|
|
|
getTasksByJob, |
|
17
|
|
|
getCriteriaByJob, |
|
18
|
|
|
} from "../../../store/Job/jobSelector"; |
|
19
|
|
|
import { DispatchType } from "../../../configureStore"; |
|
20
|
|
|
import { batchUpdateJobTasks } from "../../../store/Job/jobActions"; |
|
21
|
|
|
import RootContainer from "../../RootContainer"; |
|
22
|
|
|
import JobBuilderStepContainer from "../JobBuilderStep"; |
|
23
|
|
|
import { navigate } from "../../../helpers/router"; |
|
24
|
|
|
|
|
25
|
|
|
interface JobTasksPageProps { |
|
26
|
|
|
jobId: number; |
|
27
|
|
|
job: Job | null; |
|
28
|
|
|
keyTasks: JobPosterKeyTask[]; |
|
29
|
|
|
// Criteria associated with the job, used to determine if its complete |
|
30
|
|
|
criteria: Criteria[]; |
|
31
|
|
|
handleUpdateTasks: ( |
|
32
|
|
|
jobId: number, |
|
33
|
|
|
tasks: JobPosterKeyTask[], |
|
34
|
|
|
) => Promise<JobPosterKeyTask[]>; |
|
35
|
|
|
} |
|
36
|
|
|
|
|
37
|
|
|
const JobTasksPage: React.FunctionComponent<JobTasksPageProps & |
|
38
|
|
|
WrappedComponentProps> = ({ |
|
39
|
|
|
jobId, |
|
40
|
|
|
job, |
|
41
|
|
|
keyTasks, |
|
42
|
|
|
criteria, |
|
43
|
|
|
handleUpdateTasks, |
|
44
|
|
|
intl, |
|
45
|
|
|
}): React.ReactElement => { |
|
46
|
|
|
const { locale } = intl; |
|
47
|
|
|
if (locale !== "en" && locale !== "fr") { |
|
48
|
|
|
throw new Error("Unexpected locale"); |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
const handleModalCancel = (): void => {}; // No need to do anything. |
|
52
|
|
|
const handleModalConfirm = (): void => { |
|
53
|
|
|
// Continue to next page |
|
54
|
|
|
navigate(jobBuilderSkills(locale, jobId)); |
|
55
|
|
|
}; |
|
56
|
|
|
const handleSubmit = ( |
|
57
|
|
|
tasks: JobPosterKeyTask[], |
|
58
|
|
|
): Promise<JobPosterKeyTask[]> => handleUpdateTasks(jobId, tasks); |
|
59
|
|
|
const handleReturn = (): void => { |
|
60
|
|
|
navigate(jobBuilderImpact(locale, jobId)); |
|
61
|
|
|
}; |
|
62
|
|
|
const handleSkipToReview = async (): Promise<void> => { |
|
63
|
|
|
navigate(jobBuilderReview(locale, jobId)); |
|
64
|
|
|
}; |
|
65
|
|
|
const jobIsComplete = |
|
66
|
|
|
job !== null && isJobBuilderComplete(job, keyTasks, criteria, locale); |
|
67
|
|
|
return ( |
|
68
|
|
|
<JobBuilderStepContainer jobId={jobId} currentPage="tasks"> |
|
69
|
|
|
{job !== null && ( |
|
70
|
|
|
<JobTasks |
|
71
|
|
|
jobId={jobId} |
|
72
|
|
|
keyTasks={keyTasks} |
|
73
|
|
|
validCount={NUM_OF_TASKS} |
|
74
|
|
|
handleSubmit={handleSubmit} |
|
75
|
|
|
handleReturn={handleReturn} |
|
76
|
|
|
handleModalCancel={handleModalCancel} |
|
77
|
|
|
handleModalConfirm={handleModalConfirm} |
|
78
|
|
|
handleSkipToReview={handleSkipToReview} |
|
79
|
|
|
jobIsComplete={jobIsComplete} |
|
80
|
|
|
/> |
|
81
|
|
|
)} |
|
82
|
|
|
</JobBuilderStepContainer> |
|
83
|
|
|
); |
|
84
|
|
|
}; |
|
85
|
|
|
|
|
86
|
|
|
const mapStateToProps = ( |
|
87
|
|
|
state: RootState, |
|
88
|
|
|
ownProps: { jobId: number }, |
|
89
|
|
|
): { |
|
90
|
|
|
job: Job | null; |
|
91
|
|
|
keyTasks: JobPosterKeyTask[]; |
|
92
|
|
|
criteria: Criteria[]; |
|
93
|
|
|
} => ({ |
|
94
|
|
|
job: getJob(state, ownProps), |
|
95
|
|
|
keyTasks: getTasksByJob(state, ownProps), |
|
96
|
|
|
criteria: getCriteriaByJob(state, ownProps), |
|
97
|
|
|
}); |
|
98
|
|
|
|
|
99
|
|
|
const mapDispatchToProps = ( |
|
100
|
|
|
dispatch: DispatchType, |
|
101
|
|
|
): { |
|
102
|
|
|
handleUpdateTasks: ( |
|
103
|
|
|
jobId: number, |
|
104
|
|
|
tasks: JobPosterKeyTask[], |
|
105
|
|
|
) => Promise<JobPosterKeyTask[]>; |
|
106
|
|
|
} => ({ |
|
107
|
|
|
handleUpdateTasks: async ( |
|
108
|
|
|
jobId: number, |
|
109
|
|
|
tasks: JobPosterKeyTask[], |
|
110
|
|
|
): Promise<JobPosterKeyTask[]> => { |
|
111
|
|
|
const result = await dispatch(batchUpdateJobTasks(jobId, tasks)); |
|
112
|
|
|
if (result.error) { |
|
113
|
|
|
return Promise.reject(result.payload); |
|
114
|
|
|
} |
|
115
|
|
|
const resultTasks = await result.payload; |
|
116
|
|
|
return resultTasks; |
|
117
|
|
|
}, |
|
118
|
|
|
}); |
|
119
|
|
|
|
|
120
|
|
|
const JobTasksPageContainer = connect( |
|
121
|
|
|
mapStateToProps, |
|
122
|
|
|
mapDispatchToProps, |
|
123
|
|
|
)(injectIntl(JobTasksPage)); |
|
124
|
|
|
|
|
125
|
|
|
export default JobTasksPageContainer; |
|
126
|
|
|
|
|
127
|
|
|
if (document.getElementById("job-builder-tasks")) { |
|
128
|
|
|
const container = document.getElementById("job-builder-tasks") as HTMLElement; |
|
129
|
|
|
const jobIdAttr = container.getAttribute("data-job-id"); |
|
130
|
|
|
const jobId = jobIdAttr ? Number(jobIdAttr) : null; |
|
131
|
|
|
if (jobId) { |
|
132
|
|
|
ReactDOM.render( |
|
133
|
|
|
<RootContainer> |
|
134
|
|
|
<JobTasksPageContainer jobId={jobId} /> |
|
135
|
|
|
</RootContainer>, |
|
136
|
|
|
container, |
|
137
|
|
|
); |
|
138
|
|
|
} |
|
139
|
|
|
} |
|
140
|
|
|
|