#16 deletiion capabillity

This commit is contained in:
2024-07-30 22:04:40 +02:00
parent db021cec0f
commit c7b6cb0c99
5 changed files with 58 additions and 7 deletions

View File

@@ -45,7 +45,7 @@
<a href="/" class="flex-1">ME-FIT</a>
{#if user}
<p>{user?.email}</p>
<button class="btn" on:click={async () => signOut()}>Logout</button>
<button class="btn" on:click={async () => signOut()}>Sign Out</button>
{:else}
<a href="/signup" class="btn">Sign Up</a>
<a href="/signin" class="btn">Sign In</a>

View File

@@ -4,6 +4,7 @@
import type { Auth } from 'firebase/auth';
import { goto } from '$app/navigation';
import { addToast } from '$lib/toast';
import MdiDelete from '~icons/mdi/delete';
var auth: Auth | null = null;
@@ -53,9 +54,30 @@
});
if (response.ok) {
const data = await response.json();
console.log(data);
workouts = data;
workouts = await response.json();
} else {
addToast('Failed to fetch workouts: ' + (await response.text()), 'error');
}
} catch (error: any) {
addToast('Failed to fetch workouts: ' + error.message, 'error');
}
}
async function deleteWorkout(id: string) {
console.log('Deleting workout with id: ', id);
try {
const data = new FormData();
data.append('id', id);
const response = await fetch(PUBLIC_BASE_API_URL + '/workout', {
headers: {
Authorization: 'Bearer ' + (await auth?.currentUser?.getIdToken())
},
body: data,
method: 'DELETE'
});
if (response.ok) {
workouts = workouts.filter((workout) => workout.id !== id);
} else {
addToast('Failed to fetch workouts: ' + (await response.text()), 'error');
}
@@ -107,6 +129,7 @@
<th>Type</th>
<th>Sets</th>
<th>Reps</th>
<th></th>
</tr>
</thead>
@@ -117,6 +140,13 @@
<th>{workout.type}</th>
<th>{workout.sets}</th>
<th>{workout.reps}</th>
<th>
<div class="tooltip" data-tip="Delete Entry">
<button on:click={deleteWorkout(workout.id)}>
<MdiDelete class="text-gray-400 text-lg"></MdiDelete>
</button>
</div>
</th>
</tr>
{/each}
</tbody>

View File

@@ -14,7 +14,6 @@
data.get('password') as string
);
addToast('Signed in successfully', 'success');
goto('/');
} catch (error: any) {
const errorStr = error.code ? error.code : error.message;