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