add validation

This commit is contained in:
kevmodrome 2020-05-27 13:48:38 +02:00
parent 3a72eddd78
commit 9392de550a
1 changed files with 47 additions and 16 deletions

View File

@ -11,26 +11,44 @@
let name = "" let name = ""
let description = "" let description = ""
let loading = false let loading = false
let error = {}
const createNewApp = async () => { const createNewApp = async () => {
const data = { name, description } if ((name.length > 100 || name.length < 1) && description.length < 1) {
loading = true error = {
try { name: true,
const response = await fetch("/api/applications", { description: true,
method: "POST", // *GET, POST, PUT, DELETE, etc. }
credentials: "same-origin", // include, *same-origin, omit } else if (description.length < 1) {
headers: { error = {
"Content-Type": "application/json", name: false,
// 'Content-Type': 'application/x-www-form-urlencoded', description: true,
}, }
body: JSON.stringify(data), // body data type must match "Content-Type" header } else if (name.length > 100 || name.length < 1) {
}) error = {
name: true,
}
} else {
error = {}
const data = { name, description }
loading = true
try {
const response = await fetch("/api/applications", {
method: "POST", // *GET, POST, PUT, DELETE, etc.
credentials: "same-origin", // include, *same-origin, omit
headers: {
"Content-Type": "application/json",
// 'Content-Type': 'application/x-www-form-urlencoded',
},
body: JSON.stringify(data), // body data type must match "Content-Type" header
})
const res = await response.json() const res = await response.json()
$goto(`./${res._id}`) $goto(`./${res._id}`)
} catch (error) { } catch (error) {
console.error(error) console.error(error)
}
} }
} }
@ -60,11 +78,19 @@
placeholder="Enter application name" placeholder="Enter application name"
on:change={e => (name = e.target.value)} on:change={e => (name = e.target.value)}
on:input={e => (name = e.target.value)} /> on:input={e => (name = e.target.value)} />
{#if error.name}
<span class="error">You need to enter a name for your application.</span>
{/if}
<TextArea <TextArea
bind:value={description} bind:value={description}
name="description" name="description"
label="Description" label="Description"
placeholder="Describe your application" /> placeholder="Describe your application" />
{#if error.description}
<span class="error">
Please enter a short description of your application
</span>
{/if}
</div> </div>
<div class="footer"> <div class="footer">
<a href="./#" class="info"> <a href="./#" class="info">
@ -163,4 +189,9 @@
.spinner-text { .spinner-text {
font-size: 2em; font-size: 2em;
} }
.error {
color: var(--deletion100);
font-weight: bold;
font-size: 0.8em;
}
</style> </style>