Passed
Push — master ( 9e8d48...ad4ce1 )
by Daniel
01:34 queued 10s
created

scripts/classes/ProcessInputForm/classProcessInputForm.ts   A

Complexity

Total Complexity 5
Complexity/F 1.25

Size

Lines of Code 81
Function Count 4

Duplication

Duplicated Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
wmc 5
eloc 44
mnd 1
bc 1
fnc 4
dl 0
loc 81
rs 10
bpm 0.25
cpm 1.25
noi 0
c 0
b 0
f 0

4 Functions

Rating   Name   Duplication   Size   Complexity  
A classProcessInputForm.addDataToTable 0 21 1
A classProcessInputForm.sendForm 0 12 1
A classProcessInputForm.processRemoteData 0 18 2
A classProcessInputForm.setInnerHTML 0 8 1
1
// Class imports
2
import classFetchRemoteData from './classFetchRemoteData';
3
import classShowHideElements from './classShowHideElements';
4
import classErrorHandler from '../ErrorHandler/classErrorHandler';
5
6
// Type definition imports
7
import { TStatensVegvesenBilData } from '../../types/typeDefinitions';
8
9
/**
10
 * Class responsible for fetching the remote data
11
 * @property {TStatensVegvesenBilData} remoteBilData Remote data from API
12
 */
13
export default class classProcessInputForm {
14
  private static remoteBilData: TStatensVegvesenBilData;
15
16
  /**
17
   * Send the form, show the loading spinner and fetch remote data
18
   * @param event Event Used to prevent default form submit action
19
   * @returns void
20
   */
21
  public async sendForm(event: Event) {
22
    event.preventDefault();
23
    classShowHideElements.showLoadingSpinner();
24
    classFetchRemoteData.fetchRemoteData().then((response) => {
25
      classProcessInputForm.remoteBilData = response;
26
      classProcessInputForm.processRemoteData();
27
    });
28
  }
29
30
  /**
31
   * Check if we get any errors from the API, if we do, display the error and return
32
   * Otherwise we hide the loading spinner, show the data table, add the data and reset the error text
33
   * @returns void
34
   */
35
  private static processRemoteData() {
36
    if (classProcessInputForm.remoteBilData.melding !== undefined) {
37
      classErrorHandler.displayErrorFromAPI(
38
        classProcessInputForm.remoteBilData
39
      );
40
      classShowHideElements.hideDataTable();
41
      return;
42
    }
43
    classShowHideElements.hideLoadingSpinner();
44
    classShowHideElements.showDataTable();
45
    this.addDataToTable();
46
    classErrorHandler.resetErrorText();
47
  }
48
49
  /**
50
   * Helper function to set the innerHTML attribute for each element
51
   * @param {string} elementId ID of element that we need to modify
52
   * @param {string} value Value that we modify with
53
   */
54
  private static setInnerHTML(elementId: string, value: string) {
55
    window.document.getElementById(elementId)!.innerHTML = value;
56
  }
57
58
  /**
59
   * Set the content of the table <td>s to the fetched remote data
60
   * @returns void
61
   */
62
  private static addDataToTable() {
63
    classProcessInputForm.setInnerHTML(
64
      'kjennemerke',
65
      this.remoteBilData.kjennemerke
66
    );
67
    classProcessInputForm.setInnerHTML(
68
      'forstegangsregistrering',
69
      this.remoteBilData.registrering.forstegangsregistrering
70
    );
71
    classProcessInputForm.setInnerHTML(
72
      'forstegangsregistreringEier',
73
      this.remoteBilData.registrering.forstegangsregistreringEier
74
    );
75
    classProcessInputForm.setInnerHTML(
76
      'sistKontrollert',
77
      this.remoteBilData.periodiskKjoretoykontroll.sistKontrollert
78
    );
79
  }
80
}
81