mirror of
https://github.com/solidtime-io/solidtime.git
synced 2026-08-08 08:12:17 +01:00
upgrade inertia v2; add prefetching; migrate queries to tanstack query
vue
This commit is contained in:
39
resources/js/utils/useProjectMembersQuery.ts
Normal file
39
resources/js/utils/useProjectMembersQuery.ts
Normal file
@@ -0,0 +1,39 @@
|
||||
import { useQuery, useQueryClient } from '@tanstack/vue-query';
|
||||
import { api } from '@/packages/api/src';
|
||||
import { getCurrentOrganizationId } from '@/utils/useUser';
|
||||
import type { ProjectMember } from '@/packages/api/src';
|
||||
import { computed, type Ref } from 'vue';
|
||||
|
||||
export function useProjectMembersQuery(projectId: Ref<string | null> | string) {
|
||||
const queryClient = useQueryClient();
|
||||
|
||||
const projectIdValue = computed(() => {
|
||||
return typeof projectId === 'string' ? projectId : projectId.value;
|
||||
});
|
||||
|
||||
const query = useQuery({
|
||||
queryKey: ['projectMembers', projectIdValue],
|
||||
queryFn: async () => {
|
||||
const organizationId = getCurrentOrganizationId();
|
||||
const pid = projectIdValue.value;
|
||||
if (!organizationId || !pid) throw new Error('No organization or project');
|
||||
return api.getProjectMembers({
|
||||
params: { organization: organizationId, project: pid },
|
||||
});
|
||||
},
|
||||
enabled: () => !!getCurrentOrganizationId() && !!projectIdValue.value,
|
||||
staleTime: 1000 * 30, // 30 seconds
|
||||
});
|
||||
|
||||
const projectMembers = computed<ProjectMember[]>(() => query.data.value?.data ?? []);
|
||||
|
||||
const invalidateProjectMembers = () => {
|
||||
queryClient.invalidateQueries({ queryKey: ['projectMembers', projectIdValue.value] });
|
||||
};
|
||||
|
||||
return {
|
||||
...query,
|
||||
projectMembers,
|
||||
invalidateProjectMembers,
|
||||
};
|
||||
}
|
||||
Reference in New Issue
Block a user