From 9a6c8c5f9155a114704dd857afeecc6bc55c42ce Mon Sep 17 00:00:00 2001 From: Triston Armstrong Date: Thu, 21 Mar 2024 23:34:49 -0500 Subject: [PATCH] fix: fix issue with delete button not working The delete button wasn't fully implimented and the deletion of the items logic wasnt updating the storage file bug #7 --- src/hooks/stationStores.ts | 14 +++++++++----- src/pages/Main.tsx | 26 +++++++++++++++++++++----- 2 files changed, 30 insertions(+), 10 deletions(-) diff --git a/src/hooks/stationStores.ts b/src/hooks/stationStores.ts index ee72682..a2fe568 100644 --- a/src/hooks/stationStores.ts +++ b/src/hooks/stationStores.ts @@ -1,4 +1,3 @@ -import { writeTextFile } from "@tauri-apps/api/fs" import { createStore } from "kaioken" export const useStationsStore = createStore( @@ -13,10 +12,15 @@ export const useStationsStore = createStore( //@ts-ignore return newState }, - delete: (stationId) => - set( - (state) => state?.filter((station) => station.id !== stationId) ?? [] - ), + rmStation: (stationId): Station[] => { + let newState: Station[] | null = null + set((state) => { + newState = state?.filter((station) => station.id !== stationId) ?? [] + return newState + }) + //@ts-ignore + return newState + }, override: (stationsList: Station[]) => { set((_state) => stationsList) }, diff --git a/src/pages/Main.tsx b/src/pages/Main.tsx index 611014d..204cb66 100644 --- a/src/pages/Main.tsx +++ b/src/pages/Main.tsx @@ -1,14 +1,30 @@ import { navigate } from "kaioken" import { Station, useSelectStationStore } from "../hooks/stationStores" import { useStationsStore } from "../hooks/stationStores" +import { writeFile } from "@tauri-apps/api/fs" +import { useStorageStore } from "../hooks/storageStores" export default function Main() { const { make } = useSelectStationStore() - const stations = useStationsStore.getState() + const { value, rmStation } = useStationsStore() + const { value: storageFile } = useStorageStore() + const stations = value function _handleStationClick(station: Station) { - make(station) - navigate('/player') + return function() { + make(station) + navigate('/player') + } + } + + function _handleDeleteClick(s: Station) { + return function(ev: MouseEvent) { + ev.preventDefault() + ev.stopPropagation() + const newStations = rmStation(s.id) + if (!storageFile) return + writeFile(storageFile, JSON.stringify(newStations)) + } } if (!stations?.length) { @@ -33,11 +49,11 @@ export default function Main() {
{stations.map((s) => ( - +
))} -- 2.43.0