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 Deaths = Exclude['data'], undefined>['deaths']; export type Death = Deaths[0]; // state export const deaths = writable([]); // actions export async function fetchDeaths() { const { data, error } = await actions.team.deaths(); if (error) { actionErrorPopup(error); return; } deaths.set(data.deaths); } export async function addDeath(death: Death) { const { data, error } = await actions.team.addDeath({ deadUserId: death.killed.id, killerUserId: death.killer?.id, message: death.message }); if (error) { actionErrorPopup(error); return; } addToWritableArray(deaths, Object.assign(death, { id: data.id })); } export async function editDeath(death: Death) { const { error } = await actions.team.editDeath({ id: death.id, deadUserId: death.killed.id, killerUserId: death.killer?.id, message: death.message }); if (error) { actionErrorPopup(error); return; } updateWritableArray(deaths, death, (d) => d.id == death.id); } export async function deleteDeath(death: Death) { const { error } = await actions.team.deleteDeath({ id: death.id }); if (error) { actionErrorPopup(error); return; } deleteFromWritableArray(deaths, (d) => d.id == death.id); }