Skip to content

Commit ea88b75

Browse files
committed
added dayjs support to HistoryComponent
1 parent d0939b8 commit ea88b75

File tree

2 files changed

+16
-28
lines changed

2 files changed

+16
-28
lines changed

src/lib/components/HabitActivityHistory.svelte

Lines changed: 14 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,40 +1,28 @@
11
<script lang="ts">
22
import { onMount } from 'svelte';
33
import ActivityBubble from './ActivityBubble.svelte';
4+
import dayjs from 'dayjs';
5+
import isoWeek from 'dayjs/plugin/isoWeek';
6+
7+
dayjs.extend(isoWeek);
48
59
let { dates, showWeeks } = $props();
610
const activeDateSet = $derived(new Set(dates));
711
8-
const days: Date[] = $state([]);
9-
10-
const months = [
11-
'Jan',
12-
'Feb',
13-
'Mar',
14-
'Apr',
15-
'May',
16-
'Jun',
17-
'Jul',
18-
'Aug',
19-
'Sep',
20-
'Oct',
21-
'Nov',
22-
'Dec'
23-
];
12+
const days: dayjs.Dayjs[] = $state([]);
2413
25-
const today = new Date();
26-
const startDate = new Date(today);
27-
startDate.setDate(today.getDate() - today.getDay() - (7 * showWeeks - 1));
14+
const today = dayjs();
15+
const startDate = today.startOf('isoWeek').subtract(showWeeks, 'weeks');
2816
2917
onMount(() => {
30-
let current = new Date(startDate);
31-
while (current <= today) {
32-
days.push(new Date(current));
33-
current.setDate(current.getDate() + 1);
18+
let current = startDate.clone();
19+
while (current.isBefore(today.add(1, 'day'), 'day')) {
20+
days.push(current.clone());
21+
current = current.add(1, 'day');
3422
}
3523
});
3624
37-
const formatDate = (date: Date) => date.toISOString().split('T')[0];
25+
const formatDate = (date: dayjs.Dayjs) => date.format('YYYY-MM-DD');
3826
</script>
3927

4028
<div class="grid grid-flow-col grid-rows-8 items-center justify-center gap-1 text-[0.5rem]">
@@ -47,9 +35,9 @@
4735
<div></div>
4836
<div class="sticky left-0 pr-1">Sun</div>
4937
{#each days as day, i}
50-
{#if i % 7 === 0 && day.getDate() <= 7}
38+
{#if i % 7 === 0 && day.date() <= 7}
5139
<div class="-mr-2">
52-
{months[days[i + 6]?.getMonth()]}
40+
{days[i + 6]?.format('MMM')}
5341
</div>
5442
{:else if i % 7 === 0}
5543
<div></div>

src/routes/[id]/values/+page.svelte

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,11 @@
2020
</tr>
2121
</thead>
2222
<tbody>
23-
{#each data.habit?.dates as date}
23+
{#each data.habit.dates as date}
2424
<tr>
2525
<td>{date}</td>
2626
<td>
27-
<form method="POST" action="?/delete">
27+
<form method="POST" action="?/delete" use:enhance>
2828
<input type="hidden" name="date" value={date} />
2929
<button class="btn btn-outline btn-error btn-xs">Delete</button>
3030
</form>

0 commit comments

Comments
 (0)