feedback.js 2.07 KB
// eslint-disable-next-line import/prefer-default-export
export function initFeedbackForm() {
    $(document).ready(function () {
        $('#submit').removeAttr('disabled');

        $('#submit').click(function () {
            $('.error').hide();
            $('.error ul').empty();
            var hasError = false;
            var nameVal = $('#contactName').val();
            if (nameVal == '') {
                $('.error ul').append('<li>Please give us your name</li>');
                hasError = true;
            }

            var emailVal = $('#contactEmail').val();
            var emailReg = /^([\w-\.]+@([\w-]+\.)+[\w-]{2,4})?$/;
            if (emailVal == '') {
                $('.error ul').append('<li>Please give us your email</li>');
                hasError = true;
            }
            if (!emailReg.test(emailVal)) {
                $('.error ul').append('<li>Please input a valid email</li>');
                hasError = true;
            }

            var msgVal = $('#message').val();
            if (msgVal == '') {
                $('.error ul').append('<li>Please enter your message</li>');
                hasError = true;
            }

            var companyVal = $('#company').val();

            if (hasError) {
                $('.error').show();
            } else {
                $(this).attr('disabled', 'true');
                $(this).attr('value', 'Sending...');

                $.post('/feedback/send-mail', { contactName: nameVal, company: companyVal, contactEmail: emailVal, message: msgVal }, function (response) {
                    $('#feedbackForm').after('<p class="success">Thank you. Your email to FootyRoom has been sent</p>');
                    $('#submit').attr('value', 'Sent');
                })

                    .fail(function (response) {
                        $('#feedbackForm').after('<p class="error">Whoops, looks like something went wrong. Try again later or report this to us.</p>');
                        $('#submit').attr('value', 'Sent');
                    });
            }

            return false;
        });
    });
}