muffin-admin 2.5.1 → 2.5.3

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,6 +1,6 @@
1
1
  {
2
2
  "name": "muffin-admin",
3
- "version": "2.5.1",
3
+ "version": "2.5.3",
4
4
  "description": "NPM package for https://github.com/klen/muffin-admin",
5
5
  "files": [
6
6
  "src",
@@ -1,4 +1,3 @@
1
- import * as icons from "@mui/icons-material"
2
1
  import {
3
2
  Button,
4
3
  useListContext,
@@ -16,65 +15,80 @@ import { buildIcon, findBuilder } from "../utils"
16
15
  export function BulkActionButton({ label, icon, title, action }) {
17
16
  const resource = useResourceContext()
18
17
  const { selectedIds } = useListContext()
18
+ const [payloadActive, setPayloadActive] = useState(false)
19
+
20
+ const PayloadBuilder = findBuilder(["action", "payload", action])
21
+
19
22
  const refresh = useRefresh(),
20
23
  unselectAll = useUnselectAll(resource),
21
24
  notify = useNotify()
22
25
 
23
26
  const { mutate, isLoading } = useAction(action)
24
27
 
25
- const onClick = () => {
28
+ const process = (payload) =>
26
29
  mutate(
27
- {
28
- action,
29
- ids: selectedIds,
30
- },
30
+ { ids: selectedIds, payload },
31
31
  {
32
32
  onSuccess: ({ data }) => {
33
33
  if (data && data.message) notify(data.message, { type: "success" })
34
- refresh()
35
- unselectAll()
34
+ if (data && data.redirectTo) window.location = data.redirectTo
35
+ else {
36
+ unselectAll()
37
+ refresh()
38
+ }
36
39
  },
37
40
  onError: (err) => {
38
41
  notify(typeof err === "string" ? err : err.message, { type: "error" })
39
42
  },
40
43
  }
41
44
  )
42
- }
43
45
 
44
46
  return (
45
- <Button label={label} title={title} onClick={onClick} disabled={isLoading}>
46
- {buildIcon(icon)}
47
- </Button>
47
+ <>
48
+ <Button
49
+ label={label}
50
+ title={title}
51
+ onClick={PayloadBuilder ? () => setPayloadActive(true) : process}
52
+ disabled={isLoading}
53
+ >
54
+ {buildIcon(icon)}
55
+ </Button>
56
+ {PayloadBuilder && (
57
+ <PayloadBuilder
58
+ active={payloadActive}
59
+ onHandle={(payload) => process(payload)}
60
+ onClose={() => setPayloadActive(false)}
61
+ />
62
+ )}
63
+ </>
48
64
  )
49
65
  }
50
66
 
51
- export function ActionButton(props) {
52
- const { icon, label, title, action } = props
53
-
54
- const [payload, setPayload] = useState(null)
67
+ export function ActionButton({ icon, label, title, action }) {
55
68
  const [payloadActive, setPayloadActive] = useState(false)
56
69
 
57
70
  const record = useRecordContext()
58
- const actionData = { record, payload }
59
71
  const { mutate, isLoading } = useAction(action)
60
72
 
61
73
  const PayloadBuilder = findBuilder(["action", "payload", action])
62
74
 
63
75
  const refresh = useRefresh(),
64
- notify = useNotify(),
65
- Icon = icons[icon]
76
+ notify = useNotify()
66
77
 
67
- const process = () =>
68
- mutate(actionData, {
69
- onSuccess: ({ data }) => {
70
- if (data && data.message) notify(data.message, { type: "success" })
71
- if (data && data.redirectTo) window.location = data.redirectTo
72
- else refresh()
73
- },
74
- onError: (err) => {
75
- notify(typeof err === "string" ? err : err.message, { type: "error" })
76
- },
77
- })
78
+ const process = (payload) =>
79
+ mutate(
80
+ { record, payload },
81
+ {
82
+ onSuccess: ({ data }) => {
83
+ if (data && data.message) notify(data.message, { type: "success" })
84
+ if (data && data.redirectTo) window.location = data.redirectTo
85
+ else refresh()
86
+ },
87
+ onError: (err) => {
88
+ notify(typeof err === "string" ? err : err.message, { type: "error" })
89
+ },
90
+ }
91
+ )
78
92
 
79
93
  return (
80
94
  <>
@@ -84,16 +98,12 @@ export function ActionButton(props) {
84
98
  onClick={PayloadBuilder ? () => setPayloadActive(true) : process}
85
99
  disabled={isLoading}
86
100
  >
87
- {Icon && <Icon />}
101
+ {buildIcon(icon)}
88
102
  </Button>
89
103
  {PayloadBuilder && (
90
104
  <PayloadBuilder
91
105
  active={payloadActive}
92
- onChange={(payload) => {
93
- setPayloadActive(false)
94
- setPayload(payload)
95
- process()
96
- }}
106
+ onHandle={(payload) => process(payload)}
97
107
  onClose={() => setPayloadActive(false)}
98
108
  />
99
109
  )}