2022-08-18 07:35:32 +00:00
|
|
|
import type { User } from "@halo-dev/api-client";
|
2024-06-25 04:31:44 +00:00
|
|
|
import { coreApiClient } from "@halo-dev/api-client";
|
2024-06-26 10:42:50 +00:00
|
|
|
import type { Ref } from "vue";
|
|
|
|
import { onMounted, ref } from "vue";
|
2022-08-18 07:35:32 +00:00
|
|
|
|
|
|
|
interface useUserFetchReturn {
|
|
|
|
users: Ref<User[]>;
|
|
|
|
loading: Ref<boolean>;
|
|
|
|
handleFetchUsers: () => void;
|
|
|
|
}
|
|
|
|
|
2022-09-06 11:48:12 +00:00
|
|
|
export function useUserFetch(options?: {
|
|
|
|
fetchOnMounted: boolean;
|
|
|
|
}): useUserFetchReturn {
|
|
|
|
const { fetchOnMounted } = options || {};
|
|
|
|
|
2022-08-18 07:35:32 +00:00
|
|
|
const users = ref<User[]>([] as User[]);
|
|
|
|
const loading = ref(false);
|
|
|
|
|
2023-02-28 14:54:18 +00:00
|
|
|
const ANONYMOUSUSER_NAME = "anonymousUser";
|
|
|
|
|
2022-08-18 07:35:32 +00:00
|
|
|
const handleFetchUsers = async () => {
|
|
|
|
try {
|
|
|
|
loading.value = true;
|
2024-06-25 04:31:44 +00:00
|
|
|
const { data } = await coreApiClient.user.listUser({
|
2023-02-28 14:54:18 +00:00
|
|
|
fieldSelector: [`name!=${ANONYMOUSUSER_NAME}`],
|
|
|
|
});
|
2022-08-18 07:35:32 +00:00
|
|
|
users.value = data.items;
|
|
|
|
} catch (e) {
|
|
|
|
console.error("Failed to fetch users", e);
|
|
|
|
} finally {
|
|
|
|
loading.value = false;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2022-09-06 11:48:12 +00:00
|
|
|
onMounted(() => {
|
|
|
|
fetchOnMounted && handleFetchUsers();
|
|
|
|
});
|
2022-08-18 07:35:32 +00:00
|
|
|
|
|
|
|
return {
|
|
|
|
users,
|
|
|
|
loading,
|
|
|
|
handleFetchUsers,
|
|
|
|
};
|
|
|
|
}
|