add delete to admin panel
All checks were successful
deploy / build-and-deploy (push) Successful in 21s

This commit is contained in:
2025-05-21 20:53:53 +02:00
parent e47268111a
commit 45f984e4da
16 changed files with 207 additions and 50 deletions

View File

@@ -1,7 +1,8 @@
<script lang="ts">
import { addTeam, teams } from '@app/admin/teams/teams.ts';
import { addTeam, deleteTeam, type Team, teams } from '@app/admin/teams/teams.ts';
import DataTable from '@components/admin/table/DataTable.svelte';
import CrudPopup from '@components/admin/popup/CrudPopup.svelte';
import { confirmPopupState } from '@components/popup/ConfirmPopup.ts';
// state
let editPopupTeam = $state(null);
@@ -11,6 +12,15 @@
$effect(() => {
if (!editPopupOpen) editPopupTeam = null;
});
// callback
function onTeamDelete(team: Team) {
$confirmPopupState = {
title: 'Team löschen?',
message: 'Soll das Team wirklich gelöscht werden?',
onConfirm: () => deleteTeam(team)
};
}
</script>
{#snippet color(value: string)}
@@ -27,6 +37,7 @@
{ key: 'memberTwo.username', label: 'Spieler 2', width: 30 }
]}
onEdit={(team) => (editPopupTeam = team)}
onDelete={onTeamDelete}
/>
<CrudPopup

View File

@@ -1,13 +1,12 @@
import { type ActionReturnType, actions } from 'astro:actions';
import { writable } from 'svelte/store';
import { actionErrorPopup } from '@util/action.ts';
import { addToWritableArray, deleteFromWritableArray, updateWritableArray } from '@util/state.ts';
// types
export type Teams = Exclude<ActionReturnType<typeof actions.team.teams>['data'], undefined>['teams'];
export type Team = Teams[0];
export type Users = Exclude<ActionReturnType<typeof actions.user.users>['data'], undefined>['users'];
// state
export const teams = writable<Teams>([]);
@@ -29,10 +28,7 @@ export async function addTeam(team: Team) {
return;
}
teams.update((old) => {
old.push(Object.assign(team, { id: data.id }));
return old;
});
addToWritableArray(teams, Object.assign(team, { id: data.id }));
}
export async function editTeam(team: Team) {
@@ -42,9 +38,15 @@ export async function editTeam(team: Team) {
return;
}
teams.update((old) => {
const index = old.findIndex((a) => a.id == team.id);
old[index] = team;
return old;
});
updateWritableArray(teams, team, (t) => t.id == team.id);
}
export async function deleteTeam(team: Team) {
const { error } = await actions.team.deleteTeam(team);
if (error) {
actionErrorPopup(error);
return;
}
deleteFromWritableArray(teams, (t) => t.id == team.id);
}