Passed
Push — master ( dd4450...9a9614 )
by Andrey
05:55
created

web/js/feedback.js   A

Complexity

Total Complexity 20
Complexity/F 1.67

Size

Lines of Code 100
Function Count 12

Duplication

Duplicated Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 71
c 0
b 0
f 0
dl 0
loc 100
rs 10
wmc 20
mnd 8
bc 8
fnc 12
bpm 0.6666
cpm 1.6666
noi 6
1
$(document).ready(function() {
2
3
    var feedbackBlock = $('#feedback');
4
    feedbackBlock.on("click", '[role="send"]', function(e) {
5
        e.preventDefault();
6
        if (recaptcha_status == 'passive' || recaptcha_status == 'unvalid') {
0 ignored issues
show
Bug introduced by
The variable recaptcha_status seems to be never declared. If this is a global, consider adding a /** global: recaptcha_status */ 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...
7
            var textAlert;
8
            switch (recaptcha_status) {
9
                case 'passive':
10
                    textAlert = window.need_captcha;
11
                    break;
12
                case 'unvalid':
13
                    textAlert = window.error_captcha;
14
                    break;
15
            }
16
            showAlert(feedbackBlock, textAlert, 'danger');
0 ignored issues
show
Bug introduced by
The variable textAlert seems to not be initialized for all possible execution paths. Are you sure showAlert handles undefined variables?
Loading history...
17
            return;
18
        }
19
        closeAlert(feedbackBlock);
20
21
        var url = '/ajax/feedback-ajax/send',
22
            nameBlock = feedbackBlock.find('[role="name"]'),
23
            emailBlock = feedbackBlock.find('[role="email"]'),
24
            subjectBlock = feedbackBlock.find('[role="subject"]'),
25
            messageBlock = feedbackBlock.find('[role="message"]'),
26
            params = {
27
                _csrf: window.yii.getCsrfToken(),
28
                name: nameBlock.val(),
29
                email: emailBlock.val(),
30
                subject: subjectBlock.val(),
31
                message: messageBlock.val()//,
32
                //short_language: window.short_language
33
            };
34
35
        AJAX(url, 'POST', params, {
36
            response_json: true,
37
            func_waiting: function () {
38
                showPreloader();
39
            },
40
            func_callback: function (resp) {
41
                hidePreloader();
42
                if (resp.meta.status == 'success') {
43
44
                    feedbackBlock.find('.form-group').each(function () {
45
                        if ($(this).hasClass('has-error')) {
46
                            $(this).removeClass('has-error');
47
                        }
48
                        $(this).find('.help-block').text('');
49
                        $(this).find('.form-control').val('');
50
                    });
51
52
                    showAlert(feedbackBlock, window.sent_message, 'success');
53
                    setTimeout(function () {
54
                        closeAlert(feedbackBlock);
55
                        grecaptcha_reset();
56
                        recaptcha_status = 'passive';
0 ignored issues
show
Bug introduced by
The variable recaptcha_status seems to be never declared. Assigning variables without defining them first makes them global. If this was intended, consider making it explicit like using window.recaptcha_status.
Loading history...
57
                    }, 3000);
58
59
                } else if (resp.meta.status == 'fail') {
60
                    var errors = resp.data.errors;
61
                    feedbackBlock.find('.form-group').each(function () {
62
                        var dataGroup = $(this).attr('data-group');
63
                        var help_block = $('#help_block_'+dataGroup);
64
                        if (dataGroup in errors) {
65
                            if (!$(this).hasClass('has-error')) {
66
                                $(this).addClass('has-error');
67
                            }
68
                            help_block.text(errors[dataGroup][0]);
69
                        } else {
70
                            if ($(this).hasClass('has-error')) {
71
                                $(this).removeClass('has-error');
72
                            }
73
                            help_block.text('');
74
                        }
75
                    });
76
                }
77
            },
78
            func_error: function (resp) {
79
                console.log(resp.message);
0 ignored issues
show
Debugging Code introduced by
console.log looks like debug code. Are you sure you do not want to remove it?
Loading history...
80
            }
81
        });
82
    });
83
});
84
85
var validateRecaptchaFeedback = function () {
86
    validateRecaptcha({
87
        func_waiting: function () {
88
            showPreloader();
89
        },
90
        func_callback_error: function () {
91
            hidePreloader();
92
            recaptcha_status = 'unvalid';
0 ignored issues
show
Bug introduced by
The variable recaptcha_status seems to be never declared. Assigning variables without defining them first makes them global. If this was intended, consider making it explicit like using window.recaptcha_status.
Loading history...
93
        },
94
        func_callback_success: function () {
95
            hidePreloader();
96
            recaptcha_status = 'valid';
0 ignored issues
show
Bug introduced by
The variable recaptcha_status seems to be never declared. Assigning variables without defining them first makes them global. If this was intended, consider making it explicit like using window.recaptcha_status.
Loading history...
97
            closeAlert($('#feedback'));
98
        }
99
    });
100
};