-
Notifications
You must be signed in to change notification settings - Fork 5
/
useUserDataQuery.js
33 lines (29 loc) · 1.3 KB
/
useUserDataQuery.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
import { useQuery } from '@tanstack/vue-query';
import { storeToRefs } from 'pinia';
import { useAuthStore } from '@/store/auth';
import { computeQueryOverrides } from '@/helpers/computeQueryOverrides';
import { fetchDocById } from '@/helpers/query/utils';
import { USER_DATA_QUERY_KEY } from '@/constants/queryKeys';
import { FIRESTORE_COLLECTIONS } from '@/constants/firebase';
import { computed } from 'vue';
/**
* User profile data query.
*
* @param {string|undefined|null} userId – The user ID to fetch, set to a falsy value to fetch the current user.
* @param {QueryOptions|undefined} queryOptions – Optional TanStack query options.
* @returns {UseQueryResult} The TanStack query result.
*/
const useUserDataQuery = (userId = undefined, queryOptions = undefined) => {
const authStore = useAuthStore();
const { roarUid, userQueryKeyIndex } = storeToRefs(authStore);
const uid = computed(() => userId || roarUid.value);
const queryConditions = [() => !!uid.value];
const { isQueryEnabled, options } = computeQueryOverrides(queryConditions, queryOptions);
return useQuery({
queryKey: [USER_DATA_QUERY_KEY, uid.value, userQueryKeyIndex.value],
queryFn: () => fetchDocById(FIRESTORE_COLLECTIONS.USERS, uid.value),
enabled: isQueryEnabled,
...options,
});
};
export default useUserDataQuery;