Giter Club home page Giter Club logo

hapi-mixin's Introduction

HAPI Vue Form Mixins

This mixin is for HAPI Form to post data to the backend server and get responses returned by the server. You can reuse this mixin if your website has more than one form without having to write your own repetitively.

Technically, you can use this mixin on any project as long as the backend is built on top of Laravel or your backend returns the same response structures as Laravel.

If your computer doesn't have bit.dev installed, you would need to run this command to enable npm or yarn able to talk bit.dev.

Kindly run this command.

npm config set @bit:registry https://node.bit.dev

Via npm

npm i @bit/voon.vue.mixins.hapi-mixin

Via yarn

yarn add @bit/voon.vue.mixins.hapi-mixin

Import hapi-mixin into your Vue component.

import HapiMixin from '@bit/voon.vue.mixins.hapi-mixin';

export default {
    mixins: [HapiMixin],
    data() {
        return {
            action: 'Endpoint URL here',
            redirect: '/thank-you',
        }
    }
    ...
}

Secondly, your markups should look something like this.

<form @submit.prevent="submit">
  <div>
    <label style="display: block">Name</label>
    <input type="text" v-model="fields.name" />
    <p v-if="errors && errors.name" v-text="errors.name[0]"></p>
  </div>
  <div>
    <label style="display: block">Email</label>
    <input type="email" v-model="fields.email" />
    <p v-if="errors && errors.email" v-text="errors.email[0]"></p>
  </div>
  <div>
    <label style="display: block">Phone</label>
    <input type="text" v-model="fields.phone" />
    <p v-if="errors && errors.phone" v-text="errors.phone[0]"></p>
  </div>
  <div>
    <label style="display: block">Message</label>
    <textarea type="text" v-model="fields.message"></textarea>
    <p v-if="errors && errors.message" v-text="errors.message[0]"></p>
  </div>
  <button type="submit">Submit</button>
</form>

redirect property

If redirect is set, the form will redirect to the destination accordingly using Vue's router push() method for internal link.

import HapiMixin from '@bit/voon.vue.mixins.hapi-mixin';

export default {
    mixins: [HapiMixin],
    data() {
        return {
            action: 'Endpoint URL here',
            redirect: '/thank-you',
        }
    }
    ...
}

If your redirect is external URL such as google.com. Please provide full URL with http/https scheme.

import HapiMixin from '@bit/voon.vue.mixins.hapi-mixin';

export default {
    mixins: [HapiMixin],
    data() {
        return {
            action: 'Endpoint URL here',
            redirect: 'https://www.google.com',
        }
    }
    ...
}

statusCode() watcher

If your project does not require any redirection, you can use watch method to watch the status code returned by the server. For instance, you wanted to show custom message based on the HTTP response status codes. You can do something like this.

import HapiMixin from '@bit/voon.vue.mixins.hapi-mixin';

export default {
    ...
    watch: {
        statusCode(code) {
            if (code > 200 && code < 400) {
                alert('Form is submitted!')
            }
        }
    }
    ...
}

resetFields() method

If you want your form able to clear all fields, simply call this.resetFields() method.

responseMessage property

Get the server response message whether it is failed or success.

Tips

  1. You don't need to declare the field name at the data(). Use dot notation to assign the field name within the fields: {} instead. Typically, your component would only have action and redirect properties and rest the mixin will do it behind the scene.

  2. Giving bad URL will cause error. Please provide a valid URL if you wish to redirect to external link.

  3. Use environment variable to store your base API endpoint when your website have more than 1 form.

Example of using environment variable (.env) to store base API endpoint.

NUXT_ENV_API_ENDPOINT=https://app.example.com/api/e4a35782-1349-4e75-ab53-fca0e85d0483/forms
// Booking form
import HapiMixin from '@bit/voon.vue.mixins.hapi-mixin';

export default {
    mixins: [HapiMixin],
    data() {
        return {
            action: process.env.NUXT_ENV_API_ENDPOINT + "/23",
            redirect: 'https://www.google.com',
        }
    }
    ...
}

// https://app.example.com/api/e4a35782-1349-4e75-ab53-fca0e85d0483/forms/23

---

// Enquiry form
import HapiMixin from '@bit/voon.vue.mixins.hapi-mixin';

export default {
    mixins: [HapiMixin],
    data() {
        return {
            action: process.env.NUXT_ENV_API_ENDPOINT + "/45",
            redirect: 'https://www.google.com',
        }
    }
    ...
}

// https://app.example.com/api/e4a35782-1349-4e75-ab53-fca0e85d0483/forms/45

hapi-mixin's People

Contributors

juno-w avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.