1
|
|
|
import React, { FunctionComponent } from "react"; |
2
|
|
|
import { Formik, Form } from "formik"; |
3
|
|
|
import { useIntl } from "react-intl"; |
4
|
|
|
import * as Yup from "yup"; |
5
|
|
|
import { |
6
|
|
|
ProfileSkillSubform, |
7
|
|
|
SkillFormValues, |
8
|
|
|
validationShape as skillValidationShape, |
9
|
|
|
submitDataToFormSkills, |
10
|
|
|
} from "./ProfileSkillSubform"; |
11
|
|
|
import { ExperienceSkill, ExperienceWork, Skill } from "../../../models/types"; |
12
|
|
|
import { ExperienceSubmitData } from "./ExperienceModalCommon"; |
13
|
|
|
import { |
14
|
|
|
ExperienceModalHeader, |
15
|
|
|
ExperienceDetailsIntro, |
16
|
|
|
ExperienceModalFooter, |
17
|
|
|
} from "../../Application/ExperienceModals/ExperienceModalCommon"; |
18
|
|
|
import Modal from "../../Modal"; |
19
|
|
|
import { |
20
|
|
|
WorkDetailsFormValues, |
21
|
|
|
experienceToDetails, |
22
|
|
|
newExperienceWork, |
23
|
|
|
workValidationShape, |
24
|
|
|
messages, |
25
|
|
|
detailsToExperience, |
26
|
|
|
WorkDetailsSubform, |
27
|
|
|
} from "../../Application/ExperienceModals/WorkExperienceModal"; |
28
|
|
|
|
29
|
|
|
type WorkExperienceFormValues = SkillFormValues & WorkDetailsFormValues; |
30
|
|
|
|
31
|
|
|
const dataToFormValues = ( |
32
|
|
|
data: ExperienceSubmitData<ExperienceWork>, |
33
|
|
|
allSkills: Skill[], |
34
|
|
|
): WorkExperienceFormValues => { |
35
|
|
|
return { |
36
|
|
|
...experienceToDetails(data.experience), |
37
|
|
|
...submitDataToFormSkills(data, allSkills), |
38
|
|
|
}; |
39
|
|
|
}; |
40
|
|
|
|
41
|
|
|
interface ProfileWorkModalProps { |
42
|
|
|
modalId: string; |
43
|
|
|
experienceWork: ExperienceWork | null; |
44
|
|
|
experienceableId: number; |
45
|
|
|
experienceableType: ExperienceWork["experienceable_type"]; |
46
|
|
|
userSkills: Skill[]; |
47
|
|
|
experienceSkills: ExperienceSkill[]; |
48
|
|
|
parentElement: Element | null; |
49
|
|
|
visible: boolean; |
50
|
|
|
onModalCancel: () => void; |
51
|
|
|
onModalConfirm: (data: ExperienceWork) => Promise<void>; |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
export const ProfileWorkModal: FunctionComponent<ProfileWorkModalProps> = ({ |
55
|
|
|
modalId, |
56
|
|
|
experienceWork, |
57
|
|
|
experienceableId, |
58
|
|
|
experienceableType, |
59
|
|
|
userSkills, |
60
|
|
|
experienceSkills, |
61
|
|
|
parentElement, |
62
|
|
|
visible, |
63
|
|
|
onModalCancel, |
64
|
|
|
onModalConfirm, |
65
|
|
|
}) => { |
66
|
|
|
const intl = useIntl(); |
67
|
|
|
|
68
|
|
|
const originalExperience = |
69
|
|
|
experienceWork ?? newExperienceWork(experienceableId, experienceableType); |
70
|
|
|
|
71
|
|
|
const relevantExperienceSkills = experienceSkills.filter( |
72
|
|
|
(expSkill) => |
73
|
|
|
expSkill.experience_id === experienceWork?.id && |
74
|
|
|
expSkill.experience_type === "experience_work", |
75
|
|
|
); |
76
|
|
|
|
77
|
|
|
const validationSchema = Yup.object().shape({ |
78
|
|
|
...workValidationShape(intl), |
79
|
|
|
...skillValidationShape(intl), |
80
|
|
|
}); |
81
|
|
|
|
82
|
|
|
const initialFormValues = dataToFormValues( |
83
|
|
|
{ |
84
|
|
|
experience: originalExperience, |
85
|
|
|
savedSkills: relevantExperienceSkills.map((expSkill) => ({ |
86
|
|
|
skillId: expSkill.skill_id, |
87
|
|
|
justification: expSkill.justification ?? "", |
88
|
|
|
})), |
89
|
|
|
}, |
90
|
|
|
userSkills, |
91
|
|
|
); |
92
|
|
|
|
93
|
|
|
return ( |
94
|
|
|
<Modal |
95
|
|
|
id={modalId} |
96
|
|
|
parentElement={parentElement} |
97
|
|
|
visible={visible} |
98
|
|
|
onModalCancel={onModalCancel} |
99
|
|
|
onModalConfirm={onModalCancel} |
100
|
|
|
className="application-experience-dialog" |
101
|
|
|
> |
102
|
|
|
<ExperienceModalHeader |
103
|
|
|
title={intl.formatMessage(messages.modalTitle)} |
104
|
|
|
iconClass="fa-briefcase" |
105
|
|
|
/> |
106
|
|
|
<Formik |
107
|
|
|
enableReinitialize |
108
|
|
|
initialValues={initialFormValues} |
109
|
|
|
onSubmit={async (values, actions): Promise<void> => { |
110
|
|
|
await onModalConfirm(detailsToExperience(values, originalExperience)); |
111
|
|
|
actions.setSubmitting(false); |
112
|
|
|
actions.resetForm(); |
113
|
|
|
}} |
114
|
|
|
validationSchema={validationSchema} |
115
|
|
|
> |
116
|
|
|
{(formikProps): React.ReactElement => ( |
117
|
|
|
<Form> |
118
|
|
|
<Modal.Body> |
119
|
|
|
<ExperienceDetailsIntro |
120
|
|
|
description={intl.formatMessage(messages.modalDescription)} |
121
|
|
|
/> |
122
|
|
|
<WorkDetailsSubform /> |
123
|
|
|
<ProfileSkillSubform keyPrefix="work" skills={userSkills} /> |
124
|
|
|
</Modal.Body> |
125
|
|
|
<ExperienceModalFooter buttonsDisabled={formikProps.isSubmitting} /> |
126
|
|
|
</Form> |
127
|
|
|
)} |
128
|
|
|
</Formik> |
129
|
|
|
</Modal> |
130
|
|
|
); |
131
|
|
|
}; |
132
|
|
|
|
133
|
|
|
export default ProfileWorkModal; |
134
|
|
|
|