Completed
Push — master ( 401bbc...02c732 )
by Grant
05:46 queued 02:35
created

CreateJobPosterAPI.prepopulateValuesFromManagerProfile   B

Complexity

Conditions 7
Paths 33

Size

Total Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 7
nc 33
nop 1
dl 0
loc 1
rs 8
c 1
b 0
f 0
1
/*
2
 * To change this license header, choose License Headers in Project Properties.
3
 * To change this template file, choose Tools | Templates
4
 * and open the template in the editor.
5
 */
6
7
8
9
var CreateJobPosterAPI = {};
10
11
CreateJobPosterAPI.jobPosterObj = null;
12
13
CreateJobPosterAPI.version = "v1";
14
CreateJobPosterAPI.baseURL = "/tc/api/" + CreateJobPosterAPI.version + "";
15
16
CreateJobPosterAPI.JobPosterQuestion = function (question, description) {
17
    this.question = question;
18
    this.description = description;
19
};
20
21
CreateJobPosterAPI.JobPostNonLocalized = function (
22
        id,
23
        manager_user_id,
24
        title,
25
        title_fr,
26
        department_id,
27
        province_id,
28
        branch_en,
29
        branch_fr,
30
        division_en,
31
        division_fr,
32
        city,
33
        city_fr,
34
        open_date_time,
35
        close_date_time,
36
        start_date,
37
        term_qty,
38
        remuneration_range_low,
39
        remuneration_range_high,
40
        impact,
41
        impact_fr,
42
        key_tasks_en,
43
        key_tasks_fr,
44
        core_competencies_en,
45
        core_competencies_fr,
46
        developing_competencies_en,
47
        developing_competencies_fr,
48
        questions_en,
49
        questions_fr,
50
        noc,
51
        classification,
52
        clearance_id,
53
        language_id
54
        ) {
55
    this.id = id;
56
    this.manager_user_id = manager_user_id;
57
    this.title = {};
58
    this.title.en_CA = title;
59
    this.title.fr_CA = title_fr;
60
    this.department_id = department_id;
61
    this.province_id = province_id;
62
    this.branch = {};
63
    this.branch.en_CA = branch_en;
64
    this.branch.fr_CA = branch_fr;
65
    this.division = {};
66
    this.division.en_CA = division_en;
67
    this.division.fr_CA = division_fr;
68
    this.city = {};
69
    this.city.en_CA = city;
70
    this.city.fr_CA = city_fr;
71
    this.open_date_time = open_date_time;
72
    this.close_date_time = close_date_time;
73
    this.start_date = start_date;
74
    this.term_qty = term_qty;
75
    this.remuneration_range_low = remuneration_range_low;
76
    this.remuneration_range_high = remuneration_range_high;
77
    this.impact = {};
78
    this.impact.en_CA = impact;
79
    this.impact.fr_CA = impact_fr;
80
    this.key_tasks = {};
81
    this.key_tasks.en_CA = key_tasks_en;
82
    this.key_tasks.fr_CA = key_tasks_fr;
83
    this.core_competencies = {};
84
    this.core_competencies.en_CA = core_competencies_en;
85
    this.core_competencies.fr_CA = core_competencies_fr;
86
    this.developing_competencies = {};
87
    this.developing_competencies.en_CA = developing_competencies_en;
88
    this.developing_competencies.fr_CA = developing_competencies_fr;
89
    this.questions = {};
90
    this.questions.en_CA = questions_en;
91
    this.questions.fr_CA = questions_fr;
92
    this.noc = noc;
93
    this.classification = classification;
94
    this.clearance_id = clearance_id;
95
    this.language_id = language_id;
96
97
    this.term_units_id = 2; //default to months for now
98
    this.job_min_level_id = 1; //default to CS1
99
    this.job_max_level_id = 3; //default to CS3
100
};
101
102
CreateJobPosterAPI.localizeJobPost = function (jobPostNonLocalized, locale) {
103
    var jp = jobPostNonLocalized;
104
105
    return new JobPostAPI.JobPost(
0 ignored issues
show
Bug introduced by
The variable JobPostAPI seems to be never declared. If this is a global, consider adding a /** global: JobPostAPI */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
106
            jp.id,
107
            jp.manager_user_id,
108
            jp.title[locale],
109
            jp.appplicants_to_date,
110
            jp.close_date_time,
111
            LookupAPI.getLocalizedLookupValue("department", jp.department_id),
0 ignored issues
show
Bug introduced by
The variable LookupAPI seems to be never declared. If this is a global, consider adding a /** global: LookupAPI */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
112
            jp.branch[locale],
113
            jp.division[locale],
114
            jp.city[locale],
115
            LookupAPI.getLocalizedLookupValue("province", jp.province_id),
116
            jp.term_qty,
117
            LookupAPI.getLocalizedLookupValue("jobterm", jp.term_units_id),
118
            jp.remuneration_type,
119
            jp.remuneration_range_low,
120
            jp.remuneration_range_high,
121
            jp.impact[locale],
122
            jp.key_tasks[locale],
123
            jp.core_competencies[locale],
124
            jp.developing_competencies[locale],
125
            jp.questions[locale],
126
            jp.noc,
127
            jp.classification,
128
            LookupAPI.getLocalizedLookupValue("clearance", jp.clearance_id),
129
            LookupAPI.getLocalizedLookupValue("language", jp.language_id),
130
            jp.start_date
131
            );
132
};
133
134
CreateJobPosterAPI.showCreateJobPosterForm = function () {
135
    var stateInfo = {pageInfo: 'create_job_poster', pageTitle: 'Talent Cloud: Create Job Poster'};
136
    document.title = stateInfo.pageTitle;
137
    history.pushState(stateInfo, stateInfo.pageInfo, '#CreateJobPoster');
0 ignored issues
show
Bug introduced by
The variable history seems to be never declared. If this is a global, consider adding a /** global: history */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
138
139
    ManagerEventsAPI.hideAllLayouts();
0 ignored issues
show
Bug introduced by
The variable ManagerEventsAPI seems to be never declared. If this is a global, consider adding a /** global: ManagerEventsAPI */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
140
141
    document.getElementById("createJobPoster_openDate").value = Utilities.formatDateTimeLocal(new Date());
0 ignored issues
show
Bug introduced by
The variable Utilities seems to be never declared. If this is a global, consider adding a /** global: Utilities */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
142
143
    CreateJobPosterAPI.getManagerProfile(CreateJobPosterAPI.prepopulateValuesFromManagerProfile);
144
145
    var createJobPosterSection = document.getElementById("createJobPosterSection");
146
    createJobPosterSection.classList.remove("hidden");
147
148
    // New Subpage Hero Scripts
149
150
    Utilities.getHeroElements();
151
152
    var posterHeroTitle = document.getElementById("posterHeroTitle");
153
    posterHeroTitle.classList.remove("hidden");
154
    posterHeroTitle.setAttribute("aria-hidden", "false");
155
156
    // Google Analytics
157
158
    ga('set', 'page', '/admin/create-job');
159
    ga('send', 'pageview');
160
161
};
162
163
CreateJobPosterAPI.localizeCreateJobPosterForm = function (siteContent) {
0 ignored issues
show
Unused Code introduced by
The parameter siteContent is not used and could be removed.

This check looks for parameters in functions that are not used in the function body and are not followed by other parameters which are used inside the function.

Loading history...
164
    try {
165
        LookupAPI.populateDropdown("department", "createJobPoster_department");
0 ignored issues
show
Bug introduced by
The variable LookupAPI seems to be never declared. If this is a global, consider adding a /** global: LookupAPI */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
166
        LookupAPI.populateDropdown("province", "createJobPoster_province");
167
        LookupAPI.populateDropdown("clearance", "createJobPoster_clearance");
168
        LookupAPI.populateDropdown("language", "createJobPoster_language");
169
    } catch (e) {
170
        (console.error || console.log).call(console, e.stack || e);
171
    }
172
}
0 ignored issues
show
Coding Style introduced by
There should be a semicolon.

