1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
|
<script lang="ts">
import {delete_time_entry, get_time_entries, get_time_entry, update_time_entry} from "$shared/lib/api/time-entry";
import {IconNames, QueryKeys} from "$shared/lib/configuration";
import {TimeEntryDto} from "$shared/lib/models/TimeEntryDto";
import {Temporal} from "@js-temporal/polyfill";
import {useMutation, useQuery, useQueryClient} from "@sveltestack/svelte-query";
import {onMount} from "svelte";
import Tile from "$shared/components/tile.svelte";
import Button from "$shared/components/button.svelte";
import Stopwatch from "$shared/components/stopwatch.svelte";
import {Table, THead, TBody, TCell, TRow} from "$shared/components/table";
import Layout from "./_layout.svelte";
import EntryFrom from "$app/pages/views/entry-form/index.svelte";
import {seconds_to_hour_minute_string, unwrap_date_time_from_entry} from "$shared/lib/helpers";
import {TimeEntryQueryDuration} from "$shared/lib/models/TimeEntryQuery";
let currentTime = "";
let isLoading = false;
let EditEntryForm: any;
let timeEntries = [] as Array<TimeEntryDto>;
let timeLoggedTodayString = "0h0m";
const queryClient = useQueryClient();
const queryResult = useQuery(QueryKeys.entries, async () => await get_time_entries({
duration: TimeEntryQueryDuration.TODAY,
page: 1,
pageSize: 100,
})?.data ?? []
);
function set_current_time() {
currentTime = Temporal.Now.plainTimeISO().toLocaleString(undefined, {
timeStyle: "short",
});
}
const delete_entry_mutation = useMutation(delete_time_entry, {
onSuccess: (data) => {
queryClient.invalidateQueries([QueryKeys.entries, data.data.id]);
},
});
async function on_edit_entry_button_click(event, entryId: string) {
const response = useQuery([QueryKeys.entries, entryId], () => {
return get_time_entry(entryId);
});
EditEntryForm.set_values(response);
}
async function on_delete_entry_button_click(event, entryId: string) {
if (confirm("Are you sure you want to delete this entry?")) {
$delete_entry_mutation.mutate(entryId);
}
}
function on_create_from_stopwatch(event) {
EditEntryForm.set_time({to: event.detail.to, from: event.detail.from});
if (event.detail.description) {
EditEntryForm.set_description(event.detail.description);
}
}
onMount(async () => {
set_current_time();
setInterval(() => {
set_current_time();
}, 1e4);
queryResult.subscribe((result) => {
const newEntries = [];
let loggedSecondsToday = 0;
for (const entry of result.data?.results ?? []) {
const date_time = unwrap_date_time_from_entry(entry);
newEntries.push({
id: entry.id,
start: date_time.start_time,
stop: date_time.stop_time,
category: entry.category,
});
loggedSecondsToday += (date_time.duration.hours * 60 * 60) + (date_time.duration.minutes * 60);
}
timeLoggedTodayString = seconds_to_hour_minute_string(loggedSecondsToday);
timeEntries = newEntries;
});
});
</script>
<Layout>
<div class="grid gap-md margin-top-xs flex-row@md items-start flex-column-reverse">
<Tile class="col">
<h3 class="text-md padding-bottom-xxxs">New entry</h3>
<EntryFrom bind:functions={EditEntryForm}/>
</Tile>
<div class="col grid gap-sm">
<Tile class="col-6@md col-12">
<p class="text-xxl">{timeLoggedTodayString}</p>
<p class="text-xs margin-bottom-xxs">Logged time today</p>
<pre class="text-xxl">{currentTime}</pre>
<p class="text-xs">Current time</p>
</Tile>
<Tile class="col-6@md col-12">
<Stopwatch on:create={on_create_from_stopwatch}>
<h3 slot="header"
class="text-md">Stopwatch</h3>
</Stopwatch>
</Tile>
<Tile class="col-12">
<h3 class="text-md padding-bottom-xxxs">Today's entries</h3>
<div class="max-width-100% overflow-auto">
<Table class="width-100% text-sm">
<THead>
<TCell type="th"
class="text-left">
<span>Category</span>
</TCell>
<TCell type="th"
class="text-left">
<span>Timespan</span>
</TCell>
<TCell type="th"
class="text-right">
<Button icon="{IconNames.refresh}"
variant="reset"
icon_width="1.2rem"
icon_height="1.2rem"
title="Refresh today's entries"
on:click={() => queryClient.invalidateQueries(QueryKeys.entries)}/>
</TCell>
</THead>
<TBody>
{#if timeEntries.length > 0}
{#each timeEntries as entry}
<TRow class="text-nowrap text-left"
data-id={entry.id}>
<TCell>
<span data-id={entry.category?.id}>
{entry.category?.name}
</span>
</TCell>
<TCell>
{entry.start.toLocaleString(undefined, {timeStyle: "short"})}
<span>-</span>
{entry.stop.toLocaleString(undefined, {timeStyle: "short"})}
</TCell>
<TCell class="flex flex-row justify-end items-center">
<Button icon="{IconNames.pencilSquare}"
variant="reset"
icon_width="1.2rem"
icon_height="1.2rem"
on:click={(e) => on_edit_entry_button_click(e, entry.id)}
title="Edit entry"/>
<Button icon="{IconNames.trash}"
variant="reset"
icon_width="1.2rem"
icon_height="1.2rem"
on:click={(e) => on_delete_entry_button_click(e, entry.id)}
title="Delete entry"/>
</TCell>
</TRow>
{/each}
{:else}
<TRow class="text-nowrap">
<TCell type="th"
thScope="row"
colspan="7">
{isLoading ? "Loading..." : "No entries today"}
</TCell>
</TRow>
{/if}
</TBody>
</Table>
</div>
</Tile>
</div>
</div>
</Layout>
|