diff options
| author | Fuwn <[email protected]> | 2022-09-21 18:29:52 -0700 |
|---|---|---|
| committer | Fuwn <[email protected]> | 2022-09-21 18:29:52 -0700 |
| commit | 7051213e0fdbd47ab1c0e6e8a67bda4f9cfba323 (patch) | |
| tree | a0a59d1bf0b800635eb988a8c030cc0b0ed7c7fe /backup/todos/+page.server.ts | |
| download | capybara-markets-7051213e0fdbd47ab1c0e6e8a67bda4f9cfba323.tar.xz capybara-markets-7051213e0fdbd47ab1c0e6e8a67bda4f9cfba323.zip | |
feat: initial commit
Diffstat (limited to 'backup/todos/+page.server.ts')
| -rw-r--r-- | backup/todos/+page.server.ts | 61 |
1 files changed, 61 insertions, 0 deletions
diff --git a/backup/todos/+page.server.ts b/backup/todos/+page.server.ts new file mode 100644 index 0000000..6444b59 --- /dev/null +++ b/backup/todos/+page.server.ts @@ -0,0 +1,61 @@ +import { error } from '@sveltejs/kit'; +import { api } from './api'; +import type { PageServerLoad, Actions } from './$types'; + +type Todo = { + uid: string; + created_at: Date; + text: string; + done: boolean; + pending_delete: boolean; +}; + +export const load: PageServerLoad = async ({ locals }) => { + // locals.userid comes from src/hooks.js + const response = await api('GET', `todos/${locals.userid}`); + + if (response.status === 404) { + // user hasn't created a todo list. + // start with an empty array + return { + todos: [] as Todo[] + }; + } + + if (response.status === 200) { + return { + todos: (await response.json()) as Todo[] + }; + } + + throw error(response.status); +}; + +export const actions: Actions = { + add: async ({ request, locals }) => { + const form = await request.formData(); + + await api('POST', `todos/${locals.userid}`, { + text: form.get('text') + }); + }, + edit: async ({ request, locals }) => { + const form = await request.formData(); + + await api('PATCH', `todos/${locals.userid}/${form.get('uid')}`, { + text: form.get('text') + }); + }, + toggle: async ({ request, locals }) => { + const form = await request.formData(); + + await api('PATCH', `todos/${locals.userid}/${form.get('uid')}`, { + done: !!form.get('done') + }); + }, + delete: async ({ request, locals }) => { + const form = await request.formData(); + + await api('DELETE', `todos/${locals.userid}/${form.get('uid')}`); + } +}; |