mirror of
https://github.com/ManInDark/HabitTrove.git
synced 2026-01-21 06:34:30 +01:00
fix coin balance
This commit is contained in:
@@ -1,5 +1,6 @@
|
||||
import { useAtom } from 'jotai'
|
||||
import { useTranslations } from 'next-intl'
|
||||
import { useAtom } from 'jotai';
|
||||
import { useState, useEffect } from 'react';
|
||||
import { useTranslations } from 'next-intl';
|
||||
import { calculateCoinsEarnedToday, calculateCoinsSpentToday, calculateTotalEarned, calculateTotalSpent, calculateTransactionsToday, checkPermission } from '@/lib/utils'
|
||||
import {
|
||||
coinsAtom,
|
||||
@@ -11,11 +12,11 @@ import {
|
||||
coinsBalanceAtom,
|
||||
settingsAtom,
|
||||
usersAtom,
|
||||
currentUserAtom,
|
||||
} from '@/lib/atoms'
|
||||
import { addCoins, removeCoins, saveCoinsData } from '@/app/actions/data'
|
||||
import { CoinsData, User } from '@/lib/types'
|
||||
import { toast } from '@/hooks/use-toast'
|
||||
import { useHelpers } from '@/lib/client-helpers'
|
||||
import { MAX_COIN_LIMIT } from '@/lib/constants'
|
||||
|
||||
function handlePermissionCheck(
|
||||
@@ -51,23 +52,58 @@ export function useCoins(options?: { selectedUser?: string }) {
|
||||
const [coins, setCoins] = useAtom(coinsAtom)
|
||||
const [settings] = useAtom(settingsAtom)
|
||||
const [users] = useAtom(usersAtom)
|
||||
const { currentUser } = useHelpers()
|
||||
let user: User | undefined;
|
||||
if (!options?.selectedUser) {
|
||||
user = currentUser;
|
||||
} else {
|
||||
user = users.users.find(u => u.id === options.selectedUser)
|
||||
}
|
||||
const [currentUser] = useAtom(currentUserAtom)
|
||||
const [allCoinsData] = useAtom(coinsAtom) // All coin transactions
|
||||
const [loggedInUserBalance] = useAtom(coinsBalanceAtom) // Balance of the *currently logged-in* user
|
||||
const [atomCoinsEarnedToday] = useAtom(coinsEarnedTodayAtom);
|
||||
const [atomTotalEarned] = useAtom(totalEarnedAtom)
|
||||
const [atomTotalSpent] = useAtom(totalSpentAtom)
|
||||
const [atomCoinsSpentToday] = useAtom(coinsSpentTodayAtom);
|
||||
const [atomTransactionsToday] = useAtom(transactionsTodayAtom);
|
||||
const targetUser = options?.selectedUser ? users.users.find(u => u.id === options.selectedUser) : currentUser
|
||||
// Filter transactions for the targetUser
|
||||
const transactions = allCoinsData.transactions.filter(t => t.userId === targetUser?.id)
|
||||
const timezone = settings.system.timezone;
|
||||
const [coinsEarnedToday, setCoinsEarnedToday] = useState(0);
|
||||
const [totalEarned, setTotalEarned] = useState(0);
|
||||
const [totalSpent, setTotalSpent] = useState(0);
|
||||
const [coinsSpentToday, setCoinsSpentToday] = useState(0);
|
||||
const [transactionsToday, setTransactionsToday] = useState<number>(0);
|
||||
const [balance, setBalance] = useState(0);
|
||||
|
||||
// Filter transactions for the selectd user
|
||||
const transactions = coins.transactions.filter(t => t.userId === user?.id)
|
||||
|
||||
const [balance] = useAtom(coinsBalanceAtom)
|
||||
const [coinsEarnedToday] = useAtom(coinsEarnedTodayAtom)
|
||||
const [totalEarned] = useAtom(totalEarnedAtom)
|
||||
const [totalSpent] = useAtom(totalSpentAtom)
|
||||
const [coinsSpentToday] = useAtom(coinsSpentTodayAtom)
|
||||
const [transactionsToday] = useAtom(transactionsTodayAtom)
|
||||
useEffect(() => {
|
||||
// Calculate other metrics
|
||||
if (targetUser?.id && targetUser.id === currentUser?.id) {
|
||||
// If the target user is the currently logged-in user, use the derived atom's value
|
||||
setCoinsEarnedToday(atomCoinsEarnedToday);
|
||||
setTotalEarned(atomTotalEarned);
|
||||
setTotalSpent(atomTotalSpent);
|
||||
setCoinsSpentToday(atomCoinsSpentToday);
|
||||
setTransactionsToday(atomTransactionsToday);
|
||||
setBalance(loggedInUserBalance);
|
||||
} else if (targetUser?.id) {
|
||||
// If an admin is viewing another user, calculate their metrics manually
|
||||
setCoinsEarnedToday(calculateCoinsEarnedToday(transactions, timezone));
|
||||
setTotalEarned(calculateTotalEarned(transactions));
|
||||
setTotalSpent(calculateTotalSpent(transactions));
|
||||
setCoinsSpentToday(calculateCoinsSpentToday(transactions, timezone));
|
||||
setTransactionsToday(calculateTransactionsToday(transactions, timezone));
|
||||
const userTransactions = allCoinsData.transactions.filter(t => t.userId === targetUser!.id);
|
||||
setBalance(userTransactions.reduce((acc, t) => acc + t.amount, 0));
|
||||
}
|
||||
}, [
|
||||
targetUser,
|
||||
currentUser,
|
||||
transactions, // Derived from allCoinsData and targetUser
|
||||
allCoinsData, // Added for balance calculation when targetUser is not currentUser
|
||||
timezone,
|
||||
loggedInUserBalance, // Added for balance calculation when targetUser is currentUser
|
||||
atomCoinsEarnedToday,
|
||||
atomTotalEarned,
|
||||
atomTotalSpent,
|
||||
atomCoinsSpentToday,
|
||||
atomTransactionsToday,
|
||||
]);
|
||||
|
||||
const add = async (amount: number, description: string, note?: string) => {
|
||||
if (!handlePermissionCheck(currentUser, 'coins', 'write', tCommon)) return null
|
||||
@@ -91,7 +127,7 @@ export function useCoins(options?: { selectedUser?: string }) {
|
||||
description,
|
||||
type: 'MANUAL_ADJUSTMENT',
|
||||
note,
|
||||
userId: user?.id
|
||||
userId: targetUser?.id
|
||||
})
|
||||
setCoins(data)
|
||||
toast({ title: t("successTitle"), description: t("addedCoinsDescription", { amount }) })
|
||||
@@ -121,7 +157,7 @@ export function useCoins(options?: { selectedUser?: string }) {
|
||||
description,
|
||||
type: 'MANUAL_ADJUSTMENT',
|
||||
note,
|
||||
userId: user?.id
|
||||
userId: targetUser?.id
|
||||
})
|
||||
setCoins(data)
|
||||
toast({ title: t("successTitle"), description: t("removedCoinsDescription", { amount: numAmount }) })
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
import { useAtom, atom } from 'jotai'
|
||||
import { useTranslations } from 'next-intl'
|
||||
import { habitsAtom, coinsAtom, settingsAtom, usersAtom, habitFreqMapAtom } from '@/lib/atoms'
|
||||
import { habitsAtom, coinsAtom, settingsAtom, usersAtom, habitFreqMapAtom, currentUserAtom } from '@/lib/atoms'
|
||||
import { addCoins, removeCoins, saveHabitsData } from '@/app/actions/data'
|
||||
import { Habit, Permission, SafeUser, User } from '@/lib/types'
|
||||
import { toast } from '@/hooks/use-toast'
|
||||
@@ -20,10 +20,10 @@ import {
|
||||
} from '@/lib/utils'
|
||||
import { ToastAction } from '@/components/ui/toast'
|
||||
import { Undo2 } from 'lucide-react'
|
||||
import { useHelpers } from '@/lib/client-helpers'
|
||||
|
||||
|
||||
function handlePermissionCheck(
|
||||
user: SafeUser | undefined,
|
||||
user: SafeUser | User | undefined,
|
||||
resource: 'habit' | 'wishlist' | 'coins',
|
||||
action: 'write' | 'interact',
|
||||
tCommon: (key: string, values?: Record<string, any>) => string
|
||||
@@ -54,7 +54,7 @@ export function useHabits() {
|
||||
const t = useTranslations('useHabits');
|
||||
const tCommon = useTranslations('Common');
|
||||
const [usersData] = useAtom(usersAtom)
|
||||
const { currentUser } = useHelpers()
|
||||
const [currentUser] = useAtom(currentUserAtom)
|
||||
const [habitsData, setHabitsData] = useAtom(habitsAtom)
|
||||
const [coins, setCoins] = useAtom(coinsAtom)
|
||||
const [settings] = useAtom(settingsAtom)
|
||||
|
||||
@@ -1,16 +1,15 @@
|
||||
import { useAtom } from 'jotai'
|
||||
import { useTranslations } from 'next-intl'
|
||||
import { wishlistAtom, coinsAtom } from '@/lib/atoms'
|
||||
import { wishlistAtom, coinsAtom, currentUserAtom } from '@/lib/atoms'
|
||||
import { saveWishlistItems, removeCoins } from '@/app/actions/data'
|
||||
import { toast } from '@/hooks/use-toast'
|
||||
import { WishlistItemType } from '@/lib/types'
|
||||
import { WishlistItemType, User, SafeUser } from '@/lib/types'
|
||||
import { celebrations } from '@/utils/celebrations'
|
||||
import { checkPermission } from '@/lib/utils'
|
||||
import { useHelpers } from '@/lib/client-helpers'
|
||||
import { useCoins } from './useCoins'
|
||||
|
||||
function handlePermissionCheck(
|
||||
user: any, // Consider using a more specific type like SafeUser | User | undefined
|
||||
user: User | SafeUser | undefined,
|
||||
resource: 'habit' | 'wishlist' | 'coins',
|
||||
action: 'write' | 'interact',
|
||||
tCommon: (key: string, values?: Record<string, any>) => string
|
||||
@@ -39,7 +38,7 @@ function handlePermissionCheck(
|
||||
export function useWishlist() {
|
||||
const t = useTranslations('useWishlist');
|
||||
const tCommon = useTranslations('Common');
|
||||
const { currentUser: user } = useHelpers()
|
||||
const [user] = useAtom(currentUserAtom)
|
||||
const [wishlist, setWishlist] = useAtom(wishlistAtom)
|
||||
const [coins, setCoins] = useAtom(coinsAtom)
|
||||
const { balance } = useCoins()
|
||||
|
||||
Reference in New Issue
Block a user