muffin-admin 2.3.13 → 2.5.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -4,22 +4,22 @@ import SvgIcon from "@mui/material/SvgIcon";
4
4
  import Tooltip from "@mui/material/Tooltip";
5
5
  import Typography from "@mui/material/Typography";
6
6
  import createTheme from "@mui/material/styles/createTheme";
7
- import { Admin, AppBar, Layout, Login, ToggleThemeButton, defaultTheme, } from "react-admin";
7
+ import { Admin, AppBar, Layout, Login } from "react-admin";
8
8
  import { AdminPropsContext, buildAdmin, findBuilder, findIcon, setupAdmin } from "./utils";
9
9
  const darkTheme = createTheme({
10
10
  palette: { mode: "dark" },
11
11
  });
12
12
  export function MuffinAdmin(props) {
13
13
  const { resources, auth, appBarLinks, adminProps, dashboard } = props;
14
- const children = resources.map((resource) => buildAdmin(["resource", resource.name], Object.assign(Object.assign({}, resource), { key: resource.name })));
14
+ const children = resources.map((resource) => buildAdmin(["resource", resource.name], Object.assign({}, resource)));
15
15
  document.title = adminProps.title || "Muffin Admin";
16
16
  const appBar = (props) => (_jsxs(AppBar, Object.assign({}, props, { children: [_jsx(Typography, { variant: "h6", color: "inherit", id: "react-admin-title", style: {
17
17
  flex: 1,
18
18
  textOverflow: "ellipsis",
19
19
  whiteSpace: "nowrap",
20
20
  overflow: "hidden",
21
- } }), _jsx(ToggleThemeButton, { lightTheme: defaultTheme, darkTheme: darkTheme }), appBarLinks.map((info) => (_jsx(Tooltip, { title: info.title, children: _jsx(IconButton, { color: "inherit", href: info.url, children: _jsx(SvgIcon, { component: findIcon(info.icon) }) }) }, info.url)))] })));
22
- return (_jsx(AdminPropsContext.Provider, { value: props, children: _jsx(Admin, { authProvider: buildAdmin(["authprovider"], auth), dashboard: dashboard ? buildAdmin(["dashboard"], dashboard) : null, dataProvider: buildAdmin(["dataprovider"], props), layout: (props) => _jsx(Layout, Object.assign({}, props, { appBar: appBar })), loginPage: findBuilder(["loginpage"]), requireAuth: auth.required, children: children }) }));
21
+ } }), appBarLinks.map((info) => (_jsx(Tooltip, { title: info.title, children: _jsx(IconButton, { color: "inherit", href: info.url, children: _jsx(SvgIcon, { component: findIcon(info.icon) }) }) }, info.url)))] })));
22
+ return (_jsx(AdminPropsContext.Provider, { value: props, children: _jsx(Admin, { authProvider: buildAdmin(["authprovider"], auth), dashboard: dashboard ? buildAdmin(["dashboard"], dashboard) : null, dataProvider: buildAdmin(["dataprovider"], props), layout: (props) => _jsx(Layout, Object.assign({}, props, { appBar: appBar })), loginPage: findBuilder(["loginpage"]), requireAuth: auth.required, darkTheme: { palette: { mode: "dark" } }, children: children }) }));
23
23
  }
24
24
  setupAdmin(["admin"], MuffinAdmin);
25
25
  // Initialize login page
@@ -14,6 +14,6 @@ import { Resource } from "react-admin";
14
14
  import { buildAdmin, findIcon, setupAdmin } from "./utils";
15
15
  export function MuffinResource(_a) {
16
16
  var { name, create: createProps, edit: editProps, list: listProps, show: showProps, icon } = _a, props = __rest(_a, ["name", "create", "edit", "list", "show", "icon"]);
17
- return (_jsx(Resource, Object.assign({ name: name, icon: findIcon(icon), create: createProps ? buildAdmin(["create", name], createProps) : undefined, edit: editProps ? buildAdmin(["edit", name], editProps) : undefined, list: buildAdmin(["list", name], listProps), show: buildAdmin(["show", name], showProps) }, props)));
17
+ return (_jsx(Resource, Object.assign({ name: name, icon: findIcon(icon), create: createProps ? buildAdmin(["create", name], createProps) : undefined, edit: editProps ? buildAdmin(["edit", name], editProps) : undefined, list: buildAdmin(["list", name], listProps), show: buildAdmin(["show", name], showProps) }, props), name));
18
18
  }
