mirror of
https://github.com/SamyRai/turash.git
synced 2025-12-26 23:01:33 +00:00
- Remove nested git repository from bugulma/frontend/.git - Add all frontend files to main repository tracking - Convert from separate frontend/backend repos to unified monorepo - Preserve all frontend code and development history as tracked files - Eliminate nested repository complexity for simpler development workflow This creates a proper monorepo structure with frontend and backend coexisting in the same repository for easier development and deployment.
101 lines
2.9 KiB
TypeScript
101 lines
2.9 KiB
TypeScript
import { useMutation, useQuery, useQueryClient } from '@tanstack/react-query';
|
|
import {
|
|
getSiteById,
|
|
getSitesByOrganization,
|
|
getNearbySites,
|
|
createSite,
|
|
deleteSite,
|
|
type CreateSiteRequest,
|
|
type NearbySitesQuery,
|
|
} from '@/services/sites-api';
|
|
|
|
/**
|
|
* Query key factory for sites
|
|
*/
|
|
export const siteKeys = {
|
|
all: ['sites'] as const,
|
|
lists: () => [...siteKeys.all, 'list'] as const,
|
|
list: (filters?: Record<string, unknown>) => [...siteKeys.lists(), filters] as const,
|
|
details: () => [...siteKeys.all, 'detail'] as const,
|
|
detail: (id: string) => [...siteKeys.details(), id] as const,
|
|
byOrganization: (organizationId: string) =>
|
|
[...siteKeys.all, 'organization', organizationId] as const,
|
|
nearby: (query: NearbySitesQuery) => [...siteKeys.all, 'nearby', query] as const,
|
|
// Deprecated: kept for backwards compatibility
|
|
byBusiness: (businessId: string) => siteKeys.byOrganization(businessId),
|
|
};
|
|
|
|
/**
|
|
* Hook to fetch a single site by ID
|
|
* Returns undefined immediately to prevent blocking render
|
|
*/
|
|
export function useSite(id: string | null | undefined) {
|
|
return useQuery({
|
|
queryKey: siteKeys.detail(id!),
|
|
queryFn: () => getSiteById(id!),
|
|
enabled: !!id,
|
|
placeholderData: undefined, // Render immediately, data loads async
|
|
});
|
|
}
|
|
|
|
/**
|
|
* Hook to fetch sites by organization ID
|
|
* Sites belong to Organizations (not just Businesses)
|
|
* Returns empty array immediately to prevent blocking render
|
|
*/
|
|
export function useSitesByOrganization(organizationId: string | null | undefined) {
|
|
return useQuery({
|
|
queryKey: siteKeys.byOrganization(organizationId!),
|
|
queryFn: () => getSitesByOrganization(organizationId!),
|
|
enabled: !!organizationId,
|
|
placeholderData: [], // Render immediately with empty array
|
|
});
|
|
}
|
|
|
|
/**
|
|
* @deprecated Use useSitesByOrganization instead
|
|
* Kept for backwards compatibility during migration
|
|
*/
|
|
export const useSitesByBusiness = useSitesByOrganization;
|
|
|
|
/**
|
|
* Hook to fetch nearby sites
|
|
* Returns empty array immediately to prevent blocking render
|
|
*/
|
|
export function useNearbySites(query: NearbySitesQuery | null | undefined) {
|
|
return useQuery({
|
|
queryKey: siteKeys.nearby(query!),
|
|
queryFn: () => getNearbySites(query!),
|
|
enabled: !!query,
|
|
placeholderData: [], // Render immediately with empty array
|
|
});
|
|
}
|
|
|
|
/**
|
|
* Hook to create a new site
|
|
*/
|
|
export function useCreateSite() {
|
|
const queryClient = useQueryClient();
|
|
|
|
return useMutation({
|
|
mutationFn: (request: CreateSiteRequest) => createSite(request),
|
|
onSuccess: () => {
|
|
queryClient.invalidateQueries({ queryKey: siteKeys.lists() });
|
|
},
|
|
});
|
|
}
|
|
|
|
/**
|
|
* Hook to delete a site
|
|
*/
|
|
export function useDeleteSite() {
|
|
const queryClient = useQueryClient();
|
|
|
|
return useMutation({
|
|
mutationFn: (id: string) => deleteSite(id),
|
|
onSuccess: () => {
|
|
queryClient.invalidateQueries({ queryKey: siteKeys.lists() });
|
|
},
|
|
});
|
|
}
|