All files / app/controllers contact.js

81.25% Statements 13/16
63.63% Branches 7/11
50% Functions 2/4
81.25% Lines 13/16

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113                          1x                 13x 13x           4x 4x 4x                     8x   8x                                                                     1x   1x   1x       1x 1x                                                
/* eslint-disable ember/no-computed-properties-in-native-classes */
import Controller from '@ember/controller';
import { action, computed } from '@ember/object';
import { isEmpty } from '@ember/utils';
import { tracked } from '@glimmer/tracking';
 
/**
    TODO: Uncomment this when SMTP script returns
 
    const mailScriptUrl = 'https://www.kevinboucher.com/cgi-sys/formmail.pl';
    const mailSubject = 'KevinBoucher.com contact email';
 */
 
const contactFormEmail = 'kevin@kevinboucher.com';
 
/**
    Provide Ember controller for the `ContactRoute`.
 
    @module ContactController
    @extends Ember.Controller
 */
export default class ContactController extends Controller {
    gRecaptcha = null;
    reCaptchaResponse = null;
 
    @tracked contactEmail;
    @tracked contactMessage;
    @tracked contactMailTo;
    @tracked contactName;
    @tracked isHuman = false;
    @tracked showBrokenEmailModal = false;
    @tracked showCaptchaError = false;
 
    /**
        Ember computed property that provides the contact form's validity state
        (via native form element `ValidityState`).
 
        @property isFormValid
        @type {Boolean}
     */
    @computed('contactEmail', 'isHuman')
    get isFormValid() {
        const { contactEmail, isHuman } = this;
 
        return isHuman &&
            !isEmpty(contactEmail) &&
            document.querySelector('[id="contact-email-input"]')
                .validity
                .valid;
    }
 
    /**
        Handles the `ember-g-captcha` component's `captchaExpired` event (sets
        flag used by form validation).
 
        @method onCaptchaExpired
     */
    @action onCaptchaExpired() {
        this.isHuman = false;
    }
 
    /**
        Handles the `ember-g-captcha` component's `captchaResolved` event (sets
        flag used by form validation).
 
        @method onCaptchaExpired
     */
    @action onCaptchaResolved(reCaptchaResponse) {
        this.reCaptchaResponse = reCaptchaResponse;
        this.isHuman = true;
    }
 
    /**
        Handles the contact form's submit event by invoking a request to an SMTP
        script that resides on the server.
 
        @method onContactFormSubmit
     */
    @action onContactFormSubmit(event) {
        event.preventDefault();
 
        const { /* contactEmail, */ contactMessage, contactName } = this;
 
        const emailBody = contactName || contactMessage ?
            `?body=${contactName ? `${encodeURI(`${contactName}\n\n`)}` : ''}${contactMessage ? encodeURI(contactMessage) : ''}` :
            '';
 
        this.contactMailTo = `mailto:${contactFormEmail}${emailBody}`;
        this.showBrokenEmailModal = true;
 
        /**
            TODO: Uncomment this when SMTP script returns
 
            fetch(mailScriptUrl, {
                body: JSON.stringify({
                    recipient: contactFormEmail,
                    subject: mailSubject,
                    name: contactName,
                    email: contactEmail,
                    message: contactMessage,
                }),
                method: 'POST',
            })
                .then((response) => {
                    // TODO: Handle server error
                })
                .catch((error) => {
                    // TODO: Handle network error
                });
         */
    }
}