Skip to content

Commit bc394be

Browse files
Merge pull request #18 from MichaelHolley/feature/16-habit-props-edit
Edit Habits
2 parents aad9590 + ee77d2d commit bc394be

File tree

14 files changed

+103
-20
lines changed

14 files changed

+103
-20
lines changed
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
ALTER TABLE `Habit` CHANGE `name` `title` VARCHAR(191) NOT NULL;

prisma/schema.prisma

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ model Session {
3131

3232
model Habit {
3333
id String @id @default(uuid())
34-
name String
34+
title String
3535
description String? @db.VarChar(255)
3636
userId String
3737
createdAt DateTime @default(now())

src/lib/components/Habit/HabitOverviewItem.svelte

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
<div class="flex flex-col gap-1">
1111
<div class="flex flex-row justify-between">
1212
<a href="/{habit.id}">
13-
<span class="link-hover link text-lg decoration-secondary">{habit.name}</span></a
13+
<span class="link-hover link text-lg decoration-secondary">{habit.title}</span></a
1414
>
1515
<form method="POST" action="{habit.id}?/addToday" use:enhance>
1616
<button

src/lib/components/Habit/HabitSummaryComponent.svelte

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
import dayjs from 'dayjs';
33
import ActivityBubble from './ActivityBubble.svelte';
44
5-
let { summary }: { summary: { id: string; name: string; dates: string[] }[] } = $props();
5+
let { summary }: { summary: { id: string; title: string; dates: string[] }[] } = $props();
66
77
const thirtyDaysAgo = dayjs().subtract(30, 'days');
88
</script>
@@ -13,7 +13,7 @@
1313
{#each summary as summaryItem}
1414
<a
1515
class="link-hover link sticky left-0 mr-2 bg-base-200 pr-1 text-sm"
16-
href="/{summaryItem.id}">{summaryItem.name}</a
16+
href="/{summaryItem.id}">{summaryItem.title}</a
1717
>
1818
{#each { length: 30 } as _, i}
1919
<ActivityBubble

src/lib/server/habit.ts

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,16 +33,30 @@ export const updateDates = async (id: string, userId: string, dates: string[]) =
3333
});
3434
};
3535

36-
export const createHabit = async (name: string, userId: string, description?: string) => {
36+
export const createHabit = async (title: string, userId: string, description?: string) => {
3737
return await prisma.habit.create({
3838
data: {
39-
name: name,
39+
title: title,
4040
description: description,
4141
userId: userId,
4242
dates: []
4343
}
4444
});
4545
};
46+
export const updateHabit = async (
47+
id: string,
48+
userId: string,
49+
title: string,
50+
description?: string
51+
) => {
52+
return await prisma.habit.update({
53+
where: { userId: userId, id: id },
54+
data: {
55+
title: title,
56+
description: description
57+
}
58+
});
59+
};
4660

4761
export const deleteHabit = async (id: string, userId: string) => {
4862
await prisma.habit.delete({

src/routes/(app)/+page.server.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ export const load: PageServerLoad = async (event) => {
2525

2626
return {
2727
id: habit.id,
28-
name: habit.name,
28+
title: habit.title,
2929
dates: dates as string[]
3030
};
3131
});
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<script>
2+
import { page } from '$app/state';
3+
import NavigateBackButton from '$lib/components/NavigateBackButton.svelte';
4+
let { children } = $props();
5+
</script>
6+
7+
<div class="mb-3">
8+
<NavigateBackButton backUrl="/{page.data.habit.id}" />
9+
</div>
10+
11+
{@render children()}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
import { getHabitForUser, updateHabit } from '$lib/server/habit';
2+
import { redirect } from '@sveltejs/kit';
3+
import type { Actions, PageServerLoad } from './$types';
4+
5+
export const load: PageServerLoad = async (event) => {
6+
if (!event.locals.user) {
7+
return redirect(302, '/');
8+
}
9+
10+
const habit = await getHabitForUser(event.params.id, event.locals.user.id);
11+
12+
if (habit === null) {
13+
return redirect(302, '/');
14+
}
15+
16+
return { habit: habit };
17+
};
18+
19+
export const actions: Actions = {
20+
updateHabit: async (event) => {
21+
const formData = await event.request.formData();
22+
const title = formData.get('title');
23+
const description = formData.get('description');
24+
25+
if (!event.locals.user) {
26+
return redirect(302, '/');
27+
}
28+
29+
const habit = await updateHabit(
30+
event.params.id,
31+
event.locals.user.id,
32+
title as string,
33+
description as string
34+
);
35+
36+
return redirect(302, `/${habit.id}`);
37+
}
38+
};
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<script>
2+
import { enhance } from '$app/forms';
3+
4+
const { data } = $props();
5+
</script>
6+
7+
<div class="max-w-md">
8+
<h3 class="pb-4 text-3xl">Update Habit</h3>
9+
<form method="POST" action="?/updateHabit" use:enhance class="flex flex-col items-end gap-3">
10+
<label class="form-control w-full text-sm">
11+
<div class="label">
12+
<span class="label-text">Title</span>
13+
</div>
14+
<input name="title" class="input input-bordered" required value={data.habit.title} />
15+
</label>
16+
<label class="form-control w-full text-sm">
17+
<div class="label">
18+
<span class="label-text">Description (optional)</span>
19+
</div>
20+
<input name="description" class="input input-bordered" value={data.habit.description} />
21+
</label>
22+
<button class="btn btn-primary w-fit">Update</button>
23+
</form>
24+
</div>

src/routes/(app)/[id]/values/+page.server.ts renamed to src/routes/(app)/[id]/(habit-subpages)/values/+page.server.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import { getHabitForUser, updateDates } from '$lib/server/habit';
22
import { redirect } from '@sveltejs/kit';
3-
import type { PageServerLoad } from './$types';
4-
import type { Actions } from '../$types';
3+
import type { Actions, PageServerLoad } from './$types';
54
import { Prisma } from '@prisma/client';
65

76
export const load: PageServerLoad = async (event) => {

src/routes/(app)/[id]/values/+page.svelte renamed to src/routes/(app)/[id]/(habit-subpages)/values/+page.svelte

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,11 @@
11
<script lang="ts">
22
import { enhance } from '$app/forms';
3-
import NavigateBackButton from '$lib/components/NavigateBackButton.svelte';
43
import type { PageData } from './$types';
54
65
let { data }: { data: PageData } = $props();
76
</script>
87

9-
<div class="mb-3">
10-
<NavigateBackButton backUrl="/{data.habit.id}" />
11-
</div>
12-
13-
<h2 class="mb-3 text-3xl">{data.habit?.name}</h2>
8+
<h2 class="mb-3 text-3xl">{data.habit?.title}</h2>
149
<div class="max-w-lg overflow-x-auto">
1510
<table class="table table-xs">
1611
<thead>

src/routes/(app)/[id]/+page.svelte

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
</div>
1616

1717
<div class="mb-3">
18-
<h2 class="text-3xl">{data.habit?.name}</h2>
18+
<h2 class="text-3xl">{data.habit?.title}</h2>
1919
<p class="text-xs text-neutral-400">{data.habit?.description}</p>
2020
</div>
2121
<div class="grid gap-x-3 text-xs text-neutral-400">
@@ -39,6 +39,7 @@
3939
>
4040
</form>
4141
<a href="/{data.habit.id}/values" class="btn btn-outline btn-accent btn-xs">Show Values</a>
42+
<a href="/{data.habit.id}/edit" class="btn btn-outline btn-error btn-xs">Edit</a>
4243
<button class="btn btn-error btn-xs" onclick={() => deleteModal.showModal()}>Delete</button>
4344
</div>
4445

src/routes/(app)/create/+page.server.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,14 @@ import { createHabit } from '$lib/server/habit';
55
export const actions: Actions = {
66
createHabit: async (event) => {
77
const formData = await event.request.formData();
8-
const name = formData.get('name');
8+
const title = formData.get('title');
99
const description = formData.get('description');
1010

1111
if (!event.locals.user) {
1212
return redirect(302, '/');
1313
}
1414

15-
const habit = await createHabit(name as string, event.locals.user.id, description as string);
15+
const habit = await createHabit(title as string, event.locals.user.id, description as string);
1616

1717
return redirect(302, `/${habit.id}`);
1818
}

src/routes/(app)/create/+page.svelte

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,9 @@
77
<form method="POST" action="?/createHabit" use:enhance class="flex flex-col items-end gap-3">
88
<label class="form-control w-full text-sm">
99
<div class="label">
10-
<span class="label-text">Name</span>
10+
<span class="label-text">Title</span>
1111
</div>
12-
<input name="name" class="input input-bordered" required />
12+
<input name="title" class="input input-bordered" required />
1313
</label>
1414
<label class="form-control w-full text-sm">
1515
<div class="label">

0 commit comments

Comments
 (0)