updated admin to use swr hooks instead of react-query

This commit is contained in:
dswbx
2024-12-13 16:24:55 +01:00
parent 50c5adce0c
commit 8c91dff94d
20 changed files with 380 additions and 275 deletions

View File

@@ -1,9 +1,20 @@
import type { PrimaryFieldType } from "core";
import { objectTransform } from "core/utils";
import type { EntityData, RepoQuery } from "data";
import type { ResponseObject } from "modules/ModuleApi";
import useSWR, { type SWRConfiguration } from "swr";
import { useApi } from "ui/client";
export class UseEntityApiError<Payload = any> extends Error {
constructor(
public payload: Payload,
public response: Response,
message?: string
) {
super(message ?? "UseEntityApiError");
}
}
export const useEntity = <
Entity extends string,
Id extends PrimaryFieldType | undefined = undefined
@@ -16,18 +27,27 @@ export const useEntity = <
return {
create: async (input: EntityData) => {
const res = await api.createOne(entity, input);
return res.data;
if (!res.ok) {
throw new UseEntityApiError(res.data, res.res, "Failed to create entity");
}
return res;
},
read: async (query: Partial<RepoQuery> = {}) => {
const res = id ? await api.readOne(entity, id!, query) : await api.readMany(entity, query);
return res.data;
if (!res.ok) {
throw new UseEntityApiError(res.data, res.res, "Failed to read entity");
}
return res;
},
update: async (input: Partial<EntityData>, _id: PrimaryFieldType | undefined = id) => {
if (!_id) {
throw new Error("id is required");
}
const res = await api.updateOne(entity, _id, input);
return res.data;
if (!res.ok) {
throw new UseEntityApiError(res.data, res.res, "Failed to update entity");
}
return res;
},
_delete: async (_id: PrimaryFieldType | undefined = id) => {
if (!_id) {
@@ -35,7 +55,10 @@ export const useEntity = <
}
const res = await api.deleteOne(entity, _id);
return res.data;
if (!res.ok) {
throw new UseEntityApiError(res.data, res.res, "Failed to delete entity");
}
return res;
}
};
};
@@ -47,24 +70,30 @@ export const useEntityQuery = <
entity: Entity,
id?: Id,
query?: Partial<RepoQuery>,
options?: SWRConfiguration
options?: SWRConfiguration & { enabled?: boolean }
) => {
const api = useApi().data;
const key = [...(api.options?.basepath?.split("/") ?? []), entity, ...(id ? [id] : [])].filter(
Boolean
);
const key =
options?.enabled !== false
? [...(api.options?.basepath?.split("/") ?? []), entity, ...(id ? [id] : [])].filter(
Boolean
)
: null;
const { read, ...actions } = useEntity(entity, id) as any;
const fetcher = id ? () => read(query) : () => null;
const swr = useSWR<EntityData>(id ? key : null, fetcher, options);
const fetcher = () => read(query);
type T = Awaited<ReturnType<(typeof api)[Id extends undefined ? "readMany" : "readOne"]>>;
const swr = useSWR<T>(key, fetcher, {
revalidateOnFocus: false,
keepPreviousData: false,
...options
});
const mapped = objectTransform(actions, (action) => {
if (action === "read") return;
return async (...args) => {
return swr.mutate(async () => {
const res = await action(...args);
return res;
});
return swr.mutate(action(...args)) as any;
};
}) as Omit<ReturnType<typeof useEntity<Entity, Id>>, "read">;
@@ -74,3 +103,18 @@ export const useEntityQuery = <
key
};
};
export const useEntityMutate = <
Entity extends string,
Id extends PrimaryFieldType | undefined = undefined
>(
entity: Entity,
id?: Id,
options?: SWRConfiguration
) => {
const { data, ...$q } = useEntityQuery(entity, id, undefined, {
...options,
enabled: false
});
return $q;
};