decoupling-drupal-vuejs/vuejs/src/components/SessionForm.vue

98 lines
2.4 KiB
Vue
Raw Normal View History

2019-06-02 23:36:07 +01:00
<template>
<section class="mt-8">
2019-06-05 09:15:31 +01:00
<h2 class="text-2xl mb-4">Submit a Session</h2>
2019-06-02 23:36:07 +01:00
2019-08-30 20:02:25 +01:00
<SessionFormMessage :messages="messages" class="bg-green-100 border-green-300"/>
<SessionFormMessage :messages="errors" class="bg-red-100 border-red-300"/>
2019-06-04 21:18:13 +01:00
2019-06-02 23:36:07 +01:00
<form action="" @submit.prevent="submit">
<label class="block mb-4">
Title
2019-06-05 09:12:54 +01:00
<input name="title" type="text" v-model="form.title" required/>
2019-06-02 23:36:07 +01:00
</label>
<label class="block mb-4">
Abstract
2019-06-05 09:12:54 +01:00
<textarea name="title" rows="5" v-model="form.body" required/>
2019-06-02 23:36:07 +01:00
</label>
<input class="cursor-pointer bg-blue-500 hover:bg-blue-700 focus:bg-blue-700 text-gray-100 px-4 py-2 rounded" type="submit" value="Submit session">
</form>
</section>
</template>
<script>
import axios from 'axios'
2019-08-30 20:06:09 +01:00
import map from 'lodash/map'
2019-06-04 21:42:31 +01:00
import SessionFormMessage from '@/components/SessionFormMessage'
2019-06-02 23:36:07 +01:00
export default {
2019-06-04 21:42:31 +01:00
components: {
SessionFormMessage
},
2019-06-02 23:36:07 +01:00
data () {
return {
2019-06-04 21:18:13 +01:00
errors: [],
2019-06-02 23:36:07 +01:00
form: {
body: '',
2019-08-30 19:38:20 +01:00
field_session_status: 'submitted',
2019-06-03 00:54:25 +01:00
field_session_type: 'full',
2019-06-04 21:22:10 +01:00
title: ''
2019-06-04 21:31:43 +01:00
},
messages: []
2019-06-02 23:36:07 +01:00
}
},
methods: {
submit () {
const adminUuid = '11dad4c2-baa8-4fb2-97c6-12e1ce925806'
const apiUuid = '63936126-87cd-4166-9cb4-63b61a210632'
2019-06-02 23:50:01 +01:00
2019-06-03 00:54:25 +01:00
const data = {
type: 'node--session',
attributes: this.form,
relationships: {
2019-06-04 21:27:12 +01:00
'field_speakers': {
'data': {
'id': adminUuid,
'type': 'user--user'
}
},
'uid': {
'data': {
'id': apiUuid,
2019-06-04 21:27:12 +01:00
'type': 'user--user'
2019-06-03 00:54:25 +01:00
}
2019-06-04 21:27:12 +01:00
}
2019-06-03 00:54:25 +01:00
}
}
const baseUrl = process.env.VUE_APP_DRUPAL_URL
2019-06-04 21:21:55 +01:00
2019-06-03 00:54:25 +01:00
axios({
method: 'post',
2019-06-04 21:21:55 +01:00
url: `${baseUrl}/jsonapi/node/session`,
2019-06-03 00:54:25 +01:00
data: { data },
headers: {
'Accept': 'application/vnd.api+json',
'Authorization': 'Basic YXBpOmFwaQ==',
2019-06-04 21:27:12 +01:00
'Content-Type': 'application/vnd.api+json'
2019-06-03 00:54:25 +01:00
}
2019-06-05 20:37:45 +01:00
}).then(({ data }) => {
2019-06-05 20:52:00 +01:00
this.errors = []
this.messages = []
2019-06-05 20:37:45 +01:00
const title = data.data.attributes.title
2019-06-05 19:23:45 +01:00
this.messages.push(`Session ${title} has been created.`)
2019-06-04 21:31:43 +01:00
2019-06-05 19:23:45 +01:00
this.form.body = ''
this.form.title = ''
}).catch(({ response: { data } }) => {
2019-08-30 20:06:09 +01:00
this.errors = map(data.errors, 'detail')
2019-06-05 19:23:45 +01:00
})
2019-06-02 23:36:07 +01:00
}
}
}
</script>