Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Develop CRUD endpoints #8

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions src/app.d.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
/// <reference types="@sveltejs/kit" />

type Todo = {
create_at: Date;
text: string;
done: boolean;
}

// See https://kit.svelte.dev/docs/typescript
// for information about these interfaces
declare namespace App {
Expand All @@ -11,3 +17,4 @@ declare namespace App {

interface Stuff {}
}

6 changes: 5 additions & 1 deletion src/lib/todo-item.svelte
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
<script lang="ts">
export let todo: Todo;
</script>

<div class="todo done">
<form action="" method="">
<input type="hidden" name="done" value="" />
<button aria-label="Mark done/not done" class="toggle" />
</form>

<form action="" method="" class="text">
<input type="text" />
<input type="text" value={todo.text} />
<button aria-label="Save todo" class="save" />
</form>

Expand Down
32 changes: 27 additions & 5 deletions src/routes/index.svelte
Original file line number Diff line number Diff line change
@@ -1,6 +1,28 @@
<script>
<!-- context="module" only get's executed once across all the instances -->
<script context="module" lang="ts">
import type { Load } from "@sveltejs/kit";

export const load: Load = async ({ fetch }) => {
const res = await fetch("/todos.json");

if (res.ok) {
const todos = await res.json();
return {
props: { todos },
};
}
const { message } = await res.json();
return {
error: new Error(message),
};
};
</script>

<script lang="ts">
import TodoItem from "$lib/todo-item.svelte";

export let todos: Todo[];

const title = "Todos";
</script>

Expand All @@ -10,17 +32,17 @@
<div class="todos">
<h1>{title}</h1>

<form action="" method="" class="new">
<form action="/todos.json" method="post" class="new">
<input
type="text"
name="text"
aria-label="Add a todo"
placeholder="+ type to add a todo"
/>
</form>
<TodoItem />
<TodoItem />
<TodoItem />
{#each todos as todo}
<TodoItem {todo} />
{/each}
</div>

<style>
Expand Down
29 changes: 29 additions & 0 deletions src/routes/todos/index.json.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import type { RequestHandler } from "@sveltejs/kit";

let todos: Todo[] = [];

// get
export const get: RequestHandler = async ({ request }) => {
return {
status: 200,
body: todos
}
}

// post
export const post: RequestHandler = async ({ request }) => {
const formData = await request.formData()
todos.push({
create_at: new Date(),
done: false,
text: formData.get("text") as string
})

return {
status: 303,
headers: {
location: "/"
}

}
}