migrate useUsers hook to react-query (#674)

This commit is contained in:
Sinclair Chen 2022-07-20 16:31:18 -07:00 committed by GitHub
parent ace39ef73d
commit aa554ca9f6
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 14 additions and 28 deletions

View File

@ -1,32 +1,28 @@
import { useState, useEffect } from 'react'
import { PrivateUser, User } from 'common/user'
import {
listenForAllUsers,
listenForPrivateUsers,
} from 'web/lib/firebase/users'
import { groupBy, sortBy, difference } from 'lodash'
import { getContractsOfUserBets } from 'web/lib/firebase/bets'
import { useFollows } from './use-follows'
import { useUser } from './use-user'
import { useFirestoreQueryData } from '@react-query-firebase/firestore'
import { DocumentData } from 'firebase/firestore'
import { users, privateUsers } from 'web/lib/firebase/users'
export const useUsers = () => {
const [users, setUsers] = useState<User[]>([])
useEffect(() => {
listenForAllUsers(setUsers)
}, [])
return users
const result = useFirestoreQueryData<DocumentData, User[]>(['users'], users, {
subscribe: true,
includeMetadataChanges: true,
})
return result.data ?? []
}
export const usePrivateUsers = () => {
const [users, setUsers] = useState<PrivateUser[]>([])
useEffect(() => {
listenForPrivateUsers(setUsers)
}, [])
return users
const result = useFirestoreQueryData<DocumentData, PrivateUser[]>(
['private users'],
privateUsers,
{ subscribe: true, includeMetadataChanges: true }
)
return result.data || []
}
export const useDiscoverUsers = (userId: string | null | undefined) => {

View File

@ -258,16 +258,6 @@ export async function listAllUsers() {
return docs.map((doc) => doc.data())
}
export function listenForAllUsers(setUsers: (users: User[]) => void) {
listenForValues(users, setUsers)
}
export function listenForPrivateUsers(
setUsers: (users: PrivateUser[]) => void
) {
listenForValues(privateUsers, setUsers)
}
export function getTopTraders(period: Period) {
const topTraders = query(
users,