Skip to content

Commit

Permalink
delete button for sessions - gets sessions from backend
Browse files Browse the repository at this point in the history
  • Loading branch information
moostoet committed Jan 29, 2023
1 parent d39c20b commit 7be3fd7
Show file tree
Hide file tree
Showing 11 changed files with 162 additions and 14 deletions.
22 changes: 22 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 4 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,5 +34,8 @@
"vite": "^4.0.0",
"vitest": "^0.25.3"
},
"type": "module"
"type": "module",
"dependencies": {
"@sveltestack/svelte-query": "^1.6.0"
}
}
27 changes: 27 additions & 0 deletions src/components/DeleteButton.svelte
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<script lang="ts">
import { createEventDispatcher } from 'svelte';
import type { Session } from '../types/session';
export let session: Session;
const dispatch = createEventDispatcher();
const deleteSession = (session: Session) => async () => {
const res = await fetch('http://localhost:8000/sessions', {
method: 'DELETE',
body: JSON.stringify(session._id)
});
if (res.ok) {
dispatch('delete', {
id: session._id
});
}
};
</script>

<div class="ml-auto">
<button
on:click={deleteSession(session)}
class="bg-red-500 p-[10px] text-white font-semibold rounded-sm hover:bg-red-600 transition"
>Delete Session</button
>
</div>
14 changes: 14 additions & 0 deletions src/routes/+page.server.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import type { PageServerLoad } from './$types';
import type { Session } from '../types/session';
// import { useQuery } from '@sveltestack/svelte-query'
import { fetchData } from '../use/fetch';
export const load = (async () => {
return { sessions: await fetchData<Session[]>('http://localhost:8000/sessions')}
}) satisfies PageServerLoad;

// export const load = (() => {
// const queryResult = useQuery('sessions', () => {
// return fetch('http://localhost:8000/sessions').then(res => res.json())
// })
// return { sessions: queryResult }
// }) satisfies PageServerLoad;
63 changes: 58 additions & 5 deletions src/routes/+page.svelte
Original file line number Diff line number Diff line change
@@ -1,8 +1,61 @@
<script lang="ts">
import type { PageData } from './$types';
export let data: PageData;
import type { PageServerData } from './$types';
import { convertISODate, calculateHours } from '../use/time';
import DeleteButton from '../components/DeleteButton.svelte';
export let data: PageServerData;
$: startTimesFormatted = data.sessions.map((session) => convertISODate(session.startTime));
$: endTimesFormatted = data.sessions.map((session) => convertISODate(session.endTime));
$: totalHours = data.sessions.map((session) =>
calculateHours(session.startTime, session.endTime)
);
const handleDelete = (event: CustomEvent<{id: string}>) => {
data.sessions = data.sessions.filter((session) => session._id !== event.detail.id);
};
</script>

<div>
{data.text}
</div>
<main class="p-4">
<div class="px-8">
<button
class="p-[10px] rounded-sm bg-blue-400 text-white font-semibold hover:bg-blue-500 transition"
>New Session</button
>
</div>
<div class="p-8 flex flex-col gap-3">
{#if data.sessions.length === 0}
<div class="text-xl font-semibold">No sessions found</div>
{:else}
{#each data.sessions as session, i}
<div class="p-8 bg-white rounded-md flex flex-col gap-3 shadow-sm">
<div class="text-xl font-semibold">
{session.name}
</div>
<div class="relative overflow-x-auto">
<table class="w-full text-sm text-left">
<thead class="text-sm uppercase bg-gray-100 border-l border-r border-t">
<tr>
<th scope="col" class="px-6 py-3"> Session start </th>
<th scope="col" class="px-6 py-3"> Session end </th>
<th scope="col" class="px-6 py-3"> Hourly rate </th>
<th scope="col" class="px-6 py-3"> Total hours </th>
</tr>
</thead>
<tbody>
<tr class="bg-white border-l border-r border-b">
<th scope="row" class="px-6 py-4 font-medium text-gray-900">
{startTimesFormatted[i]}
</th>
<td class="px-6 py-4"> {endTimesFormatted[i]} </td>
<td class="px-6 py-4"> {session.hourlyRate} </td>
<td class="px-6 py-4"> {totalHours[i]} </td>
</tr>
</tbody>
</table>
</div>
<DeleteButton on:delete={handleDelete} session={session}/>
</div>
{/each}
{/if}
</div>
</main>
7 changes: 0 additions & 7 deletions src/routes/+page.ts

This file was deleted.

2 changes: 1 addition & 1 deletion src/styles/app.css
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
}

html {
@apply font-opensans;
@apply font-opensans bg-gray-100;
}
}

3 changes: 3 additions & 0 deletions src/types/greeting.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export interface Greeting {
message: string;
}
8 changes: 8 additions & 0 deletions src/types/session.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
export interface Session {
_id: string
name: string
hours: number
startTime: Date
endTime: Date
hourlyRate?: number
}
4 changes: 4 additions & 0 deletions src/use/fetch.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
export const fetchData = async <T>(url: string): Promise<T> => {
const response = await fetch(url);
return await response.json()
}
21 changes: 21 additions & 0 deletions src/use/time.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
export const convertISODate = (timestamp: Date): string => {
const date = new Date(timestamp);

const month = (date.getMonth() + 1).toString().padStart(2, '0')
const day = date.getDate().toString().padStart(2, '0')
const year = date.getFullYear()
const hours = date.getHours().toString().padStart(2, "0");
const minutes = date.getMinutes().toString().padStart(2, "0");
const AMorPM = Number(hours) >= 12 ? 'PM' : 'AM';

return `${month}/${day}/${year} ${hours}:${minutes} ${AMorPM}`
}

export const calculateHours = (startTimestamp: Date, endTimestamp: Date): string => {
const start = new Date(startTimestamp);
const end = new Date(endTimestamp);
const diff = end.getTime() - start.getTime();
const hours = Math.floor(diff / (1000 * 60 * 60));
const minutes = Math.floor((diff % (1000 * 60 * 60)) / (1000 * 60));
return `${hours} hours ${minutes} minutes`;
}

0 comments on commit 7be3fd7

Please sign in to comment.