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.
package/package.json
CHANGED
|
@@ -1,12 +1,4 @@
|
|
|
1
|
-
import {
|
|
2
|
-
Button,
|
|
3
|
-
useListContext,
|
|
4
|
-
useNotify,
|
|
5
|
-
useRecordContext,
|
|
6
|
-
useRefresh,
|
|
7
|
-
useResourceContext,
|
|
8
|
-
useUnselectAll,
|
|
9
|
-
} from "react-admin"
|
|
1
|
+
import { Button, useListContext, useRecordContext } from "react-admin"
|
|
10
2
|
|
|
11
3
|
import { useState } from "react"
|
|
12
4
|
import { useAction } from "../hooks/useAction"
|
|
@@ -16,18 +8,7 @@ import { buildIcon, findBuilder } from "../utils"
|
|
|
16
8
|
export function ActionButton({ icon, label, title, action, id }: AdminAction) {
|
|
17
9
|
const [payloadActive, setPayloadActive] = useState(false)
|
|
18
10
|
const record = useRecordContext()
|
|
19
|
-
const
|
|
20
|
-
const refresh = useRefresh()
|
|
21
|
-
const { mutate, isLoading } = useAction(action, {
|
|
22
|
-
onSuccess: ({ data }) => {
|
|
23
|
-
if (data && data.message) notify(data.message, { type: "success" })
|
|
24
|
-
if (data && data.redirectTo) window.location = data.redirectTo
|
|
25
|
-
else refresh()
|
|
26
|
-
},
|
|
27
|
-
onError: (err) => {
|
|
28
|
-
notify(typeof err === "string" ? err : err.message, { type: "error" })
|
|
29
|
-
},
|
|
30
|
-
})
|
|
11
|
+
const { mutate, isLoading } = useAction(action, {})
|
|
31
12
|
|
|
32
13
|
const PayloadBuilder = findBuilder(["action", "payload", id])
|
|
33
14
|
|
|
@@ -53,28 +34,10 @@ export function ActionButton({ icon, label, title, action, id }: AdminAction) {
|
|
|
53
34
|
}
|
|
54
35
|
|
|
55
36
|
export function BulkActionButton({ label, icon, title, action, id }: AdminAction) {
|
|
56
|
-
const resource = useResourceContext()
|
|
57
37
|
const { selectedIds } = useListContext()
|
|
38
|
+
const { mutate, isLoading } = useAction(action)
|
|
58
39
|
const [payloadActive, setPayloadActive] = useState(false)
|
|
59
40
|
|
|
60
|
-
const refresh = useRefresh(),
|
|
61
|
-
unselectAll = useUnselectAll(resource),
|
|
62
|
-
notify = useNotify()
|
|
63
|
-
|
|
64
|
-
const { mutate, isLoading } = useAction(action, {
|
|
65
|
-
onSuccess: ({ data }) => {
|
|
66
|
-
if (data && data.message) notify(data.message, { type: "success" })
|
|
67
|
-
if (data && data.redirectTo) window.location = data.redirectTo
|
|
68
|
-
else {
|
|
69
|
-
unselectAll()
|
|
70
|
-
refresh()
|
|
71
|
-
}
|
|
72
|
-
},
|
|
73
|
-
onError: (err) => {
|
|
74
|
-
notify(typeof err === "string" ? err : err.message, { type: "error" })
|
|
75
|
-
},
|
|
76
|
-
})
|
|
77
|
-
|
|
78
41
|
const PayloadBuilder = findBuilder(["action", "payload", id])
|
|
79
42
|
|
|
80
43
|
return (
|
package/src/hooks/useAction.ts
CHANGED
|
@@ -1,4 +1,10 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import {
|
|
2
|
+
useDataProvider,
|
|
3
|
+
useNotify,
|
|
4
|
+
useRefresh,
|
|
5
|
+
useResourceContext,
|
|
6
|
+
useUnselectAll,
|
|
7
|
+
} from "react-admin"
|
|
2
8
|
import { UseMutationOptions, useMutation } from "react-query"
|
|
3
9
|
import { MuffinDataprovider, TActionProps } from "../dataprovider"
|
|
4
10
|
|
|
@@ -6,10 +12,23 @@ export function useAction(
|
|
|
6
12
|
action: string,
|
|
7
13
|
options?: UseMutationOptions<{ data: any }, any, TActionProps>
|
|
8
14
|
) {
|
|
15
|
+
const notify = useNotify()
|
|
16
|
+
const refresh = useRefresh()
|
|
9
17
|
const resource = useResourceContext()
|
|
18
|
+
const unselectAll = useUnselectAll(resource)
|
|
10
19
|
const dataProvider = useDataProvider() as ReturnType<typeof MuffinDataprovider>
|
|
11
|
-
return useMutation(
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
20
|
+
return useMutation((params: TActionProps) => dataProvider.runAction(resource, action, params), {
|
|
21
|
+
onSuccess: ({ data }) => {
|
|
22
|
+
if (data && data.message) notify(data.message, { type: "success" })
|
|
23
|
+
if (data && data.redirectTo) window.location = data.redirectTo
|
|
24
|
+
else {
|
|
25
|
+
unselectAll()
|
|
26
|
+
refresh()
|
|
27
|
+
}
|
|
28
|
+
},
|
|
29
|
+
onError: (err) => {
|
|
30
|
+
notify(typeof err === "string" ? err : err.message, { type: "error" })
|
|
31
|
+
},
|
|
32
|
+
...options,
|
|
33
|
+
})
|
|
15
34
|
}
|