Requirement of semicolons purely is a coding style issue since JavaScript has specific rules about semicolons which are followed by all browsers.

Further Readings:

Loading history...
173
174
CreateJobPosterAPI.prepopulateValuesFromManagerProfile = function (managerProfileResponse) {
175
    if (managerProfileResponse) {
176
        var response = JSON.parse(managerProfileResponse);
177
178
        document.getElementById("createJobPoster_department").value = response.manager_profile.user_manager_profile_department_id;
179
        if (document.getElementById("createJobPoster_department").value) {
180
            document.getElementById("createJobPoster_department").parentElement.classList.add("active");
181
            document.getElementById("createJobPoster_department").parentElement.classList.add("valid");
182
        }
183
        document.getElementById("createJobPoster_branch").value = response.manager_profile_details.en_CA.user_manager_profile_details_branch;
184
        if (document.getElementById("createJobPoster_branch").value) {
185
            document.getElementById("createJobPoster_branch").parentElement.classList.add("active");
186
            document.getElementById("createJobPoster_branch").parentElement.classList.add("valid");
187
        }
188
        document.getElementById("createJobPoster_branch_fr").value = response.manager_profile_details.fr_CA.user_manager_profile_details_branch;
189
        if (document.getElementById("createJobPoster_branch_fr").value) {
190
            document.getElementById("createJobPoster_branch_fr").parentElement.classList.add("active");
191
            document.getElementById("createJobPoster_branch_fr").parentElement.classList.add("valid");
192
        }
193
        document.getElementById("createJobPoster_division").value = response.manager_profile_details.en_CA.user_manager_profile_details_division;
194
        if (document.getElementById("createJobPoster_division").value) {
195
            document.getElementById("createJobPoster_division").parentElement.classList.add("active");
196
            document.getElementById("createJobPoster_division").parentElement.classList.add("valid");
197
        }
198
        document.getElementById("createJobPoster_division_fr").value = response.manager_profile_details.fr_CA.user_manager_profile_details_division;
199
        if (document.getElementById("createJobPoster_division_fr").value) {
200
            document.getElementById("createJobPoster_division_fr").parentElement.classList.add("active");
201
            document.getElementById("createJobPoster_division_fr").parentElement.classList.add("valid");
202
        }
203
204
    }
205
};
206
207
//below are the functions for the tabbed layout of the 'create job poster' page for managers
208
CreateJobPosterAPI.goToTab = function (tabId) {
209
    var stepGroups = document.getElementsByClassName('stepGroup');
210
    //console.log("+   " + stepGroups);
211
212
    if (tabId === "createJobPosterReviewTab") {
213
        CreateJobPosterAPI.populateReviewTab();
214
    }
215
216
    for (var s = 0; s < stepGroups.length; s++) {
217
        var stepGroup = stepGroups[s];
218
        //console.log(stepGroup);
219
        if (!stepGroup.classList.contains('hidden')) {
220
            stepGroup.classList.add('hidden');
221
        }
222
        if (stepGroup.id === tabId) {
223
            stepGroup.classList.remove('hidden');
224
        }
225
    }
226
};
227
228
CreateJobPosterAPI.populateReviewTab = function () {
229
    CreateJobPosterAPI.populateJobPosterObjFromForm();
230
231
    if (CreateJobPosterAPI.jobPosterObj) {
232
        var demoAreaEnglish = document.getElementById("createJobPosterDemoAreaEnglish");
233
        demoAreaEnglish.innerHTML = "";
234
        var jobEnglish = CreateJobPosterAPI.localizeJobPost(CreateJobPosterAPI.jobPosterObj, "en_CA");
235
        demoAreaEnglish.appendChild(JobPostAPI.populateJobSummary(jobEnglish, true, "en_CA"));
0 ignored issues
show
Bug introduced by
The variable JobPostAPI seems to be never declared. If this is a global, consider adding a /** global: JobPostAPI */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
236
237
        //Create demo french
238
        var demoAreaFrench = document.getElementById("createJobPosterDemoAreaFrench");
239
        demoAreaFrench.innerHTML = "";
240
        var jobFrench = CreateJobPosterAPI.localizeJobPost(CreateJobPosterAPI.jobPosterObj, "fr_CA");
241
        demoAreaFrench.appendChild(JobPostAPI.populateJobSummary(jobFrench, true, "fr_CA"));
242
    } else {
243
        window.alert("Job Poster must be submitted first");
244
    }
245
};
246
247
CreateJobPosterAPI.stepHighlight = function (stepID) {
248
    var s1 = document.getElementById("createJobPosterStep1Label");
249
    s1.classList.remove("create-job-poster-tab-current");
250
    var s2 = document.getElementById("createJobPosterStep2Label");
251
    s2.classList.remove("create-job-poster-tab-current");
252
    var s3 = document.getElementById("createJobPosterStep3Label");
253
    s3.classList.remove("create-job-poster-tab-current");
254
    var s4 = document.getElementById("createJobPosterStep4Label");
255
    s4.classList.remove("create-job-poster-tab-current");
256
257
    var current = document.getElementById(stepID);
258
    current.classList.add("create-job-poster-tab-current");
259
};
260
261
CreateJobPosterAPI.validateJobPosterForm = function () {
262
    CreateJobPosterAPI.populateJobPosterObjFromForm();
263
264
    var jp = CreateJobPosterAPI.jobPosterObj;
0 ignored issues
show
Unused Code introduced by
The variable jp seems to be never used. Consider removing it.
Loading history...
265
266
    var valid = true;
267
    if (valid) {
268
        CreateJobPosterAPI.submitJobPosterForm();
269
    }
270
};
271
272
CreateJobPosterAPI.populateJobPosterObjFromForm = function () {
273
    var id = 0;
274
    //Keep same id if it already exists
275
    if (CreateJobPosterAPI.jobPosterObj) {
276
        id = CreateJobPosterAPI.jobPosterObj.id;
277
    }
278
279
    var manager_user_id = 0;
280
    if (UserAPI.hasSessionUser()) {
0 ignored issues
show
Bug introduced by
The variable UserAPI seems to be never declared. If this is a global, consider adding a /** global: UserAPI */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
281
        //For now, assume there is a one-to-one relation between users and hiring managers
282
        manager_user_id = UserAPI.getSessionUserAsJSON().user_id;
283
    }
284
285
    var title = document.getElementById("createJobPoster_jobTitle").value;
286
287
    var title_fr = document.getElementById("createJobPoster_jobTitle_fr").value;
288
289
    var department_id = document.getElementById("createJobPoster_department").value;
290
291
    var province_id = document.getElementById("createJobPoster_province").value;
292
293
    var branch_en = document.getElementById("createJobPoster_branch").value;
294
    var branch_fr = document.getElementById("createJobPoster_branch_fr").value;
295
296
    var division_en = document.getElementById("createJobPoster_division").value;
297
    var division_fr = document.getElementById("createJobPoster_division_fr").value;
298
299
    var city = document.getElementById("createJobPoster_city").value;
300
301
    var city_fr = document.getElementById("createJobPoster_city").value;
302
303
    var open_date_time = document.getElementById("createJobPoster_openDate").value;
304
305
    var close_date_time = document.getElementById("createJobPoster_closeDate").value;
306
307
    var start_date = document.getElementById("createJobPoster_startDate").value;
308
309
    var term_qty = document.getElementById("createJobPoster_termQuantity").value;
310
311
    var remuneration_range_low = document.getElementById("createJobPoster_remunerationLowRange").value;
312
313
    var remuneration_range_high = document.getElementById("createJobPoster_remunerationHighRange").value;
314
315
    var impact = document.getElementById("createJobPoster_impact").value;
316
317
    var impact_fr = document.getElementById("createJobPoster_impact_fr").value;
318
319
    //TODO: actually get list items from ui
320
    var key_tasks_en = CreateJobPosterAPI.getTextareaContentsAsList("createJobPoster_keyTasks");
321
    var key_tasks_fr = CreateJobPosterAPI.getTextareaContentsAsList("createJobPoster_keyTasks_fr");
322
323
    var core_competencies_en = CreateJobPosterAPI.getTextareaContentsAsList("createJobPoster_coreCompetencies");
324
    var core_competencies_fr = CreateJobPosterAPI.getTextareaContentsAsList("createJobPoster_coreCompetencies_fr");
325
326
    var developing_competencies_en = CreateJobPosterAPI.getTextareaContentsAsList("createJobPoster_developingCompetencies");
327
    var developing_competencies_fr = CreateJobPosterAPI.getTextareaContentsAsList("createJobPoster_developingCompetencies_fr");
328
329
    //Get questions & descriptions from repeaters
330
    var questionWrappers = document.querySelectorAll(".job-poster__open-question:not(.repeater__template)");
331
332
    var questionObjs_en = [];
333
    var questionObjs_fr = [];
334
335
    for (var i = 0; i < questionWrappers.length; i++) {
336
        var question_en = questionWrappers[i].querySelector(".job-poster__open-question-wrapper--english .job-poster__open-question-input").value;
337
        var question_fr = questionWrappers[i].querySelector(".job-poster__open-question-wrapper--french .job-poster__open-question-input").value;
338
339
        var description_en = questionWrappers[i].querySelector(".job-poster__open-question-wrapper--english .job-poster__open-question-description-input").value;
340
        var description_fr = questionWrappers[i].querySelector(".job-poster__open-question-wrapper--french .job-poster__open-question-description-input").value;
341
342
        if (question_en && question_fr) {
343
            questionObjs_en.push(new CreateJobPosterAPI.JobPosterQuestion(question_en, description_en));
344
            questionObjs_fr.push(new CreateJobPosterAPI.JobPosterQuestion(question_fr, description_fr));
345
        }
346
    }
347
348
    var noc = document.getElementById("createJobPoster_noc").value;
349
350
    // TAL-150
351
    var classification = document.getElementById("createJobPoster_classification").value;
352
353
    var clearance_id = document.getElementById("createJobPoster_clearance").value;
354
355
    var language_id = document.getElementById("createJobPoster_language").value;
356
357
    CreateJobPosterAPI.jobPosterObj = new CreateJobPosterAPI.JobPostNonLocalized(
358
            id, manager_user_id, title, title_fr, department_id, province_id, branch_en, branch_fr, division_en, division_fr, city, city_fr, open_date_time,
359
            close_date_time, start_date, term_qty, remuneration_range_low, remuneration_range_high, impact, impact_fr, key_tasks_en, key_tasks_fr,
360
            core_competencies_en, core_competencies_fr, developing_competencies_en, developing_competencies_fr, questionObjs_en, questionObjs_fr, noc, classification,
361
            clearance_id, language_id);
362
}
0 ignored issues
show
Coding Style introduced by
There should be a semicolon.

