gitscope
← marzneshin / v0.3.0-alpha / dashboard/src/features/users/services/users.query.ts
https://dev.kronoz.win/r/marzneshin.git
dashboard/src/features/users/services/users.query.ts · TypeScript
Blame Raw
 1import { useQuery } from "@tanstack/react-query";
 2import { fetch } from "@marzneshin/utils";
 3import { UserType } from "../types";
 4
 5interface UserResponse extends UserType {
 6    service_ids: number[]
 7    services: any[]
 8}
 9
10export async function fetchUsers(): Promise<UserType[]> {
11    return fetch('/users').then((result) => {
12        const users: UserType[] = result.items.map((fetchedUser: UserResponse) => {
13            const user: UserType = fetchedUser;
14            user.services = fetchedUser.service_ids
15            return user
16        })
17        return users;
18    });
19}
20
21export const UsersQueryFetchKey = "users";
22
23export const useUsersQuery = () => {
24    return useQuery({
25        queryKey: [UsersQueryFetchKey],
26        queryFn: fetchUsers,
27        initialData: []
28    })
29}