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.
- package/dist/MuffinResourceList.js +1 -1
- package/dist/buttons/ActionButton.d.ts +1 -1
- package/dist/buttons/ActionButton.js +18 -19
- package/dist/dataprovider.d.ts +2 -2
- package/dist/hooks/useAction.d.ts +6 -4
- package/dist/hooks/useAction.js +3 -3
- package/dist/tsconfig.tsbuildinfo +1 -1
- package/package.json +1 -1
- package/src/MuffinResourceList.tsx +1 -1
- package/src/buttons/ActionButton.tsx +36 -50
- package/src/dataprovider.ts +2 -2
- package/src/hooks/useAction.ts +10 -6
package/package.json
CHANGED
|
@@ -51,7 +51,7 @@ function MuffinListDatagrid() {
|
|
|
51
51
|
return (
|
|
52
52
|
<DatagridConfigurable rowClick={show ? "show" : edit ? "edit" : false}>
|
|
53
53
|
{buildAdmin(["list-fields", name], fields)}
|
|
54
|
-
{buildAdmin(["list-grid-buttons"
|
|
54
|
+
{buildAdmin(["list-grid-buttons", name])}
|
|
55
55
|
</DatagridConfigurable>
|
|
56
56
|
)
|
|
57
57
|
}
|
|
@@ -13,43 +13,30 @@ import { useAction } from "../hooks/useAction"
|
|
|
13
13
|
import { AdminAction } from "../types"
|
|
14
14
|
import { buildIcon, findBuilder } from "../utils"
|
|
15
15
|
|
|
16
|
-
export function
|
|
17
|
-
const resource = useResourceContext()
|
|
18
|
-
const { selectedIds } = useListContext()
|
|
16
|
+
export function ActionButton({ icon, label, title, action, id }: AdminAction) {
|
|
19
17
|
const [payloadActive, setPayloadActive] = useState(false)
|
|
18
|
+
const record = useRecordContext()
|
|
19
|
+
const notify = useNotify()
|
|
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
|
+
})
|
|
20
31
|
|
|
21
32
|
const PayloadBuilder = findBuilder(["action", "payload", id])
|
|
22
33
|
|
|
23
|
-
const refresh = useRefresh(),
|
|
24
|
-
unselectAll = useUnselectAll(resource),
|
|
25
|
-
notify = useNotify()
|
|
26
|
-
|
|
27
|
-
const { mutate, isLoading } = useAction(action)
|
|
28
|
-
|
|
29
|
-
const process = (payload?) =>
|
|
30
|
-
mutate(
|
|
31
|
-
{ ids: selectedIds, payload },
|
|
32
|
-
{
|
|
33
|
-
onSuccess: ({ data }) => {
|
|
34
|
-
if (data && data.message) notify(data.message, { type: "success" })
|
|
35
|
-
if (data && data.redirectTo) window.location = data.redirectTo
|
|
36
|
-
else {
|
|
37
|
-
unselectAll()
|
|
38
|
-
refresh()
|
|
39
|
-
}
|
|
40
|
-
},
|
|
41
|
-
onError: (err) => {
|
|
42
|
-
notify(typeof err === "string" ? err : err.message, { type: "error" })
|
|
43
|
-
},
|
|
44
|
-
}
|
|
45
|
-
)
|
|
46
|
-
|
|
47
34
|
return (
|
|
48
35
|
<>
|
|
49
36
|
<Button
|
|
50
37
|
label={label}
|
|
51
38
|
title={title}
|
|
52
|
-
onClick={() => (PayloadBuilder ? setPayloadActive(true) :
|
|
39
|
+
onClick={() => (PayloadBuilder ? setPayloadActive(true) : mutate({ record }))}
|
|
53
40
|
disabled={isLoading}
|
|
54
41
|
>
|
|
55
42
|
{buildIcon(icon)}
|
|
@@ -57,7 +44,7 @@ export function BulkActionButton({ label, icon, title, action, id }: AdminAction
|
|
|
57
44
|
{PayloadBuilder && (
|
|
58
45
|
<PayloadBuilder
|
|
59
46
|
active={payloadActive}
|
|
60
|
-
onHandle={(payload) =>
|
|
47
|
+
onHandle={(payload) => mutate({ record, payload })}
|
|
61
48
|
onClose={() => setPayloadActive(false)}
|
|
62
49
|
/>
|
|
63
50
|
)}
|
|
@@ -65,38 +52,37 @@ export function BulkActionButton({ label, icon, title, action, id }: AdminAction
|
|
|
65
52
|
)
|
|
66
53
|
}
|
|
67
54
|
|
|
68
|
-
export function
|
|
55
|
+
export function BulkActionButton({ label, icon, title, action, id }: AdminAction) {
|
|
56
|
+
const resource = useResourceContext()
|
|
57
|
+
const { selectedIds } = useListContext()
|
|
69
58
|
const [payloadActive, setPayloadActive] = useState(false)
|
|
70
59
|
|
|
71
|
-
const record = useRecordContext()
|
|
72
|
-
const { mutate, isLoading } = useAction(action)
|
|
73
|
-
|
|
74
|
-
const PayloadBuilder = findBuilder(["action", "payload", id])
|
|
75
|
-
|
|
76
60
|
const refresh = useRefresh(),
|
|
61
|
+
unselectAll = useUnselectAll(resource),
|
|
77
62
|
notify = useNotify()
|
|
78
63
|
|
|
79
|
-
const
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
else refresh()
|
|
87
|
-
},
|
|
88
|
-
onError: (err) => {
|
|
89
|
-
notify(typeof err === "string" ? err : err.message, { type: "error" })
|
|
90
|
-
},
|
|
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()
|
|
91
71
|
}
|
|
92
|
-
|
|
72
|
+
},
|
|
73
|
+
onError: (err) => {
|
|
74
|
+
notify(typeof err === "string" ? err : err.message, { type: "error" })
|
|
75
|
+
},
|
|
76
|
+
})
|
|
77
|
+
|
|
78
|
+
const PayloadBuilder = findBuilder(["action", "payload", id])
|
|
93
79
|
|
|
94
80
|
return (
|
|
95
81
|
<>
|
|
96
82
|
<Button
|
|
97
83
|
label={label}
|
|
98
84
|
title={title}
|
|
99
|
-
onClick={() => (PayloadBuilder ? setPayloadActive(true) :
|
|
85
|
+
onClick={() => (PayloadBuilder ? setPayloadActive(true) : mutate({ ids: selectedIds }))}
|
|
100
86
|
disabled={isLoading}
|
|
101
87
|
>
|
|
102
88
|
{buildIcon(icon)}
|
|
@@ -104,7 +90,7 @@ export function ActionButton({ icon, label, title, action, id }: AdminAction) {
|
|
|
104
90
|
{PayloadBuilder && (
|
|
105
91
|
<PayloadBuilder
|
|
106
92
|
active={payloadActive}
|
|
107
|
-
onHandle={(payload) =>
|
|
93
|
+
onHandle={(payload) => mutate({ ids: selectedIds, payload })}
|
|
108
94
|
onClose={() => setPayloadActive(false)}
|
|
109
95
|
/>
|
|
110
96
|
)}
|
package/src/dataprovider.ts
CHANGED
|
@@ -91,9 +91,9 @@ export function MuffinDataprovider(apiUrl: string) {
|
|
|
91
91
|
return methods
|
|
92
92
|
}
|
|
93
93
|
|
|
94
|
-
export type TActionProps = {
|
|
94
|
+
export type TActionProps<T = any> = {
|
|
95
95
|
payload?: any
|
|
96
96
|
ids?: string[]
|
|
97
|
-
record
|
|
97
|
+
record?: T
|
|
98
98
|
}
|
|
99
99
|
setupAdmin(["dataprovider"], MuffinDataprovider)
|
package/src/hooks/useAction.ts
CHANGED
|
@@ -1,11 +1,15 @@
|
|
|
1
1
|
import { useDataProvider, useResourceContext } from "react-admin"
|
|
2
|
-
import { useMutation } from "react-query"
|
|
3
|
-
import { TActionProps } from "../dataprovider"
|
|
2
|
+
import { UseMutationOptions, useMutation } from "react-query"
|
|
3
|
+
import { MuffinDataprovider, TActionProps } from "../dataprovider"
|
|
4
4
|
|
|
5
|
-
export function useAction(
|
|
6
|
-
|
|
5
|
+
export function useAction(
|
|
6
|
+
action: string,
|
|
7
|
+
options?: UseMutationOptions<{ data: any }, any, TActionProps>
|
|
8
|
+
) {
|
|
7
9
|
const resource = useResourceContext()
|
|
8
|
-
|
|
9
|
-
|
|
10
|
+
const dataProvider = useDataProvider() as ReturnType<typeof MuffinDataprovider>
|
|
11
|
+
return useMutation(
|
|
12
|
+
(params: TActionProps) => dataProvider.runAction(resource, action, params),
|
|
13
|
+
options
|
|
10
14
|
)
|
|
11
15
|
}
|