mirror of
https://github.com/ManInDark/HabitTrove.git
synced 2026-01-21 06:34:30 +01:00
support archiving habit and wishlist + wishlist redeem count (#49)
This commit is contained in:
@@ -231,11 +231,29 @@ export function useHabits() {
|
||||
}
|
||||
}
|
||||
|
||||
const archiveHabit = async (id: string) => {
|
||||
const updatedHabits = habitsData.habits.map(h =>
|
||||
h.id === id ? { ...h, archived: true } : h
|
||||
)
|
||||
await saveHabitsData({ habits: updatedHabits })
|
||||
setHabitsData({ habits: updatedHabits })
|
||||
}
|
||||
|
||||
const unarchiveHabit = async (id: string) => {
|
||||
const updatedHabits = habitsData.habits.map(h =>
|
||||
h.id === id ? { ...h, archived: undefined } : h
|
||||
)
|
||||
await saveHabitsData({ habits: updatedHabits })
|
||||
setHabitsData({ habits: updatedHabits })
|
||||
}
|
||||
|
||||
return {
|
||||
completeHabit,
|
||||
undoComplete,
|
||||
saveHabit,
|
||||
deleteHabit,
|
||||
completePastHabit
|
||||
completePastHabit,
|
||||
archiveHabit,
|
||||
unarchiveHabit
|
||||
}
|
||||
}
|
||||
|
||||
@@ -33,6 +33,16 @@ export function useWishlist() {
|
||||
|
||||
const redeemWishlistItem = async (item: WishlistItemType) => {
|
||||
if (balance >= item.coinCost) {
|
||||
// Check if item has target completions and if we've reached the limit
|
||||
if (item.targetCompletions && item.targetCompletions <= 0) {
|
||||
toast({
|
||||
title: "Redemption limit reached",
|
||||
description: `You've reached the maximum redemptions for "${item.name}".`,
|
||||
variant: "destructive",
|
||||
})
|
||||
return false
|
||||
}
|
||||
|
||||
const data = await removeCoins({
|
||||
amount: item.coinCost,
|
||||
description: `Redeemed reward: ${item.name}`,
|
||||
@@ -41,6 +51,30 @@ export function useWishlist() {
|
||||
})
|
||||
setCoins(data)
|
||||
|
||||
// Update target completions if set
|
||||
if (item.targetCompletions !== undefined) {
|
||||
const newItems = wishlist.items.map(wishlistItem => {
|
||||
if (wishlistItem.id === item.id) {
|
||||
const newTarget = wishlistItem.targetCompletions! - 1
|
||||
// If target reaches 0, archive the item
|
||||
if (newTarget <= 0) {
|
||||
return {
|
||||
...wishlistItem,
|
||||
targetCompletions: undefined,
|
||||
archived: true
|
||||
}
|
||||
}
|
||||
return {
|
||||
...wishlistItem,
|
||||
targetCompletions: newTarget
|
||||
}
|
||||
}
|
||||
return wishlistItem
|
||||
})
|
||||
setWishlist({ items: newItems })
|
||||
await saveWishlistItems(newItems)
|
||||
}
|
||||
|
||||
// Randomly choose a celebration effect
|
||||
const celebrationEffects = [
|
||||
celebrations.emojiParty
|
||||
@@ -66,11 +100,29 @@ export function useWishlist() {
|
||||
|
||||
const canRedeem = (cost: number) => balance >= cost
|
||||
|
||||
const archiveWishlistItem = async (id: string) => {
|
||||
const newItems = wishlist.items.map(item =>
|
||||
item.id === id ? { ...item, archived: true } : item
|
||||
)
|
||||
setWishlist({ items: newItems })
|
||||
await saveWishlistItems(newItems)
|
||||
}
|
||||
|
||||
const unarchiveWishlistItem = async (id: string) => {
|
||||
const newItems = wishlist.items.map(item =>
|
||||
item.id === id ? { ...item, archived: undefined } : item
|
||||
)
|
||||
setWishlist({ items: newItems })
|
||||
await saveWishlistItems(newItems)
|
||||
}
|
||||
|
||||
return {
|
||||
addWishlistItem,
|
||||
editWishlistItem,
|
||||
deleteWishlistItem,
|
||||
redeemWishlistItem,
|
||||
archiveWishlistItem,
|
||||
unarchiveWishlistItem,
|
||||
canRedeem,
|
||||
wishlistItems: wishlist.items
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user