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

EditTeamCultureAPI.populateTeamCultureForm   F

Complexity

Conditions 21
Paths > 20000

Size

Total Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 21
nc 1048576
nop 1
dl 0
loc 1
rs 0
c 0
b 0
f 0

How to fix   Complexity   

Complexity

Complex classes like EditTeamCultureAPI.populateTeamCultureForm often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

1
var EditTeamCultureAPI = {};
2
3
EditTeamCultureAPI.baseURL = "/tc/api/v1";
4
5
EditTeamCultureAPI.TeamCulture = function (teamSize, gcDirUrl, narrativeTextEn, narrativeTextFr, operatingContextEn, operatingContextFr, whatWeValueEn, whatWeValueFr, howWeWorkEn, howWeWorkFr) {
6
    this.team_size = teamSize;
7
    this.gc_directory_url = gcDirUrl;
8
    this.narrative_text_en = narrativeTextEn;
9
    this.narrative_text_fr = narrativeTextFr;
10
    this.operating_context_en = operatingContextEn;
11
    this.operating_context_fr = operatingContextFr;
12
    this.what_we_value_en = whatWeValueEn;
13
    this.what_we_value_fr = whatWeValueFr;
14
    this.how_we_work_en = howWeWorkEn;
15
    this.how_we_work_fr = howWeWorkFr;
16
17
};
18
19
EditTeamCultureAPI.parseTeamCultureResponse = function (responseText) {
20
    var json = JSON.parse(responseText);
21
    return new EditTeamCultureAPI.TeamCulture(json.team_size, json.gc_directory_url, json.narrative_text_en, json.narrative_text_fr, json.operating_context_en, json.operating_context_fr, json.what_we_value_en, json.what_we_value_fr, json.how_we_work_en, json.how_we_work_fr);
22
};
23
24
EditTeamCultureAPI.localizeEditTeamCulture = function () {
25
    if (siteContent) {
0 ignored issues
show
Best Practice introduced by
If you intend to check if the variable siteContent is declared in the current environment, consider using typeof siteContent === "undefined" instead. This is safe if the variable is not actually declared.
Loading history...
26
        try {
27
            document.getElementById("createEditProfile_teamCultureTitle").innerHTML = siteContent.teamCulture;
28
            document.getElementById("createEditProfile_teamSize_label").innerHTML = siteContent.teamSizePrompt;
29
            document.getElementById("createEditProfile_gcDirLink_label").innerHTML = siteContent.gcDirectoryLinkPrompt;
30
            //document.getElementById("createEditProfile_teamNarrative_label").innerHTML = siteContent.teamNarrativePrompt;
31
        } catch (e) {
32
            (console.error || console.log).call(console, e.stack || e);
33
        }
34
35
    }
36
};
37
38
EditTeamCultureAPI.initializeTeamCultureForm = function (managerProfileId) {
39
    //Fill previous form fields
40
    EditTeamCultureAPI.getTeamCulture(managerProfileId, function (response) {
41
        var teamCulture = EditTeamCultureAPI.parseTeamCultureResponse(response);
42
        EditTeamCultureAPI.populateTeamCultureForm(teamCulture);
43
    });
44
};
45
46
/**
47
 *
48
 * @param {EditTeamCultureAPI.TeamCulture} teamCulture
49
 * @return {undefined}
50
 */
