Passed
Push — master ( 9a9614...96b324 )
by Andrey
05:12
created

order.js ➔ removeFromBasket   B

Complexity

Conditions 6

Size

Total Lines 25
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 17
dl 0
loc 25
rs 8.6166
c 0
b 0
f 0
cc 6
1
/**
2
 * @param id
3
 * @param count
4
 */
5
function putToBasket(id, count) {
6
    var url = "/ajax/order-ajax/put-to-basket";
7
    var params = {
8
        _csrf: window.yii.getCsrfToken(),
9
        id: id,
10
        count: count !== undefined ? count : 1
11
    };
12
    AJAX(url, 'POST', params, {
13
        response_json: true,
14
        func_callback: function (resp) {
15
            if (resp.meta.status == 'success') {
16
                $('[role="total_amount"]').html(resp.data.total_amount);
17
            }
18
        },
19
        func_error: function (resp, xhr) {
0 ignored issues
show
Unused Code introduced by
The parameter xhr 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...
20
            console.error(resp.message);
21
        }
22
    });
23
}
24
25
/**
26
 * @param id
27
 * @param count
28
 */
29
function setCountInBasket(id, count) {
30
    var url = "/ajax/order-ajax/set-count-in-basket";
31
    var params = {
32
        _csrf: window.yii.getCsrfToken(),
33
        id: id,
34
        count: count
35
    };
36
    AJAX(url, 'POST', params, {
37
        response_json: true,
38
        func_callback: function (resp) {
39
            if (resp.meta.status == 'success') {
40
                $('[role="total_amount"]').html(resp.data.total_amount);
41
                $('#item_price_'+id).html(resp.data.item_price);
42
                $('#total_price_'+id).html(parseInt(count)*parseFloat(resp.data.item_price));
43
            }
44
        },
45
        func_error: function (resp, xhr) {
0 ignored issues
show
Unused Code introduced by
The parameter xhr 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...
46
            console.error(resp.message);
47
        }
48
    });
49
}
50
51
/**
52
 * @param id
53
 */
54
function removeFromBasket(id) {
55
    var url = "/ajax/order-ajax/remove-from-basket";
56
    var params = {
57
        _csrf: window.yii.getCsrfToken(),
58
        id: id
59
    };
60
    AJAX(url, 'POST', params, {
61
        response_json: true,
62
        func_callback: function (resp) {
63
            if (resp.meta.status == 'success') {
64
                $('#row_order_'+id).remove();
65
                $('[role="total_amount"]').html(resp.data.total_amount);
66
                if (resp.data.total_count == 0) {
0 ignored issues
show
Best Practice introduced by
Comparing resp.data.total_count to 0 using the == operator is not safe. Consider using === instead.
Loading history...
67
                    var send_order_el = $('#send_order_block');
68
                    if (!send_order_el.hasClass('hidden')) {
69
                        send_order_el.addClass('hidden');
70
                    }
71
                }
72
            }
73
        },
74
        func_error: function (resp, xhr) {
0 ignored issues
show
Unused Code introduced by
The parameter xhr 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...
75
            console.error(resp.message);
76
        }
77
    });
78
}
79
80
$(document).ready(function() {
81
82
    var sendOrderForm = $('#main_order_form');
83
    sendOrderForm.on("click", '[role="send"]', function(e) {
84
        e.preventDefault();
85
        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...
86
            var textAlert;
87
            switch (recaptcha_status) {
88
                case 'passive':
89
                    textAlert = window.need_captcha;
90
                    break;
91
                case 'unvalid':
92
                    textAlert = window.error_captcha;
93
                    break;
94
            }
95
            showAlert(sendOrderForm, 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...
96
            return;
97
        }
98
        closeAlert(sendOrderForm);
99
100
        var url = '/ajax/order-ajax/send-order',
101
            nameBlock = sendOrderForm.find('[role="name"]'),
102
            emailBlock = sendOrderForm.find('[role="email"]'),
103
            commentBlock = sendOrderForm.find('[role="comment"]'),
104
            params = {
105
                _csrf: window.yii.getCsrfToken(),
106
                name: nameBlock.val(),
107
                email: emailBlock.val(),
108
                comment: commentBlock.val()
109
            };
110
111
        var quantityBlocks = sendOrderForm.find('[role="quantity"]');
112
        quantityBlocks.each(function () {
113
            params[$(this)[0].name] = $(this)[0].value;
114
        });
115
116
        AJAX(url, 'POST', params, {
117
            response_json: true,
118
            func_waiting: function () {
119
                showPreloader();
120
            },
121
            func_callback: function (resp) {
122
                hidePreloader();
123
                if (resp.meta.status == 'success') {
124
125
                    sendOrderForm.find('.form-group').each(function () {
126
                        if ($(this).hasClass('has-error')) {
127
                            $(this).removeClass('has-error');
128
                        }
129
                        $(this).find('.help-block').text('');
130
                        $(this).find('.form-control').val('');
131
                    });
132
133
                    $('#cart_items').remove();
134
                    $('#send_order_block').remove();
135
                    $('[role="total_amount"]').html(0);
136
                    showAlert(sendOrderForm, resp.meta.message, 'success');
137
138
                } else if (resp.meta.status == 'fail') {
139
                    var errors = resp.data.errors;
140
                    sendOrderForm.find('.form-group').each(function () {
141
                        var dataGroup = $(this).attr('data-group');
142
                        var help_block = $('#help_block_'+dataGroup);
143
                        if (dataGroup in errors) {
144
                            if (!$(this).hasClass('has-error')) {
145
                                $(this).addClass('has-error');
146
                            }
147
                            help_block.text(errors[dataGroup][0]);
148
                        } else {
149
                            if ($(this).hasClass('has-error')) {
150
                                $(this).removeClass('has-error');
151
                            }
152
                            help_block.text('');
153
                        }
154
                    });
155
                }
156
            },
157
            func_error: function (resp) {
158
                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...
159
            }
160
        });
161
    });
162
});
163
164
var validateRecaptchaOrder = function () {
165
    validateRecaptcha({
166
        func_waiting: function () {
167
168
        },
169
        func_callback_error: function () {
170
            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...
171
        },
172
        func_callback_success: function () {
173
            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...
174
        }
175
    });
176
};
177