1
1
import { useAtom } from 'jotai'
2
- import { checkPermission } from '@/lib/utils'
2
+ import { calculateCoinsEarnedToday , calculateCoinsSpentToday , calculateTotalEarned , calculateTotalSpent , calculateTransactionsToday , checkPermission } from '@/lib/utils'
3
3
import {
4
4
coinsAtom ,
5
- coinsEarnedTodayAtom ,
6
- totalEarnedAtom ,
7
- totalSpentAtom ,
8
- coinsSpentTodayAtom ,
9
- transactionsTodayAtom ,
10
- coinsBalanceAtom
5
+ // coinsEarnedTodayAtom,
6
+ // totalEarnedAtom,
7
+ // totalSpentAtom,
8
+ // coinsSpentTodayAtom,
9
+ // transactionsTodayAtom,
10
+ // coinsBalanceAtom,
11
+ settingsAtom ,
12
+ usersAtom
11
13
} from '@/lib/atoms'
12
14
import { addCoins , removeCoins , saveCoinsData } from '@/app/actions/data'
13
- import { CoinsData } from '@/lib/types'
15
+ import { CoinsData , User } from '@/lib/types'
14
16
import { toast } from '@/hooks/use-toast'
15
17
import { useHelpers } from '@/lib/client-helpers'
16
18
17
19
function handlePermissionCheck (
18
- user : any ,
20
+ user : User | undefined ,
19
21
resource : 'habit' | 'wishlist' | 'coins' ,
20
22
action : 'write' | 'interact'
21
23
) : boolean {
@@ -40,18 +42,30 @@ function handlePermissionCheck(
40
42
return true
41
43
}
42
44
43
- export function useCoins ( ) {
44
- const { currentUser : user } = useHelpers ( )
45
+ export function useCoins ( options ?: { selectedUser ?: string } ) {
45
46
const [ coins , setCoins ] = useAtom ( coinsAtom )
46
- const [ coinsEarnedToday ] = useAtom ( coinsEarnedTodayAtom )
47
- const [ totalEarned ] = useAtom ( totalEarnedAtom )
48
- const [ totalSpent ] = useAtom ( totalSpentAtom )
49
- const [ coinsSpentToday ] = useAtom ( coinsSpentTodayAtom )
50
- const [ transactionsToday ] = useAtom ( transactionsTodayAtom )
51
- const [ balance ] = useAtom ( coinsBalanceAtom )
47
+ const [ settings ] = useAtom ( settingsAtom )
48
+ const [ users ] = useAtom ( usersAtom )
49
+ const { currentUser } = useHelpers ( )
50
+ let user : User | undefined ;
51
+ if ( ! options ?. selectedUser ) {
52
+ user = currentUser ;
53
+ } else {
54
+ user = users . users . find ( u => u . id === options . selectedUser )
55
+ }
56
+
57
+ // Filter transactions for the selectd user
58
+ const transactions = coins . transactions . filter ( t => t . userId === user ?. id )
59
+
60
+ const balance = transactions . reduce ( ( sum , t ) => sum + t . amount , 0 )
61
+ const coinsEarnedToday = calculateCoinsEarnedToday ( transactions , settings . system . timezone )
62
+ const totalEarned = calculateTotalEarned ( transactions )
63
+ const totalSpent = calculateTotalSpent ( transactions )
64
+ const coinsSpentToday = calculateCoinsSpentToday ( transactions , settings . system . timezone )
65
+ const transactionsToday = calculateTransactionsToday ( transactions , settings . system . timezone )
52
66
53
67
const add = async ( amount : number , description : string , note ?: string ) => {
54
- if ( ! handlePermissionCheck ( user , 'coins' , 'write' ) ) return null
68
+ if ( ! handlePermissionCheck ( currentUser , 'coins' , 'write' ) ) return null
55
69
if ( isNaN ( amount ) || amount <= 0 ) {
56
70
toast ( {
57
71
title : "Invalid amount" ,
@@ -64,15 +78,16 @@ export function useCoins() {
64
78
amount,
65
79
description,
66
80
type : 'MANUAL_ADJUSTMENT' ,
67
- note
81
+ note,
82
+ userId : user ?. id
68
83
} )
69
84
setCoins ( data )
70
85
toast ( { title : "Success" , description : `Added ${ amount } coins` } )
71
86
return data
72
87
}
73
88
74
89
const remove = async ( amount : number , description : string , note ?: string ) => {
75
- if ( ! handlePermissionCheck ( user , 'coins' , 'write' ) ) return null
90
+ if ( ! handlePermissionCheck ( currentUser , 'coins' , 'write' ) ) return null
76
91
const numAmount = Math . abs ( amount )
77
92
if ( isNaN ( numAmount ) || numAmount <= 0 ) {
78
93
toast ( {
@@ -86,15 +101,16 @@ export function useCoins() {
86
101
amount : numAmount ,
87
102
description,
88
103
type : 'MANUAL_ADJUSTMENT' ,
89
- note
104
+ note,
105
+ userId : user ?. id
90
106
} )
91
107
setCoins ( data )
92
108
toast ( { title : "Success" , description : `Removed ${ numAmount } coins` } )
93
109
return data
94
110
}
95
111
96
112
const updateNote = async ( transactionId : string , note : string ) => {
97
- if ( ! handlePermissionCheck ( user , 'coins' , 'write' ) ) return null
113
+ if ( ! handlePermissionCheck ( currentUser , 'coins' , 'write' ) ) return null
98
114
const transaction = coins . transactions . find ( t => t . id === transactionId )
99
115
if ( ! transaction ) {
100
116
toast ( {
@@ -128,7 +144,7 @@ export function useCoins() {
128
144
remove,
129
145
updateNote,
130
146
balance,
131
- transactions : coins . transactions ,
147
+ transactions : transactions ,
132
148
coinsEarnedToday,
133
149
totalEarned,
134
150
totalSpent,
0 commit comments