-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
56e18de
commit d0939b8
Showing
6 changed files
with
107 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,8 @@ | ||
<button class="link-hover link link-primary" onclick={() => history.back()}> | ||
← Go back | ||
</button> | ||
<script> | ||
import { goto } from '$app/navigation'; | ||
const { backUrl } = $props(); | ||
</script> | ||
|
||
<div> | ||
<button class="link-hover link link-primary" onclick={() => goto(backUrl)}>← Go back</button> | ||
</div> |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
import { getHabitForUser, updateDates } from '$lib/server/habit'; | ||
import { redirect } from '@sveltejs/kit'; | ||
import type { PageServerLoad } from './$types'; | ||
import type { Actions } from '../$types'; | ||
import { Prisma } from '@prisma/client'; | ||
|
||
export const load: PageServerLoad = async (event) => { | ||
if (!event.locals.user) { | ||
return redirect(302, '/'); | ||
} | ||
|
||
const habit = await getHabitForUser(event.params.id, event.locals.user.id); | ||
|
||
if (habit === null) { | ||
return redirect(302, '/'); | ||
} | ||
|
||
return { habit: habit }; | ||
}; | ||
|
||
export const actions: Actions = { | ||
delete: async (event) => { | ||
if (!event.locals.user) { | ||
return redirect(302, '/'); | ||
} | ||
|
||
let habit = await getHabitForUser(event.params.id, event.locals.user.id); | ||
|
||
if (habit === null) { | ||
return redirect(302, '/'); | ||
} | ||
|
||
const formData = await event.request.formData(); | ||
const date = formData.get('date') as string; | ||
|
||
if (!date) { | ||
return redirect(302, `/${event.params.id}/values`); | ||
} | ||
|
||
if (habit && habit.dates && typeof habit.dates === 'object' && Array.isArray(habit.dates)) { | ||
const dates = habit.dates as Prisma.JsonArray; | ||
const indexOfDate = dates.indexOf(date); | ||
|
||
if (indexOfDate !== -1) { | ||
dates.splice(indexOfDate, 1); | ||
habit = await updateDates(event.params.id, event.locals.user.id, dates as string[]); | ||
} | ||
} | ||
|
||
redirect(302, `/${event.params.id}/values`); | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
<script lang="ts"> | ||
import { enhance } from '$app/forms'; | ||
import NavigateBackButton from '$lib/components/NavigateBackButton.svelte'; | ||
import type { PageData } from './$types'; | ||
let { data }: { data: PageData } = $props(); | ||
</script> | ||
|
||
<div class="mb-3"> | ||
<NavigateBackButton backUrl="/{data.habit.id}" /> | ||
</div> | ||
|
||
<h2 class="mb-3 text-3xl">{data.habit?.name}</h2> | ||
<div class="max-w-lg overflow-x-auto"> | ||
<table class="table table-xs"> | ||
<thead> | ||
<tr> | ||
<th>Date</th> | ||
<th>Action</th> | ||
</tr> | ||
</thead> | ||
<tbody> | ||
{#each data.habit?.dates as date} | ||
<tr> | ||
<td>{date}</td> | ||
<td> | ||
<form method="POST" action="?/delete"> | ||
<input type="hidden" name="date" value={date} /> | ||
<button class="btn btn-outline btn-error btn-xs">Delete</button> | ||
</form> | ||
</td> | ||
</tr> | ||
{/each} | ||
</tbody> | ||
</table> | ||
</div> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters