mirror of
https://github.com/ManInDark/HabitTrove.git
synced 2026-01-21 06:34:30 +01:00
fix coin stats and add transaction note (#31)
This commit is contained in:
@@ -1,26 +1,25 @@
|
||||
import { useAtom } from 'jotai'
|
||||
import {
|
||||
coinsAtom,
|
||||
settingsAtom,
|
||||
import {
|
||||
coinsAtom,
|
||||
coinsEarnedTodayAtom,
|
||||
totalEarnedAtom,
|
||||
totalSpentAtom,
|
||||
coinsSpentTodayAtom,
|
||||
transactionsTodayAtom
|
||||
} from '@/lib/atoms'
|
||||
import { addCoins, removeCoins } from '@/app/actions/data'
|
||||
import { addCoins, removeCoins, saveCoinsData } from '@/app/actions/data'
|
||||
import { CoinsData } from '@/lib/types'
|
||||
import { toast } from '@/hooks/use-toast'
|
||||
|
||||
export function useCoins() {
|
||||
const [coins, setCoins] = useAtom(coinsAtom)
|
||||
const [settings] = useAtom(settingsAtom)
|
||||
const [coinsEarnedToday] = useAtom(coinsEarnedTodayAtom)
|
||||
const [totalEarned] = useAtom(totalEarnedAtom)
|
||||
const [totalSpent] = useAtom(totalSpentAtom)
|
||||
const [coinsSpentToday] = useAtom(coinsSpentTodayAtom)
|
||||
const [transactionsToday] = useAtom(transactionsTodayAtom)
|
||||
|
||||
const add = async (amount: number, description: string) => {
|
||||
const add = async (amount: number, description: string, note?: string) => {
|
||||
if (isNaN(amount) || amount <= 0) {
|
||||
toast({
|
||||
title: "Invalid amount",
|
||||
@@ -29,13 +28,13 @@ export function useCoins() {
|
||||
return null
|
||||
}
|
||||
|
||||
const data = await addCoins(amount, description)
|
||||
const data = await addCoins(amount, description, 'MANUAL_ADJUSTMENT', undefined, note)
|
||||
setCoins(data)
|
||||
toast({ title: "Success", description: `Added ${amount} coins` })
|
||||
return data
|
||||
}
|
||||
|
||||
const remove = async (amount: number, description: string) => {
|
||||
const remove = async (amount: number, description: string, note?: string) => {
|
||||
const numAmount = Math.abs(amount)
|
||||
if (isNaN(numAmount) || numAmount <= 0) {
|
||||
toast({
|
||||
@@ -45,15 +44,45 @@ export function useCoins() {
|
||||
return null
|
||||
}
|
||||
|
||||
const data = await removeCoins(numAmount, description)
|
||||
const data = await removeCoins(numAmount, description, 'MANUAL_ADJUSTMENT', undefined, note)
|
||||
setCoins(data)
|
||||
toast({ title: "Success", description: `Removed ${numAmount} coins` })
|
||||
return data
|
||||
}
|
||||
|
||||
const updateNote = async (transactionId: string, note: string) => {
|
||||
const transaction = coins.transactions.find(t => t.id === transactionId)
|
||||
if (!transaction) {
|
||||
toast({
|
||||
title: "Error",
|
||||
description: "Transaction not found"
|
||||
})
|
||||
return null
|
||||
}
|
||||
|
||||
const updatedTransaction = {
|
||||
...transaction,
|
||||
note: note.trim() || undefined
|
||||
}
|
||||
|
||||
const updatedTransactions = coins.transactions.map(t =>
|
||||
t.id === transactionId ? updatedTransaction : t
|
||||
)
|
||||
|
||||
const newData: CoinsData = {
|
||||
...coins,
|
||||
transactions: updatedTransactions
|
||||
}
|
||||
|
||||
await saveCoinsData(newData)
|
||||
setCoins(newData)
|
||||
return newData
|
||||
}
|
||||
|
||||
return {
|
||||
add,
|
||||
remove,
|
||||
updateNote,
|
||||
balance: coins.balance,
|
||||
transactions: coins.transactions,
|
||||
coinsEarnedToday,
|
||||
|
||||
Reference in New Issue
Block a user