Passed
Push — task/application-profile-react... ( dd70ad...773af2 )
by
unknown
05:45
created

  B

Complexity

Conditions 7

Size

Total Lines 13
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 11
dl 0
loc 13
rs 8
c 0
b 0
f 0
cc 7
1
import React, { FunctionComponent, ChangeEvent, useState } from "react";
2
import { FormattedMessage, useIntl } from "react-intl";
3
import { GocClassification, ProfileBasicInformation as ProfileBasicInformationProp } from "../../models/types";
4
import { myBasicInformationMessages } from "../Application/applicationMessages";
5
import { removeDuplicatesById } from "../../helpers/queries";
6
7
export interface ProfileBasicInformationProps {
8
  gocClassifications : GocClassification[],
9
  basicInformation: ProfileBasicInformationProp,
10
  name: string,
11
  email: string,
12
}
13
14
export interface ClassificationDropdownsProps {
15
  gocClassifications : GocClassification[]
16
  selectedItem?: GocClassification
17
}
18
19
export interface GcExperienceProps {
20
  gocClassifications : GocClassification[],
21
  previousExperienceProp: GocClassification[],
22
  currentGcClassification: GocClassification,
23
  wasGcEmployee: boolean
24
}
25
26
const GcExperience: FunctionComponent<GcExperienceProps> = ({
27
  previousExperienceProp,
28
  currentGcClassification,
29
  gocClassifications,
30
  wasGcEmployee
31
}) => {
32
33
  const intl = useIntl();
34
35
  const [previousExperience, setPreviousExperience] = useState<GocClassification[]>(previousExperienceProp);
36
37
  const removeExperience = function(e : React.MouseEvent<HTMLButtonElement, MouseEvent>) {
38
    console.dir(e)
39
  }
40
41
  const addExperience = function(/*e : React.MouseEvent<HTMLAnchorElement, MouseEvent>*/) {
42
    previousExperience.push({
43
      classification: {
44
        id: 0,
45
        key: "",
46
        name: {
47
          en: "",
48
          fr: ""
49
        },
50
      },
51
      level: 0,
52
      order: 0,
53
    })
54
    setPreviousExperience(previousExperience)
55
  }
56
57
  if (!wasGcEmployee)
58
    return (<></>)
59
60
  return (
61
    <>
62
    <label htmlFor="SEL2">Current classification and level</label>
63
    <ClassificationDropdowns selectedItem={currentGcClassification} gocClassifications={gocClassifications} />
64
65
    <label htmlFor="SEL2">Add previous Government classifications</label>
66
      <div id="list-previous-gov-class">
67
      {previousExperience.map( experience =>
68
        <ol>
69
          <li>
70
            <div data-c-grid="gutter top">
71
              <ClassificationDropdowns
72
                gocClassifications={gocClassifications}
73
                selectedItem={experience}
74
                data-c-grid-item="base(2of2) tl(2of3)"
75
              />
76
              <div
77
                data-c-grid-item="base(1of1) tl(1of3)"
78
                data-c-input="select"
79
              >
80
                <button data-c-button="solid(c1)" onClick={removeExperience}>
81
                  <i className="fa fa-trash" /> Remove
82
                </button>
83
                <span>{intl.formatMessage(myBasicInformationMessages.inputEreror)}</span>
84
              </div>
85
            </div>
86
          </li>
87
        </ol>
88
      )}
89
      </div>
90
      <a data-c-button="solid(c1)" onClick={addExperience}>
91
        {intl.formatMessage(myBasicInformationMessages.addClassification)}
92
      </a>
93
    </>
94
  );
95
};
96
97
const ClassificationDropdowns: FunctionComponent<ClassificationDropdownsProps> = ({
98
  gocClassifications,
99
  selectedItem
100
}) => {
101
102
  const safeParseInt = function(str : string | null) : number {
103
    if (str == null) return 0
104
    else if (typeof str == "string") return parseInt(str)
105
    else return -1
106
  }
107
108
  const getInitialSelectedClassification = () : string | null => {
109
    if (selectedItem?.classification.id) {
110
      return selectedItem?.classification.id.toString()
111
    }
112
    return null
113
  }
114
115
  const [selectedClassification, setSelectedClassification] = useState<string | null>(getInitialSelectedClassification());
116
117
  const handleSelectedClassification = function(e : ChangeEvent<HTMLSelectElement>){
118
    setSelectedClassification(e.target.value)
119
  }
120
121
  const uniqueClassifications = removeDuplicatesById(
122
    gocClassifications.map((item) => ({
123
      id: item.classification.id,
124
      key: item.classification.key,
125
    })),
126
  );
127
128
  function getLevelsOfClassification(classificationKey : number | null) : string[] {
129
130
    const correspondingGocClassifications = gocClassifications.filter(
131
      item => item.classification.id == classificationKey
132
    )
133
134
    let correspondingLevels : string[] = [];
135
    correspondingGocClassifications.forEach(function(correspondingGocClassification : GocClassification) {
136
      correspondingLevels.push(correspondingGocClassification.level.toString())
137
    })
138
139
    return correspondingLevels
140
  }
141
142
  return (
143
    <>
144
      <div data-c-grid-item="base(1of3)" data-c-grid="gutter top">
145
        <div data-c-grid-item="base(1of1) tl(1of2)" data-c-input="select">
146
          <div>
147
            <i className="fas fa-caret-down" />
148
            <select defaultValue={selectedItem?.classification.id} required id="SEL2" onChange={handleSelectedClassification} >
149
              <option></option>
150
              {uniqueClassifications.map(item =>
151
                <option key={item.id} value={item.id}>{item.key}</option>
152
              )};
153
            </select>
154
          </div>
155
          <span>This input has an error.</span>
156
        </div>
157
        <div data-c-grid-item="base(1of1) tl(1of2)" data-c-input="select">
158
          <div>
159
            <i className="fas fa-caret-down" />
160
            <select defaultValue={selectedItem?.level} required id="SEL2">
161
              <option></option>
162
              {
163
                getLevelsOfClassification(safeParseInt(selectedClassification)).map(item =>
164
                  <option key={item} value={item}>{item}</option>
165
                )
166
              }
167
            </select>
168
          </div>
169
          <span>This input has an error.</span>
170
        </div>
171
      </div>
172
    </>
173
  );
174
};
175
176
export const ProfileBasicInformation: React.FC<ProfileBasicInformationProps> = ({
177
  gocClassifications,
178
  basicInformation,
179
  name,
180
  email
181
}) => {
182
  const intl = useIntl();
183
184
  const getInitialEmployeeState = () : boolean => {
185
    if (basicInformation.current_classification) {
186
      return true
187
    }
188
189
    return false
190
  }
191
192
  const [currentGcEmployee, setCurrentGcEmployee] = useState<boolean>(getInitialEmployeeState());
193
194
  return (
195
    <>
196
      <div>
197
        <h2 data-c-heading="h2" data-c-margin="bottom(1)">
198
          {intl.formatMessage(myBasicInformationMessages.heading)}
199
        </h2>
200
        <p data-c-margin="bottom(1)">
201
          <FormattedMessage
202
            id="profile.experience.preamble"
203
            defaultMessage="This profile is also shared when you submit a job application."
204
            description="First section of text on the 'My Basic Information' of the Application Timeline."
205
          />
206
        </p>
207
        <div>
208
          <p>
209
            Name: <b data-c-color="c1"> {name} </b>{" "}
210
          </p>
211
          <p>
212
            Personal Email: <b data-c-color="c1"> {email} </b>{" "}
213
          </p>
214
          <p>
215
            To change these go to:{" "}
216
            <a data-c-color="c1" href="#">
217
              Account Settings
218
            </a>
219
          </p>
220
        </div>
221
        <div data-c-input="select">
222
          <label htmlFor="SEL2">{intl.formatMessage(myBasicInformationMessages.citizenStatus)}</label>
223
          <span>Required</span>
224
          <div data-c-grid-item="base(1of3)">
225
226
            <i className="fas fa-caret-down" />
227
            <select required id="SEL2">
228
              <option disabled selected>
229
                Select a status...
230
              </option>
231
              <option>Canadian Citizen</option>
232
              <option>Not a citizen</option>
233
            </select>
234
          </div>
235
          <span>{intl.formatMessage(myBasicInformationMessages.inputEreror)}</span>
236
        </div>
237
        <div data-c-input="select">
238
          <label htmlFor="SEL2">
239
            {intl.formatMessage(myBasicInformationMessages.isVerteran)}
240
          </label>
241
          <span>Required</span>
242
          <div data-c-grid-item="base(1of3)">
243
            <i className="fas fa-caret-down" />
244
            <select required id="SEL2">
245
              <option>No</option>
246
              <option>Yes</option>
247
            </select>
248
          </div>
249
          <span>{intl.formatMessage(myBasicInformationMessages.inputEreror)}</span>
250
        </div>
251
        <h2 data-c-heading="h2" data-c-margin="bottom(1)">
252
          {intl.formatMessage(myBasicInformationMessages.heading)}
253
        </h2>
254
255
        <div data-c-grid-item="base(1of3)" data-c-input="radio">
256
          <label htmlFor="RG2">
257
            {intl.formatMessage(myBasicInformationMessages.isGCEmployee)}
258
          </label>
259
          <span>Required</span>
260
          <div id="RG2" role="radiogroup">
261
            <label htmlFor="rB1">
262
              <input id="rB1" required name="radioB" type="radio" defaultChecked={currentGcEmployee} onChange={() => setCurrentGcEmployee(true)} />
263
              <span>Current GC Employee</span>
264
            </label>
265
            <label htmlFor="rB2">
266
              <input id="rB2" required name="radioB" type="radio" onChange={() => setCurrentGcEmployee(false)} />
267
              <span>Not a GC employee</span>
268
            </label>
269
          </div>
270
          <span>{intl.formatMessage(myBasicInformationMessages.inputEreror)}</span>
271
        </div>
272
273
        <GcExperience
274
          gocClassifications={gocClassifications}
275
          previousExperienceProp={basicInformation.previous_classifications}
276
          currentGcClassification={basicInformation.current_classification}
277
          wasGcEmployee={currentGcEmployee}
278
        />
279
280
      </div>
281
    </>
282
  );
283
};
284
285
export default ProfileBasicInformation;
286
287