1
1
import { api } from './_api' ;
2
2
import type { RequestHandler } from '@sveltejs/kit' ;
3
3
4
- export const get : RequestHandler = async ( { request , locals } ) => {
4
+ export const get : RequestHandler = async ( { locals } ) => {
5
5
// locals.userid comes from src/hooks.js
6
- const response = await api ( request , `todos/${ locals . userid } ` ) ;
6
+ const response = await api ( 'get' , `todos/${ locals . userid } ` ) ;
7
7
8
8
if ( response . status === 404 ) {
9
9
// user hasn't created a todo list.
@@ -15,7 +15,7 @@ export const get: RequestHandler = async ({ request, locals }) => {
15
15
} ;
16
16
}
17
17
18
- if ( response . ok ) {
18
+ if ( response . status === 200 ) {
19
19
return {
20
20
body : {
21
21
todos : await response . json ( )
@@ -31,22 +31,37 @@ export const get: RequestHandler = async ({ request, locals }) => {
31
31
export const post : RequestHandler = async ( { request, locals } ) => {
32
32
const form = await request . formData ( ) ;
33
33
34
- return api ( request , `todos/${ locals . userid } ` , {
34
+ await api ( 'post' , `todos/${ locals . userid } ` , {
35
35
text : form . get ( 'text' )
36
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
+ }
37
48
} ;
38
49
39
50
export const patch : RequestHandler = async ( { request, locals } ) => {
40
51
const form = await request . formData ( ) ;
41
52
42
- return api ( request , `todos/${ locals . userid } /${ form . get ( 'uid' ) } ` , {
53
+ await api ( 'patch' , `todos/${ locals . userid } /${ form . get ( 'uid' ) } ` , {
43
54
text : form . has ( 'text' ) ? form . get ( 'text' ) : undefined ,
44
55
done : form . has ( 'done' ) ? ! ! form . get ( 'done' ) : undefined
45
56
} ) ;
57
+
58
+ return redirect ;
46
59
} ;
47
60
48
61
export const del : RequestHandler = async ( { request, locals } ) => {
49
62
const form = await request . formData ( ) ;
50
63
51
- return api ( request , `todos/${ locals . userid } /${ form . get ( 'uid' ) } ` ) ;
64
+ await api ( 'delete' , `todos/${ locals . userid } /${ form . get ( 'uid' ) } ` ) ;
65
+
66
+ return redirect ;
52
67
} ;
0 commit comments