Skip to content

Commit

Permalink
Use fail fast for keydown handler instead of having an additional lev…
Browse files Browse the repository at this point in the history
…el (#443)

* docs: apply fail fast for keydown function handler

* style: apply fail fast for if condition
  • Loading branch information
Zerotask authored Dec 14, 2023
1 parent 787c1a2 commit 9581a34
Show file tree
Hide file tree
Showing 6 changed files with 86 additions and 86 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,10 @@
<input
placeholder="what needs to be done?"
on:keydown={(e) => {
if (e.key === 'Enter') {
todos.add(e.currentTarget.value);
e.currentTarget.value = '';
}
if (e.key !== 'Enter') return;

todos.add(e.currentTarget.value);
e.currentTarget.value = '';
}}
/>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,20 +12,20 @@ Inside the `keydown` event handler of the 'add a todo' `<input>`, let's post som
type="text"
autocomplete="off"
on:keydown={async (e) => {
if (e.key === 'Enter') {
const input = e.currentTarget;
const description = input.value;
+++ const response = await fetch('/todo', {
method: 'POST',
body: JSON.stringify({ description }),
headers: {
'Content-Type': 'application/json'
}
});+++
input.value = '';
}
if (e.key !== 'Enter') return;
const input = e.currentTarget;
const description = input.value;
+++ const response = await fetch('/todo', {
method: 'POST',
body: JSON.stringify({ description }),
headers: {
'Content-Type': 'application/json'
}
});+++
input.value = '';
}}
/>
```
Expand Down Expand Up @@ -59,27 +59,27 @@ We're returning a response with a [201 Created](https://http.dog/201) status and
type="text"
autocomplete="off"
on:keydown={async (e) => {
if (e.key === 'Enter') {
const input = e.currentTarget;
const description = input.value;
const response = await fetch('/todo', {
method: 'POST',
body: JSON.stringify({ description }),
headers: {
'Content-Type': 'application/json'
}
});
+++ const { id } = await response.json();
data.todos = [...data.todos, {
id,
description
}];+++
input.value = '';
}
if (e.key !== 'Enter') return;
const input = e.currentTarget;
const description = input.value;
const response = await fetch('/todo', {
method: 'POST',
body: JSON.stringify({ description }),
headers: {
'Content-Type': 'application/json'
}
});
+++ const { id } = await response.json();
data.todos = [...data.todos, {
id,
description
}];+++
input.value = '';
}}
/>
```
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,14 @@
type="text"
autocomplete="off"
on:keydown={async (e) => {
if (e.key === 'Enter') {
const input = e.currentTarget;
const description = input.value;

// TODO handle submit
if (e.key !== 'Enter') return;

input.value = '';
}
const input = e.currentTarget;
const description = input.value;

// TODO handle submit

input.value = '';
}}
/>
</label>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,27 +11,27 @@
type="text"
autocomplete="off"
on:keydown={async (e) => {
if (e.key === 'Enter') {
const input = e.currentTarget;
const description = input.value;

const response = await fetch('/todo', {
method: 'POST',
body: JSON.stringify({ description }),
headers: {
'Content-Type': 'application/json'
}
});
if (e.key !== 'Enter') return;

const { id } = await response.json();
const input = e.currentTarget;
const description = input.value;

const response = await fetch('/todo', {
method: 'POST',
body: JSON.stringify({ description }),
headers: {
'Content-Type': 'application/json'
}
});

data.todos = [...data.todos, {
id,
description
}];
const { id } = await response.json();

input.value = '';
}
data.todos = [...data.todos, {
id,
description
}];

input.value = '';
}}
/>
</label>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,27 +11,27 @@
type="text"
autocomplete="off"
on:keydown={async (e) => {
if (e.key === 'Enter') {
const input = e.currentTarget;
const description = input.value;

const response = await fetch('/todo', {
method: 'POST',
body: JSON.stringify({ description }),
headers: {
'Content-Type': 'application/json'
}
});
if (e.key !== 'Enter') return;

const { id } = await response.json();
const input = e.currentTarget;
const description = input.value;

const response = await fetch('/todo', {
method: 'POST',
body: JSON.stringify({ description }),
headers: {
'Content-Type': 'application/json'
}
});

data.todos = [...data.todos, {
id,
description
}];
const { id } = await response.json();

input.value = '';
}
data.todos = [...data.todos, {
id,
description
}];

input.value = '';
}}
/>
</label>
Expand Down
8 changes: 4 additions & 4 deletions src/routes/tutorial/[slug]/Chrome.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,10 @@
dispatch('change', { value: e.currentTarget.value });
}}
on:keydown={(e) => {
if (e.key === 'Enter') {
dispatch('change', { value: e.currentTarget.value });
e.currentTarget.blur();
}
if (e.key !== 'Enter') return;

dispatch('change', { value: e.currentTarget.value });
e.currentTarget.blur();
}}
/>

Expand Down

1 comment on commit 9581a34

@vercel
Copy link

@vercel vercel bot commented on 9581a34 Dec 14, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.