JSFiddle - React, Tailwind, and code Playground

by Changyu Geng

HTML

<script src="https://blueimp.github.io/JavaScript-MD5/js/md5.js"></script>
<input type="emal" size=60 placeholder="Please input your email address and press Ctrl + Enter." v-model="emailAddress" @keyup.enter.ctrl="subscribe">

JavaScript

new Vue({
  el: 'input',
  data: {
    emailAddress: ''
  },
  methods: {
    subscribe() {
      if (!/^(([^<>()[\]\\.,;:\s@"]+(\.[^<>()[\]\\.,;:\s@"]+)*)|(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/.test(this.emailAddress)) {
        return alert('Please input a valid email address.')
      }

      var headers = new Headers()
      headers.append('Content-Type', 'application/json; charset=utf-8')

      var body = `{"email_address":"${this.emailAddress}","status":"subscribed"}`

      fetch(`http://mario-studio.daoapp.io/maillist/subscribe/${md5(this.emailAddress.toLowerCase())}`, {
        method: 'PUT',
        headers,
        body
      }).then(res => res.json()).then(json => {
        this.emailAddress = ''
        alert('Thanks for subscribing.')
      }).catch(err => {
        alert('Oops... something wrong.')
      })
    }
  }
})