From 0d731c6d5af941a564a3492241122a0dd321de84 Mon Sep 17 00:00:00 2001 From: Stefano Pigozzi Date: Wed, 9 Aug 2023 04:32:25 +0200 Subject: [PATCH] Add `toggleStarred` callback to `StarredProvider` --- .../(contextStarred)/StarredContextData.ts | 1 + .../(contextStarred)/StarredProvider.tsx | 19 ++++++++++++++++++- 2 files changed, 19 insertions(+), 1 deletion(-) diff --git a/todoblue/src/app/[lang]/(layout)/(contextStarred)/StarredContextData.ts b/todoblue/src/app/[lang]/(layout)/(contextStarred)/StarredContextData.ts index b095f7c..ac4a136 100644 --- a/todoblue/src/app/[lang]/(layout)/(contextStarred)/StarredContextData.ts +++ b/todoblue/src/app/[lang]/(layout)/(contextStarred)/StarredContextData.ts @@ -6,5 +6,6 @@ export interface StarredContextData { setStarred: Dispatch> addStarred: (key: string) => void, removeStarred: (key: string) => void, + toggleStarred: (key: string) => boolean, isStarred: (key: string) => boolean, } diff --git a/todoblue/src/app/[lang]/(layout)/(contextStarred)/StarredProvider.tsx b/todoblue/src/app/[lang]/(layout)/(contextStarred)/StarredProvider.tsx index 297576a..90c9035 100644 --- a/todoblue/src/app/[lang]/(layout)/(contextStarred)/StarredProvider.tsx +++ b/todoblue/src/app/[lang]/(layout)/(contextStarred)/StarredProvider.tsx @@ -32,12 +32,29 @@ export function StarredProvider({children}: {children: ReactNode}) { }) }, []) + const toggleStarred = useCallback((value: string) => { + setStarred(prev => { + if(!prev) { + return [value] + } + const result = [...prev] + const index = result.indexOf(value) + if(!index) { + result.push(value) + } + else { + result.splice(index, 1) + } + return result + }) + }, []) + const isStarred = useCallback((value: string) => { return starred.indexOf(value) >= 0 }, [starred]) return ( - + {children} )