muffin-admin 3.1.0 → 3.1.2

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.
@@ -20,7 +20,7 @@ setupAdmin(["list-filters"], buildRA);
20
20
  function MuffinListDatagrid() {
21
21
  const { name, list } = useMuffinResourceOpts();
22
22
  const { fields, edit, show } = list;
23
- return (_jsxs(DatagridConfigurable, { rowClick: show ? "show" : edit ? "edit" : false, children: [buildAdmin(["list-fields", name], fields), buildAdmin(["list-grid-buttons"], name)] }));
23
+ return (_jsxs(DatagridConfigurable, { rowClick: show ? "show" : edit ? "edit" : false, children: [buildAdmin(["list-fields", name], fields), buildAdmin(["list-grid-buttons", name])] }));
24
24
  }
25
25
  setupAdmin(["list-grid"], MuffinListDatagrid);
26
26
  function MuffinListGridButtons() {
@@ -1,3 +1,3 @@
1
1
  import { AdminAction } from "../types";
2
- export declare function BulkActionButton({ label, icon, title, action, id }: AdminAction): import("react/jsx-runtime").JSX.Element;
3
2
  export declare function ActionButton({ icon, label, title, action, id }: AdminAction): import("react/jsx-runtime").JSX.Element;
3
+ export declare function BulkActionButton({ label, icon, title, action, id }: AdminAction): import("react/jsx-runtime").JSX.Element;
@@ -3,48 +3,47 @@ import { Button, useListContext, useNotify, useRecordContext, useRefresh, useRes
3
3
  import { useState } from "react";
4
4
  import { useAction } from "../hooks/useAction";
5
5
  import { buildIcon, findBuilder } from "../utils";
6
- export function BulkActionButton({ label, icon, title, action, id }) {
7
- const resource = useResourceContext();
8
- const { selectedIds } = useListContext();
6
+ export function ActionButton({ icon, label, title, action, id }) {
9
7
  const [payloadActive, setPayloadActive] = useState(false);
10
- const PayloadBuilder = findBuilder(["action", "payload", id]);
11
- const refresh = useRefresh(), unselectAll = useUnselectAll(resource), notify = useNotify();
12
- const { mutate, isLoading } = useAction(action);
13
- const process = (payload) => mutate({ ids: selectedIds, payload }, {
8
+ const record = useRecordContext();
9
+ const notify = useNotify();
10
+ const refresh = useRefresh();
11
+ const { mutate, isLoading } = useAction(action, {
14
12
  onSuccess: ({ data }) => {
15
13
  if (data && data.message)
16
14
  notify(data.message, { type: "success" });
17
15
  if (data && data.redirectTo)
18
16
  window.location = data.redirectTo;
19
- else {
20
- unselectAll();
17
+ else
21
18
  refresh();
22
- }
23
19
  },
24
20
  onError: (err) => {
25
21
  notify(typeof err === "string" ? err : err.message, { type: "error" });
26
22
  },
27
23
  });
28
- return (_jsxs(_Fragment, { children: [_jsx(Button, { label: label, title: title, onClick: () => (PayloadBuilder ? setPayloadActive(true) : process()), disabled: isLoading, children: buildIcon(icon) }), PayloadBuilder && (_jsx(PayloadBuilder, { active: payloadActive, onHandle: (payload) => process(payload), onClose: () => setPayloadActive(false) }))] }));
24
+ const PayloadBuilder = findBuilder(["action", "payload", id]);
25
+ return (_jsxs(_Fragment, { children: [_jsx(Button, { label: label, title: title, onClick: () => (PayloadBuilder ? setPayloadActive(true) : mutate({ record })), disabled: isLoading, children: buildIcon(icon) }), PayloadBuilder && (_jsx(PayloadBuilder, { active: payloadActive, onHandle: (payload) => mutate({ record, payload }), onClose: () => setPayloadActive(false) }))] }));
29
26
  }
30
- export function ActionButton({ icon, label, title, action, id }) {
27
+ export function BulkActionButton({ label, icon, title, action, id }) {
28
+ const resource = useResourceContext();
29
+ const { selectedIds } = useListContext();
31
30
  const [payloadActive, setPayloadActive] = useState(false);
32
- const record = useRecordContext();
33
- const { mutate, isLoading } = useAction(action);
34
- const PayloadBuilder = findBuilder(["action", "payload", id]);
35
- const refresh = useRefresh(), notify = useNotify();
36
- const process = (payload) => mutate({ record, payload }, {
31
+ const refresh = useRefresh(), unselectAll = useUnselectAll(resource), notify = useNotify();
32
+ const { mutate, isLoading } = useAction(action, {
37
33
  onSuccess: ({ data }) => {
38
34
  if (data && data.message)
39
35
  notify(data.message, { type: "success" });
40
36
  if (data && data.redirectTo)
41
37
  window.location = data.redirectTo;
42
- else
38
+ else {
39
+ unselectAll();
43
40
  refresh();
41
+ }
44
42
  },
45
43
  onError: (err) => {
46
44
  notify(typeof err === "string" ? err : err.message, { type: "error" });
47
45
  },
48
46
  });
49
- return (_jsxs(_Fragment, { children: [_jsx(Button, { label: label, title: title, onClick: () => (PayloadBuilder ? setPayloadActive(true) : process()), disabled: isLoading, children: buildIcon(icon) }), PayloadBuilder && (_jsx(PayloadBuilder, { active: payloadActive, onHandle: (payload) => process(payload), onClose: () => setPayloadActive(false) }))] }));
47
+ const PayloadBuilder = findBuilder(["action", "payload", id]);
48
+ return (_jsxs(_Fragment, { children: [_jsx(Button, { label: label, title: title, onClick: () => (PayloadBuilder ? setPayloadActive(true) : mutate({ ids: selectedIds })), disabled: isLoading, children: buildIcon(icon) }), PayloadBuilder && (_jsx(PayloadBuilder, { active: payloadActive, onHandle: (payload) => mutate({ ids: selectedIds, payload }), onClose: () => setPayloadActive(false) }))] }));
50
49
  }
@@ -61,8 +61,8 @@ export declare function MuffinDataprovider(apiUrl: string): {
61
61
  data: any;
62
62
  }>;
63
63
  };
64
- export type TActionProps = {
64
+ export type TActionProps<T = any> = {
65
65
  payload?: any;
66
66
  ids?: string[];
67
- record: any;
67
+ record?: T;
68
68
  };
@@ -1,5 +1,7 @@
1
- export declare function useAction(action: any): import("react-query").UseMutationResult<{
1
+ import { UseMutationOptions } from "react-query";
2
+ import { TActionProps } from "../dataprovider";
3
+ export declare function useAction(action: string, options?: UseMutationOptions<{
2
4
  data: any;
3
- }, string | {
4
- message: string;
5
- }, any, unknown>;
5
+ }, any, TActionProps>): import("react-query").UseMutationResult<{
6
+ data: any;
7
+ }, any, TActionProps, unknown>;
@@ -1,7 +1,7 @@
1
1
  import { useDataProvider, useResourceContext } from "react-admin";
2
2
  import { useMutation } from "react-query";
3
- export function useAction(action) {
4
- const dataProvider = useDataProvider();
3
+ export function useAction(action, options) {
5
4
  const resource = useResourceContext();
6
- return useMutation((params) => dataProvider.runAction(resource, action, params));
5
+ const dataProvider = useDataProvider();
6
+ return useMutation((params) => dataProvider.runAction(resource, action, params), options);
7
7
  }