Skip to content

Commit

Permalink
fixed form submit routes
Browse files Browse the repository at this point in the history
  • Loading branch information
MichaelHolley committed Jan 12, 2025
1 parent ef1e480 commit 9e65131
Show file tree
Hide file tree
Showing 6 changed files with 25 additions and 7 deletions.
3 changes: 1 addition & 2 deletions src/lib/components/Habit/HabitOverviewItem.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,7 @@
{habit.name} <span class="text-accent">&rsaquo;</span>
</span></a
>
<form method="post" action="?/addToday" use:enhance>
<input type="hidden" name="habitId" value={habit.id} />
<form method="POST" action="{habit.id}?/addToday" use:enhance>
<button
class="btn btn-outline btn-accent btn-xs"
title="Add Today"
Expand Down
2 changes: 1 addition & 1 deletion src/routes/(app)/+layout.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
<span class="text-xs">
Logged in as <span class="text-primary">{data.user?.username ?? 'UNDEFINED'}</span>
</span>
<form method="post" action="?/logout" use:enhance>
<form method="POST" action="/?/logout" use:enhance>
<button class="text-md btn btn-link link-accent btn-xs px-0">Sign out</button>
</form>
</div>
Expand Down
19 changes: 19 additions & 0 deletions src/routes/(app)/[id]/+page.server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { deleteHabit, getHabitForUser, updateDates } from '$lib/server/habit';
import { Prisma } from '@prisma/client';
import { redirect } from '@sveltejs/kit';
import type { Actions, PageServerLoad } from './$types';
import dayjs from 'dayjs';

export const load: PageServerLoad = async (event) => {
if (!event.locals.user) {
Expand Down Expand Up @@ -46,5 +47,23 @@ export const actions: Actions = {
await deleteHabit(event.params.id, event.locals.user.id);

return redirect(302, `/`);
},
addToday: async (event) => {
if (!event.locals.user) {
return redirect(302, '/');
}

const today = dayjs();
const habit = await getHabitForUser(event.params.id, event.locals.user.id);

if (habit && habit.dates && typeof habit.dates === 'object' && Array.isArray(habit.dates)) {
const dates = habit.dates as Prisma.JsonArray;

if (!dates.includes(today.format('YYYY-MM-DD'))) {
dates.push(today.format('YYYY-MM-DD'));
}

await updateDates(event.params.id, event.locals.user.id, dates as string[]);
}
}
};
4 changes: 2 additions & 2 deletions src/routes/(app)/[id]/+page.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
</div>

<form
method="post"
method="POST"
action="?/addDate"
use:enhance
class="flex max-w-md grow flex-col items-end gap-3"
Expand All @@ -58,7 +58,7 @@
<p class="py-4">Are you sure you want to delete this habit?</p>
<p>This action cannot be undone.</p>
<div class="modal-action">
<form method="post" action="?/delete" use:enhance>
<form method="POST" action="?/delete" use:enhance>
<button class="btn btn-error">Delete</button>
</form>
</div>
Expand Down
2 changes: 1 addition & 1 deletion src/routes/(app)/create/+page.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

<div class="max-w-md">
<h3 class="pb-4 text-3xl">Create Habit</h3>
<form method="post" action="?/createHabit" use:enhance class="flex flex-col items-end gap-3">
<form method="POST" action="?/createHabit" use:enhance class="flex flex-col items-end gap-3">
<label class="flex w-full flex-col gap-1 text-sm">
Name
<input name="name" class="input input-bordered" />
Expand Down
2 changes: 1 addition & 1 deletion src/routes/auth/+page.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
<h1 class="pb-2 text-2xl text-primary">Welcome to HabitKit</h1>
<p>Keep track of your habits and goals</p>
</div>
<form method="post" action="?/login" use:enhance class="flex flex-col gap-3">
<form method="POST" action="?/login" use:enhance class="flex flex-col gap-3">
<label class="flex flex-col gap-1 text-sm">
Username
<input name="username" class="input input-bordered" />
Expand Down

0 comments on commit 9e65131

Please sign in to comment.