Completed
Push — master ( 2f705a...c06b8c )
by Simon
01:46
created

client/src/js/partialuserforms/partialsubmission.js   A

Complexity

Total Complexity 17
Complexity/F 1.89

Size

Lines of Code 60
Function Count 9

Duplication

Duplicated Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 17
c 1
b 0
f 0
dl 0
loc 60
rs 10
cc 0
nc 1
mnd 3
bc 16
fnc 9
bpm 1.7777
cpm 1.8888
noi 1

1 Function

Rating   Name   Duplication   Size   Complexity  
A partialsubmission.js ➔ ??? 0 1 1
1
const baseDomain = document.baseURI;
2
const submitURL = 'partialuserform';
3
const buttons = () => Array.from(document.body.querySelectorAll('form.userform ul li.step-button-wrapper button'));
4
const formElements = () => Array.from(document.body.querySelectorAll('form.userform [name]'));
5
6
const getElementValue = (element, fieldName) => {
7
  const value = element.value;
8
  if (element.getAttribute('type') === 'select') {
9
    return element[element.selectedIndex].value;
10
  }
11
  if (element.getAttribute('type') === 'radio') {
12
    const name = `[name=${fieldName}]:checked`;
13
    const checkedElement = document.body.querySelector(name);
14
    if (checkedElement !== null) {
15
      return checkedElement.value;
16
    }
17
  }
18
  if (element.getAttribute('type') === 'checkbox') {
19
    const name = `[name="${fieldName}"]:checked`;
20
    const checkedElements = Array.from(document.body.querySelectorAll(name));
21
    const valueArray = [];
22
    if (checkedElements.length > 0) {
23
      checkedElements.forEach((element) => {
24
        valueArray.push(element.value);
25
      });
26
      return valueArray;
27
    }
28
  }
29
  return value;
30
};
31
32
const submitPartial = () => {
33
  const data = new FormData();
34
  formElements().forEach((element) => {
35
    const fieldName = element.getAttribute('name');
36
    const value = getElementValue(element, fieldName);
37
    if (!data.has(fieldName)) {
38
      if (typeof value === 'object') {
39
        value.forEach((arrayValue) => {
40
          data.append(fieldName, arrayValue);
41
        })
42
      } else {
43
        if (value) {
44
          data.append(fieldName, value);
45
        }
46
      }
47
    }
48
  });
49
  const httpRequest = new XMLHttpRequest();
0 ignored issues
show
Bug introduced by
The variable XMLHttpRequest seems to be never declared. If this is a global, consider adding a /** global: XMLHttpRequest */ 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...
50
  httpRequest.open('POST', `${baseDomain}${submitURL}`, true);
51
  httpRequest.send(data);
52
};
53
54
const attachSubmitPartial = (button) => {
55
  button.addEventListener('click', submitPartial);
56
};
57
58
export default function () {
59
  buttons().forEach(attachSubmitPartial);
60
}
61