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:
21
lib/atoms.ts
21
lib/atoms.ts
@@ -10,6 +10,7 @@ import {
|
||||
CompletionCache,
|
||||
getDefaultServerSettings,
|
||||
User,
|
||||
UserId,
|
||||
} from "./types";
|
||||
import {
|
||||
getTodayInTimezone,
|
||||
@@ -85,10 +86,26 @@ export const transactionsTodayAtom = atom((get) => {
|
||||
return calculateTransactionsToday(coins.transactions, settings.system.timezone);
|
||||
});
|
||||
|
||||
// Derived atom for current balance from all transactions
|
||||
// Atom to store the current logged-in user's ID.
|
||||
// This should be set by your application when the user session is available.
|
||||
export const currentUserIdAtom = atom<UserId | undefined>(undefined);
|
||||
|
||||
export const currentUserAtom = atom((get) => {
|
||||
const currentUserId = get(currentUserIdAtom);
|
||||
const users = get(usersAtom);
|
||||
return users.users.find(user => user.id === currentUserId);
|
||||
})
|
||||
|
||||
// Derived atom for current balance for the logged-in user
|
||||
export const coinsBalanceAtom = atom((get) => {
|
||||
const loggedInUserId = get(currentUserIdAtom);
|
||||
if (!loggedInUserId) {
|
||||
return 0; // No user logged in or ID not set, so balance is 0
|
||||
}
|
||||
const coins = get(coinsAtom);
|
||||
return coins.transactions.reduce((sum, transaction) => sum + transaction.amount, 0);
|
||||
return coins.transactions
|
||||
.filter(transaction => transaction.userId === loggedInUserId)
|
||||
.reduce((sum, transaction) => sum + transaction.amount, 0);
|
||||
});
|
||||
|
||||
/* transient atoms */
|
||||
|
||||
@@ -1,38 +0,0 @@
|
||||
// client helpers
|
||||
'use-client'
|
||||
|
||||
import { useSession } from "next-auth/react"
|
||||
import { User, UserId } from './types'
|
||||
import { useAtom } from 'jotai'
|
||||
import { usersAtom } from './atoms'
|
||||
import { checkPermission } from './utils'
|
||||
|
||||
export function useHelpers() {
|
||||
const { data: session, status } = useSession()
|
||||
const currentUserId = session?.user.id
|
||||
const [usersData] = useAtom(usersAtom)
|
||||
const currentUser = usersData.users.find((u) => u.id === currentUserId)
|
||||
// detect iOS: https://stackoverflow.com/a/9039885
|
||||
function iOS() {
|
||||
return [
|
||||
'iPad Simulator',
|
||||
'iPhone Simulator',
|
||||
'iPod Simulator',
|
||||
'iPad',
|
||||
'iPhone',
|
||||
'iPod',
|
||||
].includes(navigator.platform)
|
||||
// iPad on iOS 13 detection
|
||||
|| (navigator.userAgent.includes("Mac") && "ontouchend" in document)
|
||||
}
|
||||
|
||||
return {
|
||||
currentUserId,
|
||||
currentUser,
|
||||
usersData,
|
||||
status,
|
||||
hasPermission: (resource: 'habit' | 'wishlist' | 'coins', action: 'write' | 'interact') => currentUser?.isAdmin ||
|
||||
checkPermission(currentUser?.permissions, resource, action),
|
||||
isIOS: iOS(),
|
||||
}
|
||||
}
|
||||
19
lib/utils.ts
19
lib/utils.ts
@@ -2,7 +2,7 @@ import { clsx, type ClassValue } from "clsx"
|
||||
import { twMerge } from "tailwind-merge"
|
||||
import { DateTime, DateTimeFormatOptions } from "luxon"
|
||||
import { datetime, RRule } from 'rrule'
|
||||
import { Freq, Habit, CoinTransaction, Permission, ParsedFrequencyResult, ParsedResultType } from '@/lib/types'
|
||||
import { Freq, Habit, CoinTransaction, Permission, ParsedFrequencyResult, ParsedResultType, User } from '@/lib/types'
|
||||
import { DUE_MAP, INITIAL_DUE, RECURRENCE_RULE_MAP } from "./constants"
|
||||
import * as chrono from 'chrono-node'
|
||||
import _ from "lodash"
|
||||
@@ -464,3 +464,20 @@ export function checkPermission(
|
||||
export function uuid() {
|
||||
return uuidv4()
|
||||
}
|
||||
|
||||
export function hasPermission(
|
||||
currentUser: User | undefined,
|
||||
resource: 'habit' | 'wishlist' | 'coins',
|
||||
action: 'write' | 'interact'
|
||||
): boolean {
|
||||
// If no current user, no permissions.
|
||||
if (!currentUser) {
|
||||
return false;
|
||||
}
|
||||
// If user is admin, they have all permissions.
|
||||
if (currentUser.isAdmin) {
|
||||
return true;
|
||||
}
|
||||
// Otherwise, check specific permissions.
|
||||
return checkPermission(currentUser.permissions, resource, action);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user