muffin-admin 3.1.2 → 3.2.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.
@@ -1,49 +1,19 @@
1
1
  import { jsx as _jsx, Fragment as _Fragment, jsxs as _jsxs } from "react/jsx-runtime";
2
- import { Button, useListContext, useNotify, useRecordContext, useRefresh, useResourceContext, useUnselectAll, } from "react-admin";
2
+ import { Button, useListContext, useRecordContext } from "react-admin";
3
3
  import { useState } from "react";
4
4
  import { useAction } from "../hooks/useAction";
5
5
  import { buildIcon, findBuilder } from "../utils";
6
6
  export function ActionButton({ icon, label, title, action, id }) {
7
7
  const [payloadActive, setPayloadActive] = useState(false);
8
8
  const record = useRecordContext();
9
- const notify = useNotify();
10
- const refresh = useRefresh();
11
- const { mutate, isLoading } = useAction(action, {
12
- onSuccess: ({ data }) => {
13
- if (data && data.message)
14
- notify(data.message, { type: "success" });
15
- if (data && data.redirectTo)
16
- window.location = data.redirectTo;
17
- else
18
- refresh();
19
- },
20
- onError: (err) => {
21
- notify(typeof err === "string" ? err : err.message, { type: "error" });
22
- },
23
- });
9
+ const { mutate, isLoading } = useAction(action, {});
24
10
  const PayloadBuilder = findBuilder(["action", "payload", id]);
25
11
  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) }))] }));
26
12
  }
27
13
  export function BulkActionButton({ label, icon, title, action, id }) {
28
- const resource = useResourceContext();
29
14
  const { selectedIds } = useListContext();
15
+ const { mutate, isLoading } = useAction(action);
30
16
  const [payloadActive, setPayloadActive] = useState(false);
31
- const refresh = useRefresh(), unselectAll = useUnselectAll(resource), notify = useNotify();
32
- const { mutate, isLoading } = useAction(action, {
33
- onSuccess: ({ data }) => {
34
- if (data && data.message)
35
- notify(data.message, { type: "success" });
36
- if (data && data.redirectTo)
37
- window.location = data.redirectTo;
38
- else {
39
- unselectAll();
40
- refresh();
41
- }
42
- },
43
- onError: (err) => {
44
- notify(typeof err === "string" ? err : err.message, { type: "error" });
45
- },
46
- });
47
17
  const PayloadBuilder = findBuilder(["action", "payload", id]);
48
18
  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) }))] }));
49
19
  }
@@ -1,7 +1,21 @@
1
- import { useDataProvider, useResourceContext } from "react-admin";
1
+ import { useDataProvider, useNotify, useRefresh, useResourceContext, useUnselectAll, } from "react-admin";
2
2
  import { useMutation } from "react-query";
3
3
  export function useAction(action, options) {
4
+ const notify = useNotify();
5
+ const refresh = useRefresh();
4
6
  const resource = useResourceContext();
7
+ const unselectAll = useUnselectAll(resource);
5
8
  const dataProvider = useDataProvider();
6
- return useMutation((params) => dataProvider.runAction(resource, action, params), options);
9
+ return useMutation((params) => dataProvider.runAction(resource, action, params), Object.assign({ onSuccess: ({ data }) => {
10
+ if (data && data.message)
11
+ notify(data.message, { type: "success" });
12
+ if (data && data.redirectTo)
13
+ window.location = data.redirectTo;
14
+ else {
15
+ unselectAll();
16
+ refresh();
17
+ }
18
+ }, onError: (err) => {
19
+ notify(typeof err === "string" ? err : err.message, { type: "error" });
20
+ } }, options));
7
21
  }