Update admin page to show profit minus profit from first day

This commit is contained in:
James Grugett 2022-08-11 23:18:54 -05:00
parent f6bb1b9e38
commit 8f157946fa

View File

@ -10,6 +10,8 @@ import { mapKeys } from 'lodash'
import { useAdmin } from 'web/hooks/use-admin' import { useAdmin } from 'web/hooks/use-admin'
import { contractPath } from 'web/lib/firebase/contracts' import { contractPath } from 'web/lib/firebase/contracts'
import { redirectIfLoggedOut } from 'web/lib/firebase/server-auth' import { redirectIfLoggedOut } from 'web/lib/firebase/server-auth'
import { useEffect, useState } from 'react'
import { getFirstDayProfit } from 'web/lib/firebase/users'
export const getServerSideProps = redirectIfLoggedOut('/') export const getServerSideProps = redirectIfLoggedOut('/')
@ -27,14 +29,32 @@ function UsersTable() {
// Map private users by user id // Map private users by user id
const privateUsersById = mapKeys(privateUsers, 'id') const privateUsersById = mapKeys(privateUsers, 'id')
console.log('private users by id', privateUsersById)
const [profitByUser, setProfitByUser] = useState<{
[userId: string]: number
}>({})
useEffect(() => {
Promise.all(users.map((user) => getFirstDayProfit(user.id))).then(
(firstDayProfits) => {
setProfitByUser(
Object.fromEntries(
users.map((user, i) => [
user.id,
user.profitCached.allTime - firstDayProfits[i],
])
)
)
}
)
}, [users])
// For each user, set their email from the PrivateUser // For each user, set their email from the PrivateUser
const fullUsers = users const fullUsers = users
.map((user) => { .map((user) => {
return { return {
email: privateUsersById[user.id]?.email, email: privateUsersById[user.id]?.email,
profit: user.profitCached.allTime, profit: profitByUser[user.id] ?? 0,
...user, ...user,
} }
}) })