Requirement of semicolons purely is a coding style issue since JavaScript has specific rules about semicolons which are followed by all browsers.

Further Readings:

Loading history...
363
364
CreateJobPosterAPI.getTextareaContentsAsList = function (textareaElementId) {
365
    var list = document.getElementById(textareaElementId).value.split(/\r|\n/);
366
    for (var i = (list.length - 1); i >= 0; i--) {
367
        list[i] = list[i].trim();
368
        if (list[i] === "") {
369
            list.splice(i, 1);
370
        }
371
    }
372
    return list;
373
}
0 ignored issues
show
Coding Style introduced by
There should be a semicolon.

Requirement of semicolons purely is a coding style issue since JavaScript has specific rules about semicolons which are followed by all browsers.

Further Readings:

Loading history...
374
375
376
CreateJobPosterAPI.submitJobPosterForm = function () {
377
378
    if (CreateJobPosterAPI.jobPosterObj) {
379
        var jobPosterJson = JSON.stringify(CreateJobPosterAPI.jobPosterObj);
380
381
        //TODO: use the following code instead when updateJobPoster is ready
382
        /*
383
         if (CreateJobPosterAPI.jobObj.id > 0) {
384
         CreateJobPosterAPI.updateJobPoster(jobPosterJson);
385
         } else {
386
         CreateJobPosterAPI.createJobPoster(jobPosterJson);
387
         }
388
         */
389
390
        CreateJobPosterAPI.createJobPoster(jobPosterJson);
391
        return true;
392
    } else {
0 ignored issues
show
Comprehensibility introduced by
else is not necessary here since all if branches return, consider removing it to reduce nesting and make code more readable.
Loading history...
393
        return false;
394
    }
395
};
396
397
CreateJobPosterAPI.hideCreateJobPosterForm = function () {
398
    var jobPosterCreation = document.getElementById("createJobPosterOverlay");
399
    jobPosterCreation.classList.add("hidden");
400
};
401
402
CreateJobPosterAPI.createJobPoster = function (jobPosterJson) {
403
    var createJobPoster_URL = CreateJobPosterAPI.baseURL + "/createJobPoster";
404
    DataAPI.sendRequest(createJobPoster_URL, 'POST', {}, jobPosterJson, function(request) {
0 ignored issues
show
Bug introduced by
The variable DataAPI seems to be never declared. If this is a global, consider adding a /** global: DataAPI */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
405
        CreateJobPosterAPI.postJobPosterComplete(request.response);
406
    });
407
};
408
409
CreateJobPosterAPI.postJobPosterComplete = function (response) {
410
    //TODO
411
    CreateJobPosterAPI.jobPosterObj.id = JSON.parse(response).job_poster_id;
412
413
    CreateJobPosterAPI.goToTab("createJobPosterReviewTab");
414
415
416
417
};
418
419
CreateJobPosterAPI.getManagerProfile = function (responseCallback) {
420
    if (UserAPI.hasSessionUser()) {
0 ignored issues
show
Bug introduced by
The variable UserAPI seems to be never declared. If this is a global, consider adding a /** global: UserAPI */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
421
        var user = UserAPI.getSessionUserAsJSON();
422
        var user_id = user["user_id"];
0 ignored issues
show
Coding Style introduced by
['user_id'] could be written in dot notation.

You can rewrite this statement in dot notation:

var obj = { };
obj['foo'] = 'bar'; // Bad
obj.foo = 'bar'; // Good
Loading history...
423
        var manager_profile_url = CreateJobPosterAPI.baseURL + "/getManagerProfile/" + user_id;
424
        DataAPI.sendRequest(manager_profile_url, 'GET', {}, null, function(request) {
0 ignored issues
show
Bug introduced by
The variable DataAPI seems to be never declared. If this is a global, consider adding a /** global: DataAPI */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
425
            responseCallback(request.response);
426
        });
427
    }
428
};
429