khata compliant with new react-openapi
This commit is contained in:
@@ -350,12 +350,24 @@ function buildFilterComponent(field: FieldConfig, resourceName: string): React.F
|
||||
export function useResource(resourceName: string): UseResourceReturn {
|
||||
const { resources } = useAppContext();
|
||||
const resource = resources.find((r) => r.name === resourceName);
|
||||
if (!resource) {
|
||||
throw new Error(`Resource "${resourceName}" not found`);
|
||||
}
|
||||
|
||||
const [state, setState] = useState<ResourceState>({ loading: false, error: null });
|
||||
|
||||
if (!resource) {
|
||||
const noop = async () => { throw new Error(`Resource "${resourceName}" not found yet`); };
|
||||
return {
|
||||
resource: null as unknown as ResourceConfig,
|
||||
components: {},
|
||||
list: noop,
|
||||
get: noop,
|
||||
create: noop,
|
||||
update: noop,
|
||||
remove: noop,
|
||||
loading: false,
|
||||
error: null,
|
||||
};
|
||||
}
|
||||
|
||||
const setLoading = useCallback((loading: boolean) => {
|
||||
setState((s) => ({ ...s, loading }));
|
||||
}, []);
|
||||
@@ -437,7 +449,8 @@ export function useResource(resourceName: string): UseResourceReturn {
|
||||
setError(null);
|
||||
try {
|
||||
const api = getApi();
|
||||
const res = await api.put(`${resource.path}/${id}`, data);
|
||||
const method = resource.updateMethod ?? "put";
|
||||
const res = await (method === "patch" ? api.patch : api.put)(`${resource.path}/${id}`, data);
|
||||
return res.data;
|
||||
} catch (e: any) {
|
||||
setError(parseError(e));
|
||||
@@ -446,7 +459,7 @@ export function useResource(resourceName: string): UseResourceReturn {
|
||||
setLoading(false);
|
||||
}
|
||||
},
|
||||
[resource.path, setLoading, setError]
|
||||
[resource.path, resource.updateMethod, setLoading, setError]
|
||||
);
|
||||
|
||||
const remove = useCallback(
|
||||
|
||||
Reference in New Issue
Block a user