Add: Basic Toast experimentation

This commit is contained in:
Donatas Kirda 2024-05-20 00:46:29 +03:00
parent ff34d5fd96
commit 816bcfc428
Signed by: bloodwiing
GPG Key ID: 63020D8D3F4A164F
2 changed files with 22 additions and 7 deletions

View File

@ -1,8 +1,9 @@
import { addToast } from '$lib/memory/toast.js';
import { getCategories, getCategoriesCached } from '$lib/server/db/category.js';
import { createPost } from '$lib/server/db/post.js';
import { getUserIDOfSession } from '$lib/server/db/user.js';
import { parseIntNull } from '$lib/util.js';
import { error, redirect } from '@sveltejs/kit';
import { error, fail, redirect } from '@sveltejs/kit';
export async function load({ cookies }) {
if (!cookies.get('token')) {
@ -18,10 +19,6 @@ export async function load({ cookies }) {
/** @type {import('@sveltejs/kit').Action} */
async function POST({ request, cookies }) {
if (request.method !== 'POST') {
return;
}
const userToken = cookies.get('token');
if (!userToken) {
@ -38,7 +35,10 @@ async function POST({ request, cookies }) {
}
if (!name || !content) {
error(400, `Not all fields have been filled out`);
// return {
// error: true,
// } /** TODO: use fail() */
return fail(400, {reason: `Not all fields have been filled out`});
}
const category = (await getCategoriesCached([categoryId])).get(categoryId);

View File

@ -1,10 +1,21 @@
<script>
import { applyAction, enhance } from "$app/forms";
import { page } from "$app/stores";
import toasts, { addToast } from "$lib/memory/toast";
/**
* @type {{
* categories: import("$types/base").Category[],
* }}
*/
export let data;
$: {
if ($page.form) {
console.log($page.form);
addToast('error', 'Test', 'cool!', null);
}
};
</script>
<style lang="scss">
@ -15,8 +26,12 @@
}
</style>
<form action="/compose" method="post" class="composeForm">
<form action="/compose" method="post" class="composeForm" use:enhance>
<h1>Compose</h1>
{#if $page.form}
<span>{JSON.stringify($page.form)}</span>
{/if}
<span>{JSON.stringify($toasts)}</span>
<select name="category" id="category">
{#each data.categories as category}
<option value={category.id}></option>