22 |
About this app
23 |
24 |
25 | This is a SvelteKit app. You can make your own by typing the
26 | following into your command line and following the prompts:
27 |
28 |
29 |
30 |
npm init svelte@next
31 |
32 |
33 | The page you're looking at is purely static HTML, with no client-side interactivity needed.
34 | Because of that, we don't need to load any JavaScript. Try viewing the page's source, or opening
35 | the devtools network panel and reloading.
36 |
37 |
38 |
39 | The TODOs page illustrates SvelteKit's data loading and form handling. Try using
40 | it with JavaScript disabled!
41 |
42 |
43 |
44 |
51 |
--------------------------------------------------------------------------------
/src/routes/todos/index.ts:
--------------------------------------------------------------------------------
1 | import { api } from './_api';
2 | import type { RequestHandler } from './__types';
3 |
4 | export const get: RequestHandler = async ({ locals }) => {
5 | // locals.userid comes from src/hooks.js
6 | const response = await api('get', `todos/${locals.userid}`);
7 |
8 | if (response.status === 404) {
9 | // user hasn't created a todo list.
10 | // start with an empty array
11 | return {
12 | body: {
13 | todos: []
14 | }
15 | };
16 | }
17 |
18 | if (response.status === 200) {
19 | return {
20 | body: {
21 | todos: await response.json()
22 | }
23 | };
24 | }
25 |
26 | return {
27 | status: response.status
28 | };
29 | };
30 |
31 | export const post: RequestHandler = async ({ request, locals }) => {
32 | const form = await request.formData();
33 |
34 | await api('post', `todos/${locals.userid}`, {
35 | text: form.get('text')
36 | });
37 |
38 | return {};
39 | };
40 |
41 | // If the user has JavaScript disabled, the URL will change to
42 | // include the method override unless we redirect back to /todos
43 | const redirect = {
44 | status: 303,
45 | headers: {
46 | location: '/todos'
47 | }
48 | };
49 |
50 | export const patch: RequestHandler = async ({ request, locals }) => {
51 | const form = await request.formData();
52 |
53 | await api('patch', `todos/${locals.userid}/${form.get('uid')}`, {
54 | text: form.has('text') ? form.get('text') : undefined,
55 | done: form.has('done') ? !!form.get('done') : undefined
56 | });
57 |
58 | return redirect;
59 | };
60 |
61 | export const del: RequestHandler = async ({ request, locals }) => {
62 | const form = await request.formData();
63 |
64 | await api('delete', `todos/${locals.userid}/${form.get('uid')}`);
65 |
66 | return redirect;
67 | };
68 |
--------------------------------------------------------------------------------
/src/lib/Header/svelte-logo.svg:
--------------------------------------------------------------------------------
1 |