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