-
Notifications
You must be signed in to change notification settings - Fork 0
/
create.vue
110 lines (97 loc) · 2.45 KB
/
create.vue
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
<template>
<div>
<form @submit="createDraft">
<h1>Create Draft</h1>
<input autoFocus placeholder="Title" type="text" v-model="title" />
<input @keyup="checkAuthorExist" placeholder="Author (email address)" type="text" v-model="authorEmail"
/>
<span v-if="!doesAuthorExist && authorEmail && !isLoading" >'{{ authorEmail }}' does not exists in our database.</span>
<textarea cols="50" placeholder="Content" rows="8" v-model="content" />
<input :class="{'primary': title && authorEmail && doesAuthorExist}" v-bind="{'disabled': !title || !authorEmail || !doesAuthorExist}" type="submit" value="Create" />
<NuxtLink class="back" to="/"> or Cancel </NuxtLink>
</form>
</div>
</template>
<script setup>
const router = useRouter();
let title = ref();
let authorEmail = ref();
let content = ref();
let doesAuthorExist = ref(false);
let isLoading = ref(false);
const checkAuthorExist = async () => {
let body = {
email: authorEmail.value
}
isLoading.value = true;
await fetch('/author', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify(body)
})
.then(async (res)=>{
let data = await res.json();
if(data.length == 0){
doesAuthorExist.value = false;
}
else {
doesAuthorExist.value = true;
}
})
.catch((error)=>{
console.error(error);
});
isLoading.value = false;
}
const createDraft = async (e) => {
e.preventDefault()
const body = {
title: title.value,
authorEmail: authorEmail.value,
content: content.value,
}
await fetch(`/post`, {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify(body),
})
.then(()=>{
router.push({ name: 'drafts' })
})
.catch((error)=>{
console.error(error);
})
}
</script>
<style scoped>
.page {
background: white;
padding: 3rem;
display: flex;
justify-content: center;
align-items: center;
}
input[type='text'],
textarea {
width: 100%;
padding: 0.5rem;
margin: 0.5rem 0;
border-radius: 0.25rem;
border: 0.125rem solid rgba(0, 0, 0, 0.2);
}
input[type='submit'] {
background: #ececec;
border: 0;
padding: 1rem 2rem;
}
.back {
margin-left: 1rem;
}
span {
color: red;
}
.primary {
background: blue !important;
color: white;
}
</style>