show errors for saving bots

This commit is contained in:
Ilion Beyst 2022-03-06 16:58:01 +01:00
parent 148178a344
commit a8c913f284

View file

@ -8,6 +8,8 @@
let selectedOpponent = undefined; let selectedOpponent = undefined;
let botName: string | undefined = undefined; let botName: string | undefined = undefined;
let saveErrorText = undefined;
onMount(async () => { onMount(async () => {
const res = await fetch("/api/bots", { const res = await fetch("/api/bots", {
headers: { headers: {
@ -62,8 +64,15 @@
let responseData = await response.json(); let responseData = await response.json();
if (response.ok) { if (response.ok) {
dispatch("botSaved", responseData); dispatch("botSaved", responseData);
// clear errors
saveErrorText = undefined;
} else { } else {
throw responseData; if (responseData["error"] === "BotNameTaken") {
saveErrorText = "Bot name is already taken";
} else {
// unexpected error
throw responseData;
}
} }
} }
</script> </script>
@ -84,6 +93,9 @@
<div class="save-form"> <div class="save-form">
<h4>Save your bot</h4> <h4>Save your bot</h4>
<input type="text" class="bot-name-input" placeholder="bot name" bind:value={botName} /> <input type="text" class="bot-name-input" placeholder="bot name" bind:value={botName} />
{#if saveErrorText}
<div class="error-text">{saveErrorText}</div>
{/if}
<button class="submit-button save-button" on:click={saveBot}>Save</button> <button class="submit-button save-button" on:click={saveBot}>Save</button>
</div> </div>
</div> </div>
@ -104,6 +116,10 @@
margin-top: 8em; margin-top: 8em;
} }
.error-text {
color: red;
}
.submit-button { .submit-button {
padding: 8px 16px; padding: 8px 16px;
border-radius: 8px; border-radius: 8px;