fixes
This commit is contained in:
@@ -50,7 +50,7 @@ interface UseResourceReturn {
|
|||||||
resource: ResourceConfig;
|
resource: ResourceConfig;
|
||||||
components: Record<string, React.FC<FilterComponentProps>>;
|
components: Record<string, React.FC<FilterComponentProps>>;
|
||||||
list: (params?: Record<string, any>) => Promise<ParsedListResponse>;
|
list: (params?: Record<string, any>) => Promise<ParsedListResponse>;
|
||||||
get: (id: string | number) => Promise<any>;
|
get: (id: string | number, params?: Record<string, any>) => Promise<any>;
|
||||||
create: (data: any) => Promise<any>;
|
create: (data: any) => Promise<any>;
|
||||||
update: (id: string | number, data: any) => Promise<any>;
|
update: (id: string | number, data: any) => Promise<any>;
|
||||||
remove: (id: string | number) => Promise<void>;
|
remove: (id: string | number) => Promise<void>;
|
||||||
@@ -366,20 +366,11 @@ export function useResource(resourceName: string): UseResourceReturn {
|
|||||||
|
|
||||||
const [state, setState] = useState<ResourceState>({ loading: false, error: null });
|
const [state, setState] = useState<ResourceState>({ loading: false, error: null });
|
||||||
|
|
||||||
if (!resource) {
|
const rPath = resource?.path;
|
||||||
const noop = async () => { throw new Error(`Resource "${resourceName}" not found yet`); };
|
const rPagination = resource?.pagination;
|
||||||
return {
|
const rUpdateMethod = resource?.updateMethod;
|
||||||
resource: null as unknown as ResourceConfig,
|
const rStreaming = resource?.streaming;
|
||||||
components: {},
|
const rFields = resource?.fields;
|
||||||
list: noop,
|
|
||||||
get: noop,
|
|
||||||
create: noop,
|
|
||||||
update: noop,
|
|
||||||
remove: noop,
|
|
||||||
loading: false,
|
|
||||||
error: null,
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
const setLoading = useCallback((loading: boolean) => {
|
const setLoading = useCallback((loading: boolean) => {
|
||||||
setState((s) => ({ ...s, loading }));
|
setState((s) => ({ ...s, loading }));
|
||||||
@@ -391,22 +382,26 @@ export function useResource(resourceName: string): UseResourceReturn {
|
|||||||
|
|
||||||
const list = useCallback(
|
const list = useCallback(
|
||||||
async (params?: Record<string, any>): Promise<ParsedListResponse> => {
|
async (params?: Record<string, any>): Promise<ParsedListResponse> => {
|
||||||
|
if (!rPath) return { items: [] };
|
||||||
setLoading(true);
|
setLoading(true);
|
||||||
setError(null);
|
setError(null);
|
||||||
try {
|
try {
|
||||||
const api = getApi();
|
const api = getApi();
|
||||||
const res = await api.get(resource.path, { params });
|
const res = await api.get(rPath, { params });
|
||||||
const data = res.data;
|
const data = res.data;
|
||||||
|
|
||||||
if (resource.pagination) {
|
if (rPagination) {
|
||||||
|
if (Array.isArray(data)) {
|
||||||
|
return { items: data };
|
||||||
|
}
|
||||||
if (!data || typeof data !== "object" || !Array.isArray(data.items)) {
|
if (!data || typeof data !== "object" || !Array.isArray(data.items)) {
|
||||||
throw new Error(`Expected paginated response { total, items } from ${resource.path}`);
|
throw new Error(`Expected paginated response { total, items } from ${rPath}`);
|
||||||
}
|
}
|
||||||
return { items: data.items, total: data.total ?? data.items.length };
|
return { items: data.items, total: data.total ?? data.items.length };
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!Array.isArray(data)) {
|
if (!Array.isArray(data)) {
|
||||||
throw new Error(`Expected array response from ${resource.path}`);
|
throw new Error(`Expected array response from ${rPath}`);
|
||||||
}
|
}
|
||||||
return { items: data };
|
return { items: data };
|
||||||
} catch (e: any) {
|
} catch (e: any) {
|
||||||
@@ -417,16 +412,17 @@ export function useResource(resourceName: string): UseResourceReturn {
|
|||||||
setLoading(false);
|
setLoading(false);
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
[resource.path, resource.pagination, setLoading, setError]
|
[rPath, rPagination, setLoading, setError]
|
||||||
);
|
);
|
||||||
|
|
||||||
const get = useCallback(
|
const get = useCallback(
|
||||||
async (id: string | number): Promise<any> => {
|
async (id: string | number, params?: Record<string, any>): Promise<any> => {
|
||||||
|
if (!rPath) throw new Error(`Resource "${resourceName}" not found yet`);
|
||||||
setLoading(true);
|
setLoading(true);
|
||||||
setError(null);
|
setError(null);
|
||||||
try {
|
try {
|
||||||
const api = getApi();
|
const api = getApi();
|
||||||
const res = await api.get(`${resource.path}/${id}`);
|
const res = await api.get(`${rPath}/${id}`, { params });
|
||||||
return res.data;
|
return res.data;
|
||||||
} catch (e: any) {
|
} catch (e: any) {
|
||||||
setError(parseError(e));
|
setError(parseError(e));
|
||||||
@@ -435,16 +431,17 @@ export function useResource(resourceName: string): UseResourceReturn {
|
|||||||
setLoading(false);
|
setLoading(false);
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
[resource.path, setLoading, setError]
|
[rPath, setLoading, setError]
|
||||||
);
|
);
|
||||||
|
|
||||||
const create = useCallback(
|
const create = useCallback(
|
||||||
async (data: any): Promise<any> => {
|
async (data: any): Promise<any> => {
|
||||||
|
if (!rPath) throw new Error(`Resource "${resourceName}" not found yet`);
|
||||||
setLoading(true);
|
setLoading(true);
|
||||||
setError(null);
|
setError(null);
|
||||||
try {
|
try {
|
||||||
const api = getApi();
|
const api = getApi();
|
||||||
const res = await api.post(resource.path, data);
|
const res = await api.post(rPath, data);
|
||||||
return res.data;
|
return res.data;
|
||||||
} catch (e: any) {
|
} catch (e: any) {
|
||||||
setError(parseError(e));
|
setError(parseError(e));
|
||||||
@@ -453,17 +450,18 @@ export function useResource(resourceName: string): UseResourceReturn {
|
|||||||
setLoading(false);
|
setLoading(false);
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
[resource.path, setLoading, setError]
|
[rPath, setLoading, setError]
|
||||||
);
|
);
|
||||||
|
|
||||||
const update = useCallback(
|
const update = useCallback(
|
||||||
async (id: string | number, data: any): Promise<any> => {
|
async (id: string | number, data: any): Promise<any> => {
|
||||||
|
if (!rPath) throw new Error(`Resource "${resourceName}" not found yet`);
|
||||||
setLoading(true);
|
setLoading(true);
|
||||||
setError(null);
|
setError(null);
|
||||||
try {
|
try {
|
||||||
const api = getApi();
|
const api = getApi();
|
||||||
const method = resource.updateMethod ?? "put";
|
const method = rUpdateMethod ?? "put";
|
||||||
const res = await (method === "patch" ? api.patch : api.put)(`${resource.path}/${id}`, data);
|
const res = await (method === "patch" ? api.patch : api.put)(`${rPath}/${id}`, data);
|
||||||
return res.data;
|
return res.data;
|
||||||
} catch (e: any) {
|
} catch (e: any) {
|
||||||
setError(parseError(e));
|
setError(parseError(e));
|
||||||
@@ -472,16 +470,17 @@ export function useResource(resourceName: string): UseResourceReturn {
|
|||||||
setLoading(false);
|
setLoading(false);
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
[resource.path, resource.updateMethod, setLoading, setError]
|
[rPath, rUpdateMethod, setLoading, setError]
|
||||||
);
|
);
|
||||||
|
|
||||||
const remove = useCallback(
|
const remove = useCallback(
|
||||||
async (id: string | number): Promise<void> => {
|
async (id: string | number): Promise<void> => {
|
||||||
|
if (!rPath) throw new Error(`Resource "${resourceName}" not found yet`);
|
||||||
setLoading(true);
|
setLoading(true);
|
||||||
setError(null);
|
setError(null);
|
||||||
try {
|
try {
|
||||||
const api = getApi();
|
const api = getApi();
|
||||||
await api.delete(`${resource.path}/${id}`);
|
await api.delete(`${rPath}/${id}`);
|
||||||
} catch (e: any) {
|
} catch (e: any) {
|
||||||
setError(parseError(e));
|
setError(parseError(e));
|
||||||
throw e;
|
throw e;
|
||||||
@@ -489,17 +488,17 @@ export function useResource(resourceName: string): UseResourceReturn {
|
|||||||
setLoading(false);
|
setLoading(false);
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
[resource.path, setLoading, setError]
|
[rPath, setLoading, setError]
|
||||||
);
|
);
|
||||||
|
|
||||||
const stream = useCallback(
|
const stream = useCallback(
|
||||||
(handlers: StreamHandlers): StreamSubscription => {
|
(handlers: StreamHandlers): StreamSubscription => {
|
||||||
if (!resource.streaming) {
|
if (!rPath || !rStreaming) {
|
||||||
throw new Error(`Resource "${resourceName}" does not support streaming`);
|
throw new Error(`Resource "${resourceName}" does not support streaming`);
|
||||||
}
|
}
|
||||||
const api = getApi();
|
const api = getApi();
|
||||||
const baseUrl = (api.defaults.baseURL ?? "").replace(/\/+$/, "");
|
const baseUrl = (api.defaults.baseURL ?? "").replace(/\/+$/, "");
|
||||||
const url = baseUrl + resource.path;
|
const url = baseUrl + rPath;
|
||||||
const es = new EventSource(url);
|
const es = new EventSource(url);
|
||||||
|
|
||||||
es.onopen = () => handlers.onOpen?.();
|
es.onopen = () => handlers.onOpen?.();
|
||||||
@@ -517,19 +516,35 @@ export function useResource(resourceName: string): UseResourceReturn {
|
|||||||
|
|
||||||
return { close: () => es.close() };
|
return { close: () => es.close() };
|
||||||
},
|
},
|
||||||
[resource.path, resource.streaming, resourceName]
|
[rPath, rStreaming, resourceName]
|
||||||
);
|
);
|
||||||
|
|
||||||
const components = useMemo(
|
const components = useMemo(
|
||||||
() => {
|
() => {
|
||||||
const map: Record<string, React.FC<FilterComponentProps>> = {};
|
const map: Record<string, React.FC<FilterComponentProps>> = {};
|
||||||
for (const field of resource.fields) {
|
if (!rFields) return map;
|
||||||
|
for (const field of rFields) {
|
||||||
map[field.name] = buildFilterComponent(field, resourceName);
|
map[field.name] = buildFilterComponent(field, resourceName);
|
||||||
}
|
}
|
||||||
return map;
|
return map;
|
||||||
},
|
},
|
||||||
[resource.fields, resourceName]
|
[rFields, resourceName]
|
||||||
);
|
);
|
||||||
|
|
||||||
return { resource, components, list, get, create, update, remove, stream: resource.streaming ? stream : undefined, loading: state.loading, error: state.error };
|
if (!resource) {
|
||||||
|
return {
|
||||||
|
resource: null as unknown as ResourceConfig,
|
||||||
|
components,
|
||||||
|
list,
|
||||||
|
get,
|
||||||
|
create,
|
||||||
|
update,
|
||||||
|
remove,
|
||||||
|
stream: undefined,
|
||||||
|
loading: false,
|
||||||
|
error: null,
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
return { resource, components, list, get, create, update, remove, stream: rStreaming ? stream : undefined, loading: state.loading, error: state.error };
|
||||||
}
|
}
|
||||||
@@ -12,9 +12,11 @@ export interface ReportParams {
|
|||||||
export function useReport(params: ReportParams) {
|
export function useReport(params: ReportParams) {
|
||||||
const { get } = useResource("reports");
|
const { get } = useResource("reports");
|
||||||
|
|
||||||
|
const { snapshot_id, ...queryParams } = params;
|
||||||
|
|
||||||
return useQuery({
|
return useQuery({
|
||||||
queryKey: ["reports", "read", params],
|
queryKey: ["reports", "read", params],
|
||||||
queryFn: () =>
|
queryFn: () =>
|
||||||
get(params.snapshot_id ? params.snapshot_id : "latest"),
|
get(snapshot_id ?? "latest", queryParams),
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user