muffin-admin 2.4.0 → 2.5.1

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.
@@ -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,33 @@ 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, { active: payloadActive, onChange: (payload) => {
53
+ setPayloadActive(false);
54
+ setPayload(payload);
55
+ process();
56
+ }, onClose: () => setPayloadActive(false) }))] }));
51
57
  }
@@ -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
+ };
@@ -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,6 +22,7 @@ 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
28
  export function initAdmin() {
@@ -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
  }