migrate useUsers hook to react-query (#674)
This commit is contained in:
parent
ace39ef73d
commit
aa554ca9f6
|
@ -1,32 +1,28 @@
|
||||||
import { useState, useEffect } from 'react'
|
import { useState, useEffect } from 'react'
|
||||||
import { PrivateUser, User } from 'common/user'
|
import { PrivateUser, User } from 'common/user'
|
||||||
import {
|
|
||||||
listenForAllUsers,
|
|
||||||
listenForPrivateUsers,
|
|
||||||
} from 'web/lib/firebase/users'
|
|
||||||
import { groupBy, sortBy, difference } from 'lodash'
|
import { groupBy, sortBy, difference } from 'lodash'
|
||||||
import { getContractsOfUserBets } from 'web/lib/firebase/bets'
|
import { getContractsOfUserBets } from 'web/lib/firebase/bets'
|
||||||
import { useFollows } from './use-follows'
|
import { useFollows } from './use-follows'
|
||||||
import { useUser } from './use-user'
|
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 = () => {
|
export const useUsers = () => {
|
||||||
const [users, setUsers] = useState<User[]>([])
|
const result = useFirestoreQueryData<DocumentData, User[]>(['users'], users, {
|
||||||
|
subscribe: true,
|
||||||
useEffect(() => {
|
includeMetadataChanges: true,
|
||||||
listenForAllUsers(setUsers)
|
})
|
||||||
}, [])
|
return result.data ?? []
|
||||||
|
|
||||||
return users
|
|
||||||
}
|
}
|
||||||
|
|
||||||
export const usePrivateUsers = () => {
|
export const usePrivateUsers = () => {
|
||||||
const [users, setUsers] = useState<PrivateUser[]>([])
|
const result = useFirestoreQueryData<DocumentData, PrivateUser[]>(
|
||||||
|
['private users'],
|
||||||
useEffect(() => {
|
privateUsers,
|
||||||
listenForPrivateUsers(setUsers)
|
{ subscribe: true, includeMetadataChanges: true }
|
||||||
}, [])
|
)
|
||||||
|
return result.data || []
|
||||||
return users
|
|
||||||
}
|
}
|
||||||
|
|
||||||
export const useDiscoverUsers = (userId: string | null | undefined) => {
|
export const useDiscoverUsers = (userId: string | null | undefined) => {
|
||||||
|
|
|
@ -258,16 +258,6 @@ export async function listAllUsers() {
|
||||||
return docs.map((doc) => doc.data())
|
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) {
|
export function getTopTraders(period: Period) {
|
||||||
const topTraders = query(
|
const topTraders = query(
|
||||||
users,
|
users,
|
||||||
|
|
Loading…
Reference in New Issue
Block a user