19
19
  setupAdmin(["resource"], MuffinResource);
@@ -1,15 +1,16 @@
1
- import { jsx as _jsx } from "react/jsx-runtime";
1
+ import { jsx as _jsx, Fragment as _Fragment, jsxs as _jsxs } from "react/jsx-runtime";
2
2
  import * as icons from "@mui/icons-material";
3
3
  import { Button, useListContext, useNotify, useRecordContext, useRefresh, useResourceContext, useUnselectAll, } from "react-admin";
4
+ import { useState } from "react";
4
5
  import { useAction } from "../hooks/useAction";
5
- import { buildIcon } from "../utils";
6
+ import { buildIcon, findBuilder } from "../utils";
6
7
  export function BulkActionButton({ label, icon, title, action }) {
7
8
  const resource = useResourceContext();
8
9
  const { selectedIds } = useListContext();
9
10
  const refresh = useRefresh(), unselectAll = useUnselectAll(resource), notify = useNotify();
10
- const mutation = useAction(resource, action);
11
+ const { mutate, isLoading } = useAction(action);
11
12
  const onClick = () => {
12
- mutation.mutate({
13
+ mutate({
13
14
  action,
14
15
  ids: selectedIds,
15
16
  }, {
@@ -24,28 +25,32 @@ export function BulkActionButton({ label, icon, title, action }) {
24
25
  },
25
26
  });
26
27
  };
27
- return (_jsx(Button, { label: label, title: title, onClick: onClick, disabled: mutation.isLoading, children: buildIcon(icon) }));
28
+ return (_jsx(Button, { label: label, title: title, onClick: onClick, disabled: isLoading, children: buildIcon(icon) }));
28
29
  }
29
30
  export function ActionButton(props) {
30
- const { icon, label, title, resource, action } = props;
31
+ const { icon, label, title, action } = props;
32
+ const [payload, setPayload] = useState(null);
33
+ const [payloadActive, setPayloadActive] = useState(false);
31
34
  const record = useRecordContext();
32
- const { mutate, isLoading } = useAction(resource, action);
35
+ const actionData = { record, payload };
36
+ const { mutate, isLoading } = useAction(action);
37
+ const PayloadBuilder = findBuilder(["action", "payload", action]);
33
38
  const refresh = useRefresh(), notify = useNotify(), Icon = icons[icon];
34
- const onClick = () => {
35
- mutate({ record }, {
36
- onSuccess: ({ data }) => {
39
+ const process = () => mutate(actionData, {
40
+ onSuccess: ({ data }) => {
41
+ if (data && data.message)
42
+ notify(data.message, { type: "success" });
43
+ if (data && data.redirectTo)
44
+ window.location = data.redirectTo;
45
+ else
37
46
  refresh();
38
- if (data && data.redirectTo)
39
- window.location = data.redirectTo;
40
- if (data && data.message)
41
- notify(data.message, { type: "success" });
42
- },
43
- onError: (err) => {
44
- notify(typeof err === "string" ? err : err.message, {
45
- type: "error",
46
- });
47
- },
48
- });
49
- };
50
- return (_jsx(Button, { label: label, title: title, onClick: onClick, disabled: isLoading, children: Icon && _jsx(Icon, {}) }));
47
+ },
48
+ onError: (err) => {
49
+ notify(typeof err === "string" ? err : err.message, { type: "error" });
50
+ },
51
+ });
52
+ return (_jsxs(_Fragment, { children: [_jsx(Button, { label: label, title: title, onClick: PayloadBuilder ? () => setPayloadActive(true) : process, disabled: isLoading, children: Icon && _jsx(Icon, {}) }), PayloadBuilder && (_jsx(PayloadBuilder, { onChange: (payload) => {
53
+ setPayload(payload);
54
+ process();
55
+ }, active: payloadActive }))] }));
51
56
  }
@@ -61,7 +61,12 @@ export declare function MuffinDataprovider({ apiUrl }: AdminOpts): {
61
61
  data: any;
62
62
  total: number;
63
63
  }>;
64
- runAction: (resource: any, action: any, props: any) => Promise<{
64
+ runAction: (resource: string, action: string, props: TActionProps) => Promise<{
65
65
  data: any;
66
66
  }>;
67
67
  };
68
+ export type TActionProps = {
69
+ payload?: any;
70
+ ids?: string[];
71
+ record: any;
72
+ };
@@ -47,30 +47,30 @@ export function MuffinDataprovider({ apiUrl }) {
47
47
  const { headers, data } = yield request(resource, { query });
48
48
  return { data, total: parseInt(headers.get("x-total"), 10) };
49
49
  }),
50
- getOne: (resource, { id }) => __awaiter(this, void 0, void 0, function* () {
50
+ getOne: (resource_1, _a) => __awaiter(this, [resource_1, _a], void 0, function* (resource, { id }) {
51
51
  return yield request(`${resource}/${id}`);
52
52
  }),
53
- create: (resource, { data }) => __awaiter(this, void 0, void 0, function* () {
53
+ create: (resource_2, _b) => __awaiter(this, [resource_2, _b], void 0, function* (resource, { data }) {
54
54
  return yield request(resource, {
55
55
  data,
56
56
  method: "POST",
57
57
  });
58
58
  }),
59
- update: (resource, { id, data }) => __awaiter(this, void 0, void 0, function* () {
59
+ update: (resource_3, _c) => __awaiter(this, [resource_3, _c], void 0, function* (resource, { id, data }) {
60
60
  return yield makeRequest(`${apiUrl}/${resource}/${id}`, {
61
61
  data,
62
62
  method: "PUT",
63
63
  });
64
64
  }),
65
- updateMany: (resource, { ids, data }) => __awaiter(this, void 0, void 0, function* () {
65
+ updateMany: (resource_4, _d) => __awaiter(this, [resource_4, _d], void 0, function* (resource, { ids, data }) {
66
66
  yield Promise.all(ids.map((id) => methods.update(resource, { id, data })));
67
67
  return { data: ids };
68
68
  }),
69
- delete: (resource, { id }) => __awaiter(this, void 0, void 0, function* () {
69
+ delete: (resource_5, _e) => __awaiter(this, [resource_5, _e], void 0, function* (resource, { id }) {
70
70
  yield request(`${resource}/${id}`, { method: "DELETE" });
71
71
  return { data: { id } };
72
72
  }),
73
- deleteMany: (resource, { ids }) => __awaiter(this, void 0, void 0, function* () {
73
+ deleteMany: (resource_6, _f) => __awaiter(this, [resource_6, _f], void 0, function* (resource, { ids }) {
74
74
  yield request(resource, {
75
75
  data: ids,
76
76
  method: "DELETE",
@@ -82,8 +82,8 @@ export function MuffinDataprovider({ apiUrl }) {
82
82
  const key = (meta === null || meta === void 0 ? void 0 : meta.key) || "id";
83
83
  return methods.getList(resource, { filter: { [key]: { $in: ids } } });
84
84
  },
85
- getManyReference: (resource, _a) => __awaiter(this, void 0, void 0, function* () {
86
- var { target, id, filter } = _a, opts = __rest(_a, ["target", "id", "filter"]);
85
+ getManyReference: (resource, _g) => __awaiter(this, void 0, void 0, function* () {
86
+ var { target, id, filter } = _g, opts = __rest(_g, ["target", "id", "filter"]);
87
87
  filter = filter || {};
88
88
  filter[target] = id;
89
89
  return yield methods.getList(resource, Object.assign({ filter }, opts));
@@ -7,7 +7,4 @@ export declare function AvatarField({ source, alt, style, nameProp, ...props }:
7
7
  }): import("react/jsx-runtime").JSX.Element;
8
8
  export declare namespace AvatarField {
9
9
  var displayName: string;
10
- var defaultProps: {
11
- addLabel: boolean;
12
- };
13
10
  }
@@ -28,6 +28,3 @@ export function AvatarField(_a) {
28
28
  return (_jsx(Avatar, { src: value, alt: alt, style: style, children: letters }));
29
29
  }
30
30
  AvatarField.displayName = "AvatarField";
31
- AvatarField.defaultProps = {
32
- addLabel: true,
33
- };
@@ -8,7 +8,4 @@ export declare function FKField({ reference, refSource, refKey, link, source, ..
8
8
  }): import("react/jsx-runtime").JSX.Element;
9
9
  export declare namespace FKField {
10
10
  var displayName: string;
11
- var defaultProps: {
12
- addLabel: boolean;
13
- };
14
11
  }
@@ -16,6 +16,3 @@ export function FKField(_a) {
16
16
  return (_jsx(ReferenceField, Object.assign({ link: link || "show", reference: reference, source: source, queryOptions: { meta: { key: refKey } } }, props, { children: _jsxs(_Fragment, { children: [_jsx(TextField, { source: refSource }), " (#", _jsx(TextField, { source: refKey || "id" }), ")"] }) })));
17
17
  }
18
18
  FKField.displayName = "FKField";
19
- FKField.defaultProps = {
20
- addLabel: true,
21
- };
@@ -0,0 +1 @@
1
+ export * from './useAction';
@@ -0,0 +1 @@
1
+ export * from './useAction';
@@ -1,4 +1,4 @@
1
- export declare function useAction(resource: any, action: any): import("react-query").UseMutationResult<{
1
+ export declare function useAction(action: any): import("react-query").UseMutationResult<{
2
2
  data: any;
3
3
  }, string | {
4
4
  message: string;
@@ -1,6 +1,7 @@
1
- import { useDataProvider } from "react-admin";
1
+ import { useDataProvider, useResourceContext } from "react-admin";
2
2
  import { useMutation } from "react-query";
3
- export function useAction(resource, action) {
3
+ export function useAction(action) {
4
4
  const dataProvider = useDataProvider();
5
+ const resource = useResourceContext();
5
6
  return useMutation((params) => dataProvider.runAction(resource, action, params));
6
7
  }
package/dist/index.d.ts CHANGED
@@ -11,6 +11,7 @@ export * from "./buildRA";
11
11
  export * from "./buttons";
12
12
  export * from "./dataprovider";
13
13
  export * from "./fields";
14
+ export * from "./hooks";
14
15
  export * from "./types";
15
16
  export * from "./utils";
16
17
  export declare function initAdmin(prefix?: string, containerId?: string): Promise<void>;
package/dist/index.js CHANGED
@@ -22,10 +22,11 @@ export * from "./buildRA";
22
22
  export * from "./buttons";
23
23
  export * from "./dataprovider";
24
24
  export * from "./fields";
25
+ export * from "./hooks";
25
26
  export * from "./types";
26
27
  export * from "./utils";
27
- export function initAdmin(prefix = "", containerId = "root") {
28
- return __awaiter(this, void 0, void 0, function* () {
28
+ export function initAdmin() {
29
+ return __awaiter(this, arguments, void 0, function* (prefix = "", containerId = "root") {
29
30
  const response = yield fetch(`${prefix}/ra.json`);
30
31
  const props = yield response.json();
31
32
  const container = document.getElementById(containerId);
@@ -26,5 +26,5 @@ export function FKInput(_a) {
26
26
  return `#${pk}`;
27
27
  return (pk && `${record[refSource]} (#${pk})`) || "";
28
28
  };
29
- return (_jsx(ReferenceInput, Object.assign({ reference: reference, source: source }, props, { children: _jsx(AutocompleteInput, Object.assign({ source: refKey, optionText: renderText, fullWidth: fullWidth }, props)) })));
29
+ return (_jsx(ReferenceInput, { reference: reference, source: source, children: _jsx(AutocompleteInput, Object.assign({ source: refKey, optionText: renderText, fullWidth: fullWidth }, props)) }));
30
30
  }