Passed
Pull Request — master (#74)
by oleksandr
04:18
created

src/SprykerEco/Yves/Payone/Theme/default/components/molecules/payone-direct-debit/payone-direct-debit.ts   A

Complexity

Total Complexity 8
Complexity/F 1.6

Size

Lines of Code 87
Function Count 5

Duplication

Duplicated Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
wmc 8
eloc 67
mnd 3
bc 3
fnc 5
dl 0
loc 87
bpm 0.6
cpm 1.6
noi 0
c 0
b 0
f 0
rs 10

5 Functions

Rating   Name   Duplication   Size   Complexity  
A PayoneDirectDebit.toggleInputStatus 0 9 2
A PayoneDirectDebit.mapEvents 0 4 1
A PayoneDirectDebit.onModeChange 0 3 1
A PayoneDirectDebit.readyCallback 0 12 1
A PayoneDirectDebit.toggleInputsStatus 0 10 2
1
import Component from 'ShopUi/models/component';
2
3
const BANK_ACCOUNT_MODE_BBAN = 'BBAN';
4
5
export default class PayoneDirectDebit extends Component {
6
    form: HTMLFormElement;
7
    bankAccountModeInputs: HTMLInputElement[];
8
    bankAccountInput: HTMLInputElement;
9
    bankCodeInput: HTMLInputElement;
10
    ibanInput: HTMLInputElement;
11
    bicInput: HTMLInputElement;
12
13
    protected readyCallback(): void {
14
        this.bankAccountModeInputs = <HTMLInputElement[]>Array.from(
15
            this.querySelectorAll(this.bankAccountModeSelector)
16
        );
17
        this.bankAccountInput = <HTMLInputElement>this.querySelector(this.bankAccountSelector);
18
        this.bankCodeInput = <HTMLInputElement>this.querySelector(this.bankCodeSelector);
19
        this.ibanInput = <HTMLInputElement>this.querySelector(this.ibanSelector);
20
        this.bicInput = <HTMLInputElement>this.querySelector(this.bicSelector);
21
22
        this.mapEvents();
23
        this.toggleInputsStatus();
24
    }
25
26
    protected mapEvents(): void {
27
        this.bankAccountModeInputs.forEach((input: HTMLInputElement) => {
28
            input.addEventListener('change', (event: Event) => this.onModeChange(event));
29
        });
30
    }
31
32
    protected onModeChange(event: Event): void {
33
        this.toggleInputsStatus();
34
    }
35
36
    protected toggleInputStatus(input: HTMLInputElement, enable: boolean): void {
37
        if (enable) {
38
            input.removeAttribute('disabled');
39
40
            return;
41
        }
42
43
        input.setAttribute('disabled', 'disabled');
44
    }
45
46
    toggleInputsStatus(): void {
47
        if (!this.bankAccountMode) {
48
            return;
49
        }
50
51
        this.toggleInputStatus(this.bankAccountInput, this.isBBANBankAccountMode);
52
        this.toggleInputStatus(this.bankCodeInput, this.isBBANBankAccountMode);
53
        this.toggleInputStatus(this.ibanInput, !this.isBBANBankAccountMode);
54
        this.toggleInputStatus(this.bicInput, !this.isBBANBankAccountMode);
55
    }
56
57
    get isBBANBankAccountMode(): boolean {
58
        return this.bankAccountMode === BANK_ACCOUNT_MODE_BBAN;
59
    }
60
61
    get bankAccountMode(): string {
62
        const selectedInput = this.bankAccountModeInputs.find((input: HTMLInputElement) => input.checked);
63
64
        return !!selectedInput ? selectedInput.value : '';
65
    }
66
67
    get bankAccountModeSelector(): string {
68
        return this.getAttribute('bank-account-mode-selector');
69
    }
70
71
    get bankAccountSelector(): string {
72
        return this.getAttribute('bank-account-selector');
73
    }
74
75
    get bankCodeSelector(): string {
76
        return this.getAttribute('bank-code-selector');
77
    }
78
79
    get ibanSelector(): string {
80
        return this.getAttribute('iban-selector');
81
    }
82
83
    get bicSelector(): string {
84
        return this.getAttribute('bic-selector');
85
    }
86
}
87