51
EditTeamCultureAPI.populateTeamCultureForm = function (teamCulture) {
52
    document.getElementById('createEditProfile_teamSize').value =
53
            teamCulture.team_size ? teamCulture.team_size : "";
54
            if (document.getElementById('createEditProfile_teamSize').value) {
55
                document.getElementById("createEditProfile_teamSize").parentElement.classList.add("active");
56
                document.getElementById("createEditProfile_teamSize").parentElement.classList.add("valid");
57
            }
58
    document.getElementById('createEditProfile_gcDirLink').value =
59
            teamCulture.gc_directory_url ? teamCulture.gc_directory_url : "";
60
            if (document.getElementById('createEditProfile_gcDirLink').value) {
61
                document.getElementById("createEditProfile_gcDirLink").parentElement.classList.add("active");
62
                document.getElementById("createEditProfile_gcDirLink").parentElement.classList.add("valid");
63
            }
64
    document.getElementById('createEditProfile_teamNarrative_en').value =
65
            teamCulture.narrative_text_en ? teamCulture.narrative_text_en : "";
66
            if (document.getElementById('createEditProfile_teamNarrative_en').value) {
67
                document.getElementById("createEditProfile_teamNarrative_en").parentElement.classList.add("active");
68
                document.getElementById("createEditProfile_teamNarrative_en").parentElement.classList.add("valid");
69
            }
70
    document.getElementById('createEditProfile_teamNarrative_fr').value =
71
            teamCulture.narrative_text_fr ? teamCulture.narrative_text_fr : "";
72
            if (document.getElementById('createEditProfile_teamNarrative_fr').value) {
73
                document.getElementById("createEditProfile_teamNarrative_fr").parentElement.classList.add("active");
74
                document.getElementById("createEditProfile_teamNarrative_fr").parentElement.classList.add("valid");
75
            }
76
    document.getElementById('createEditProfile_operatingContext_en').value =
77
            teamCulture.operating_context_en ? teamCulture.operating_context_en : "";
78
            if (document.getElementById('createEditProfile_operatingContext_en').value) {
79
                document.getElementById("createEditProfile_operatingContext_en").parentElement.classList.add("active");
80
                document.getElementById("createEditProfile_operatingContext_en").parentElement.classList.add("valid");
81
            }
82
    document.getElementById('createEditProfile_operatingContext_fr').value =
83
            teamCulture.operating_context_fr ? teamCulture.operating_context_fr : "";
84
            if (document.getElementById('createEditProfile_operatingContext_fr').value) {
85
                document.getElementById("createEditProfile_operatingContext_fr").parentElement.classList.add("active");
86
                document.getElementById("createEditProfile_operatingContext_fr").parentElement.classList.add("valid");
87
            }
88
    document.getElementById('createEditProfile_whatWeValue_en').value =
89
            teamCulture.what_we_value_en ? teamCulture.what_we_value_en : "";
90
            if (document.getElementById('createEditProfile_whatWeValue_en').value) {
91
                document.getElementById("createEditProfile_whatWeValue_en").parentElement.classList.add("active");
92
                document.getElementById("createEditProfile_whatWeValue_en").parentElement.classList.add("valid");
93
            }
94
    document.getElementById('createEditProfile_whatWeValue_fr').value =
95
            teamCulture.what_we_value_fr ? teamCulture.what_we_value_fr : "";
96
            if (document.getElementById('createEditProfile_whatWeValue_fr').value) {
97
                document.getElementById("createEditProfile_whatWeValue_fr").parentElement.classList.add("active");
98
                document.getElementById("createEditProfile_whatWeValue_fr").parentElement.classList.add("valid");
99
            }
100
    document.getElementById('createEditProfile_howWeWork_en').value =
101
            teamCulture.how_we_work_en ? teamCulture.how_we_work_en : "";
102
            if (document.getElementById('createEditProfile_howWeWork_en').value) {
103
                document.getElementById("createEditProfile_howWeWork_en").parentElement.classList.add("active");
104
                document.getElementById("createEditProfile_howWeWork_en").parentElement.classList.add("valid");
105
            }
106
    document.getElementById('createEditProfile_howWeWork_fr').value =
107
            teamCulture.how_we_work_fr ? teamCulture.how_we_work_fr : "";
108
            if (document.getElementById('createEditProfile_howWeWork_fr').value) {
109
                document.getElementById("createEditProfile_howWeWork_fr").parentElement.classList.add("active");
110
                document.getElementById("createEditProfile_howWeWork_fr").parentElement.classList.add("valid");
111
            }
112
};
113
114
EditTeamCultureAPI.submitTeamCulture = function (managerProfileId) {
115
    var teamCulture = new EditTeamCultureAPI.TeamCulture();
116
    teamCulture.team_size = document.getElementById('createEditProfile_teamSize').value;
117
    teamCulture.gc_directory_url = document.getElementById('createEditProfile_gcDirLink').value;
118
    teamCulture.narrative_text_en = document.getElementById('createEditProfile_teamNarrative_en').value;
119
    teamCulture.narrative_text_fr = document.getElementById('createEditProfile_teamNarrative_fr').value;
120
    teamCulture.operating_context_en = document.getElementById('createEditProfile_operatingContext_en').value;
121
    teamCulture.operating_context_fr = document.getElementById('createEditProfile_operatingContext_fr').value;
122
    teamCulture.what_we_value_en = document.getElementById('createEditProfile_whatWeValue_en').value;
123
    teamCulture.what_we_value_fr = document.getElementById('createEditProfile_whatWeValue_fr').value;
124
    teamCulture.how_we_work_en = document.getElementById('createEditProfile_howWeWork_en').value;
125
    teamCulture.how_we_work_fr = document.getElementById('createEditProfile_howWeWork_fr').value;
126
127
    EditTeamCultureAPI.putTeamCulture(managerProfileId, teamCulture, function (response) {
0 ignored issues
show
Unused Code introduced by
The parameter response 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...
128
        //TODO: handle response
129
    });
130
};
131
132
EditTeamCultureAPI.getTeamCulture = function (managerProfileId, responseCallback) {
133
    var url = EditTeamCultureAPI.baseURL + '/getTeamCultureByManagerProfile/' + managerProfileId;
134
    EditTeamCultureAPI.sendHttpRequest(url, 'GET', null, responseCallback);
135
};
136
137
EditTeamCultureAPI.putTeamCulture = function (managerProfileId, teamCulture, responseCallback) {
138
    var url = EditTeamCultureAPI.baseURL + '/putTeamCultureByManagerProfile/' + managerProfileId;
139
    EditTeamCultureAPI.sendHttpRequest(url, 'PUT', JSON.stringify(teamCulture), responseCallback);
140
};
141
142
EditTeamCultureAPI.sendHttpRequest = function (url, restMethod, payload, responseCallback) {
143
    DataAPI.sendRequest(url, restMethod, {}, payload, 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...
144
        responseCallback(request.response);
145
    });
146
};
147