Just some linting
This commit is contained in:
parent
6b799308da
commit
d9a97521a1
@ -4,7 +4,7 @@
|
||||
"beforeBuildCommand": "npm run build",
|
||||
"beforeDevCommand": "npm run dev",
|
||||
"devPath": "http://localhost:5173",
|
||||
"distDir": "../.svelte-kit/output/dist"
|
||||
"distDir": "../.svelte-kit/output/client"
|
||||
},
|
||||
"package": {
|
||||
"productName": "ziemniak",
|
||||
|
@ -1,4 +1,4 @@
|
||||
import { invalidate } from '$app/navigation';
|
||||
import { invalidate } from "$app/navigation";
|
||||
|
||||
// this action (https://svelte.dev/tutorial/actions) allows us to
|
||||
// progressively enhance a <form> that already works without JS
|
||||
@ -7,14 +7,20 @@ export function enhance(
|
||||
{
|
||||
pending,
|
||||
error,
|
||||
result
|
||||
result,
|
||||
}: {
|
||||
pending?: ({ data, form }: { data: FormData; form: HTMLFormElement }) => void;
|
||||
pending?: ({
|
||||
data,
|
||||
form,
|
||||
}: {
|
||||
data: FormData;
|
||||
form: HTMLFormElement;
|
||||
}) => void;
|
||||
error?: ({
|
||||
data,
|
||||
form,
|
||||
response,
|
||||
error
|
||||
error,
|
||||
}: {
|
||||
data: FormData;
|
||||
form: HTMLFormElement;
|
||||
@ -24,7 +30,7 @@ export function enhance(
|
||||
result?: ({
|
||||
data,
|
||||
form,
|
||||
response
|
||||
response,
|
||||
}: {
|
||||
data: FormData;
|
||||
response: Response;
|
||||
@ -47,9 +53,9 @@ export function enhance(
|
||||
const response = await fetch(form.action, {
|
||||
method: form.method,
|
||||
headers: {
|
||||
accept: 'application/json'
|
||||
accept: "application/json",
|
||||
},
|
||||
body: data
|
||||
body: data,
|
||||
});
|
||||
|
||||
if (token !== current_token) return;
|
||||
@ -58,7 +64,7 @@ export function enhance(
|
||||
if (result) result({ data, form, response });
|
||||
|
||||
const url = new URL(form.action);
|
||||
url.search = url.hash = '';
|
||||
url.search = url.hash = "";
|
||||
invalidate(url.href);
|
||||
} else if (error) {
|
||||
error({ data, form, error: null, response });
|
||||
@ -74,11 +80,11 @@ export function enhance(
|
||||
}
|
||||
}
|
||||
|
||||
form.addEventListener('submit', handle_submit);
|
||||
form.addEventListener("submit", handle_submit);
|
||||
|
||||
return {
|
||||
destroy() {
|
||||
form.removeEventListener('submit', handle_submit);
|
||||
}
|
||||
form.removeEventListener("submit", handle_submit);
|
||||
},
|
||||
};
|
||||
}
|
||||
|
@ -11,12 +11,16 @@
|
||||
|
||||
const base = "https://api.svelte.dev";
|
||||
|
||||
export function api(method: string, resource: string, data?: Record<string, unknown>) {
|
||||
export function api(
|
||||
method: string,
|
||||
resource: string,
|
||||
data?: Record<string, unknown>
|
||||
) {
|
||||
return fetch(`${base}/${resource}`, {
|
||||
method,
|
||||
headers: {
|
||||
'content-type': 'application/json'
|
||||
"content-type": "application/json",
|
||||
},
|
||||
body: data && JSON.stringify(data)
|
||||
body: data && JSON.stringify(data),
|
||||
});
|
||||
}
|
@ -1,7 +1,7 @@
|
||||
<script lang="ts">
|
||||
import { enhance } from '$lib/form';
|
||||
import { scale } from 'svelte/transition';
|
||||
import { flip } from 'svelte/animate';
|
||||
import { enhance } from "$lib/form";
|
||||
import { scale } from "svelte/transition";
|
||||
import { flip } from "svelte/animate";
|
||||
|
||||
type Todo = {
|
||||
uid: string;
|
||||
@ -29,10 +29,14 @@
|
||||
use:enhance={{
|
||||
result: async ({ form }) => {
|
||||
form.reset();
|
||||
}
|
||||
},
|
||||
}}
|
||||
>
|
||||
<input name="text" aria-label="Add todo" placeholder="+ tap to add a todo" />
|
||||
<input
|
||||
name="text"
|
||||
aria-label="Add todo"
|
||||
placeholder="+ tap to add a todo"
|
||||
/>
|
||||
</form>
|
||||
|
||||
{#each todos as todo (todo.uid)}
|
||||
@ -47,18 +51,31 @@
|
||||
method="post"
|
||||
use:enhance={{
|
||||
pending: ({ data }) => {
|
||||
todo.done = !!data.get('done');
|
||||
}
|
||||
todo.done = !!data.get("done");
|
||||
},
|
||||
}}
|
||||
>
|
||||
<input type="hidden" name="uid" value={todo.uid} />
|
||||
<input type="hidden" name="done" value={todo.done ? '' : 'true'} />
|
||||
<button class="toggle" aria-label="Mark todo as {todo.done ? 'not done' : 'done'}" />
|
||||
<input type="hidden" name="done" value={todo.done ? "" : "true"} />
|
||||
<button
|
||||
class="toggle"
|
||||
aria-label="Mark todo as {todo.done ? 'not done' : 'done'}"
|
||||
/>
|
||||
</form>
|
||||
|
||||
<form class="text" action="/todos?_method=PATCH" method="post" use:enhance>
|
||||
<form
|
||||
class="text"
|
||||
action="/todos?_method=PATCH"
|
||||
method="post"
|
||||
use:enhance
|
||||
>
|
||||
<input type="hidden" name="uid" value={todo.uid} />
|
||||
<input aria-label="Edit todo" type="text" name="text" value={todo.text} />
|
||||
<input
|
||||
aria-label="Edit todo"
|
||||
type="text"
|
||||
name="text"
|
||||
value={todo.text}
|
||||
/>
|
||||
<button class="save" aria-label="Save todo" />
|
||||
</form>
|
||||
|
||||
@ -66,11 +83,15 @@
|
||||
action="/todos?_method=DELETE"
|
||||
method="post"
|
||||
use:enhance={{
|
||||
pending: () => (todo.pending_delete = true)
|
||||
pending: () => (todo.pending_delete = true),
|
||||
}}
|
||||
>
|
||||
<input type="hidden" name="uid" value={todo.uid} />
|
||||
<button class="delete" aria-label="Delete todo" disabled={todo.pending_delete} />
|
||||
<button
|
||||
class="delete"
|
||||
aria-label="Delete todo"
|
||||
disabled={todo.pending_delete}
|
||||
/>
|
||||
</form>
|
||||
</div>
|
||||
{/each}
|
||||
|
@ -1,38 +1,38 @@
|
||||
import { api } from './_api';
|
||||
import type { RequestHandler } from './__types';
|
||||
import { api } from "./_api";
|
||||
import type { RequestHandler } from "./__types";
|
||||
|
||||
export const GET: RequestHandler = async ({ locals }) => {
|
||||
// locals.userid comes from src/hooks.js
|
||||
const response = await api('GET', `todos/${locals.userid}`);
|
||||
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 {
|
||||
body: {
|
||||
todos: []
|
||||
}
|
||||
todos: [],
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
if (response.status === 200) {
|
||||
return {
|
||||
body: {
|
||||
todos: await response.json()
|
||||
}
|
||||
todos: await response.json(),
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
return {
|
||||
status: response.status
|
||||
status: response.status,
|
||||
};
|
||||
};
|
||||
|
||||
export const POST: RequestHandler = async ({ request, locals }) => {
|
||||
const form = await request.formData();
|
||||
|
||||
await api('POST', `todos/${locals.userid}`, {
|
||||
text: form.get('text')
|
||||
await api("POST", `todos/${locals.userid}`, {
|
||||
text: form.get("text"),
|
||||
});
|
||||
|
||||
return {};
|
||||
@ -43,16 +43,16 @@ export const POST: RequestHandler = async ({ request, locals }) => {
|
||||
const redirect = {
|
||||
status: 303,
|
||||
headers: {
|
||||
location: '/todos'
|
||||
}
|
||||
location: "/todos",
|
||||
},
|
||||
};
|
||||
|
||||
export const PATCH: RequestHandler = async ({ request, locals }) => {
|
||||
const form = await request.formData();
|
||||
|
||||
await api('PATCH', `todos/${locals.userid}/${form.get('uid')}`, {
|
||||
text: form.has('text') ? form.get('text') : undefined,
|
||||
done: form.has('done') ? !!form.get('done') : undefined
|
||||
await api("PATCH", `todos/${locals.userid}/${form.get("uid")}`, {
|
||||
text: form.has("text") ? form.get("text") : undefined,
|
||||
done: form.has("done") ? !!form.get("done") : undefined,
|
||||
});
|
||||
|
||||
return redirect;
|
||||
@ -61,7 +61,7 @@ export const PATCH: RequestHandler = async ({ request, locals }) => {
|
||||
export const DELETE: RequestHandler = async ({ request, locals }) => {
|
||||
const form = await request.formData();
|
||||
|
||||
await api('DELETE', `todos/${locals.userid}/${form.get('uid')}`);
|
||||
await api("DELETE", `todos/${locals.userid}/${form.get("uid")}`);
|
||||
|
||||
return redirect;
|
||||
};
|
||||
|
@ -1,5 +1,5 @@
|
||||
import adapter from '@sveltejs/adapter-auto';
|
||||
import preprocess from 'svelte-preprocess';
|
||||
import adapter from "@sveltejs/adapter-auto";
|
||||
import preprocess from "svelte-preprocess";
|
||||
|
||||
/** @type {import('@sveltejs/kit').Config} */
|
||||
const config = {
|
||||
@ -12,9 +12,9 @@ const config = {
|
||||
|
||||
// Override http methods in the Todo forms
|
||||
methodOverride: {
|
||||
allowed: ['PATCH', 'DELETE']
|
||||
}
|
||||
}
|
||||
allowed: ["PATCH", "DELETE"],
|
||||
},
|
||||
},
|
||||
};
|
||||
|
||||
export default config;
|
||||
|
Loading…
x
Reference in New Issue
Block a user