{"size": 2998, "ext": "js", "max_stars_repo_path": "assets/scripts/modules/contact/_send_contact_form.js", "max_stars_repo_name": "habana-tech/kuba-reisen", "max_stars_repo_stars_event_min_datetime": null, "max_stars_repo_stars_event_max_datetime": null, "max_issues_repo_path": "assets/scripts/modules/contact/_send_contact_form.js", "max_issues_repo_name": "habana-tech/kuba-reisen", "max_issues_repo_issues_event_min_datetime": "2019-12-04T22:24:25.000Z", "max_issues_repo_issues_event_max_datetime": "2021-09-02T03:30:52.000Z", "max_forks_repo_path": "assets/scripts/modules/contact/_send_contact_form.js", "max_forks_repo_name": "habana-tech/kuba-reisen", "max_forks_repo_forks_event_min_datetime": null, "max_forks_repo_forks_event_max_datetime": null}
{"max_stars_count": null, "max_issues_count": 11, "max_forks_count": null, "avg_line_length": 28.8269230769, "max_line_length": 99, "alphanum_fraction": 0.5080053369}
import axios from 'axios';
const qs = require('querystring');

class SendContactForm{

    constructor()
    {
        this.form = document.querySelector('form[name="contact_planing"]');
        this.url = this.form ? this.form.action : null;
        this.btnSend = document.querySelector('.btn.sendFormBtn');
        this.sendLoadingDots = document.querySelector('.loading_dots');
        this.formData = new URLSearchParams();

        this.events();
    }

    events()
    {
        this.btnSend.addEventListener('click', this.sendForm.bind(this));
    }

    isValidForm()
    {
        return this.form.checkValidity();
    }

    collectData()
    {
        let that = this;
        this.form.querySelectorAll("input[name], textarea, select").forEach(function (input) {

            let appendCurrent = true;

            //Only add active chekbox and others inputs!
            if (input.type === 'checkbox' && input.checked == false ) {
                appendCurrent = false;
            }

            if (input.type === 'radio' && input.checked === false ) {
                appendCurrent = false;
            }

            if (appendCurrent) {
                that.formData.append(input.name, input.value);
            }
        });
    }


    sendForm(e)
    {
        if (this.isValidForm()) {
            e.preventDefault();
            this.collectData();

            let that = this;
            let config = {
                headers: {
                    'Content-Type': 'application/x-www-form-urlencoded'
                }
            };


            axios.post(this.url, this.formData, config)
                .then(function (response) {
                    // console.log(response);
                    if (response.status === 200) {
                        that.showMessage('success');
                    } else {
                        that.showMessage('error');
                    }
                })
                .catch(function (error) {
                    that.showMessage('error', error.response.data.errors);
                    console.log(error);
                })
                .finally(function () {
                    that.sendLoadingDots.classList.remove("loading_dots--visible");
                });

            this.btnSend.classList.add("invisible");
            this.sendLoadingDots.classList.add("loading_dots--visible");
            this.form.querySelectorAll(".sendFormInfo").forEach(function (msg) {
                msg.classList.add("hidden");
            });
        }
    }

    showMessage(statusCode, errorText = null)
    {
        let msg = document.querySelector(".sendFormInfo__" + statusCode);
        if (msg) {
            msg.classList.remove("hidden");
            if (statusCode === 'error') {
                this.btnSend.classList.remove("invisible");
                document.querySelector('.sendFormInfo__errors__description').innerHTML = errorText;
            }
        }
    }

}

export default SendContactForm;
