From e0320874b36bd505a443e85de12112602c663889 Mon Sep 17 00:00:00 2001 From: "s0wlz (Matthias Puchstein)" Date: Sun, 1 Mar 2026 01:37:18 +0100 Subject: [PATCH] [refactor] implemented the countdown.store.ts --- .../countdown/state/countdown.store.ts | 89 ++++++++++++++++++- 1 file changed, 88 insertions(+), 1 deletion(-) diff --git a/src/features/countdown/state/countdown.store.ts b/src/features/countdown/state/countdown.store.ts index 7e987ec..03eb777 100644 --- a/src/features/countdown/state/countdown.store.ts +++ b/src/features/countdown/state/countdown.store.ts @@ -1 +1,88 @@ -//TODO: Implement countdown store \ No newline at end of file +//TODO: Implement countdown store + +import type {CountdownSnapshot} from "../model/countdown.types"; +import {writable} from "svelte/store"; +import {createCountdown, listCountdowns} from "../api/countdown.client"; +import type {Duration} from "../../../shared/time/duration"; + + +type CountdownStateStore = { + items: CountdownSnapshot[]; + selected: CountdownSnapshot | null; + selectedId: number | null; + loading: boolean; + error: string | null; +}; + +const initalStateStore: CountdownStateStore = { + items: [], + selected: null, + selectedId: null, + loading: false, + error: null, +}; + +const {subscribe} = writable(initalStateStore); + +function resolveSelected(items: CountdownSnapshot[], selectedId: number | null): CountdownSnapshot | null { + return selectedId === null + ? null + : items.find((x) => x.id === selectedId) ?? null; +} + +export function loadList() { + initalStateStore.loading = true; + listCountdowns().then((items) => { + initalStateStore.items = items; + initalStateStore.selectedId = initalStateStore.selectedId === null + ? (items.length > 0 ? items[0].id : null) + : initalStateStore.selectedId; + initalStateStore.selected = resolveSelected(items, initalStateStore.selectedId); + initalStateStore.loading = false; + }); +} + +export function select(id: number) { + initalStateStore.selectedId = id; + initalStateStore.selected = resolveSelected(initalStateStore.items, id); +} + +export function create(label: string, duration: Duration) { + createCountdown(label, duration).then((id) => { + loadList(); + initalStateStore.selectedId = id; + initalStateStore.selected = resolveSelected(initalStateStore.items, id); + }) +} + +export function startSelected() { + +} + +export function resumeSelected() { + +} + +export function pauseSelected() { + +} + +export function resetSelected() { + +} + +export function deleteSelected() { + +} + +export const countdownStore = { + subscribe, + loadList, + select, + create, + startSelected, + resumeSelected, + pauseSelected, + resetSelected, + deleteSelected +}; \ No newline at end of file