muffin-admin 3.6.1 → 3.6.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": "3.6.1",
3
+ "version": "3.6.3",
4
4
  "description": "NPM package for https://github.com/klen/muffin-admin",
5
5
  "files": [
6
6
  "src",
@@ -7,7 +7,7 @@ import {
7
7
  useTranslate,
8
8
  } from "react-admin"
9
9
 
10
- import { useMemo, useState } from "react"
10
+ import { useState } from "react"
11
11
  import { buildRA } from "../buildRA"
12
12
  import { AdminModal, PayloadButtons, useConfirmation } from "../common"
13
13
  import { useAction } from "../hooks/useAction"
@@ -20,18 +20,12 @@ export type ActionPayloadProps = {
20
20
  onHandle: (payload: any) => void
21
21
  }
22
22
 
23
- export function ActionButton({ icon, label, title, action, id, confirm, schema }: AdminAction) {
23
+ export function ActionButton({ action, confirm, ...props }: AdminAction) {
24
24
  const translate = useTranslate()
25
25
  const record = useRecordContext()
26
- const resource = useResourceContext()
27
26
  const confirmation = useConfirmation()
28
27
  const { mutate, isPending } = useAction(action)
29
28
 
30
- const [payloadActive, setPayloadActive] = useState(false)
31
-
32
- const PayloadBuilder =
33
- findBuilder(["action", "payload", id, resource]) || (schema && CommonPayload)
34
-
35
29
  const confirmMessage =
36
30
  typeof confirm === "string" ? translate(confirm) : "Do you confirm this action?"
37
31
 
@@ -40,75 +34,24 @@ export function ActionButton({ icon, label, title, action, id, confirm, schema }
40
34
  if (process) await mutate({ record, payload })
41
35
  }
42
36
 
43
- return (
44
- <>
45
- <Button
46
- label={label}
47
- title={title}
48
- onClick={() => {
49
- if (PayloadBuilder) setPayloadActive(true)
50
- else onHandle()
51
- }}
52
- disabled={isPending}
53
- >
54
- {buildIcon(icon)}
55
- </Button>
56
- {PayloadBuilder && (
57
- <PayloadBuilder
58
- active={payloadActive}
59
- onHandle={onHandle}
60
- onClose={() => setPayloadActive(false)}
61
- title={title || label}
62
- schema={schema}
63
- />
64
- )}
65
- </>
66
- )
37
+ return <ActionButtonBase {...props} isPending={isPending} onHandle={onHandle} />
67
38
  }
68
39
 
69
- export function BulkActionButton({ label, icon, title, action, id, confirm }: AdminAction) {
40
+ export function BulkActionButton({ action, confirm, ...props }: AdminAction) {
70
41
  const translate = useTranslate()
71
42
  const { selectedIds } = useListContext()
72
43
  const { mutate, isPending } = useAction(action)
73
44
  const confirmation = useConfirmation()
74
- const resource = useResourceContext()
75
- const [payloadActive, setPayloadActive] = useState(false)
76
- const [payloadResolver, setPayloadResolver] = useState<(data: any) => void>()
77
45
 
78
- const PayloadBuilder = findBuilder(["action", "payload", id, resource])
79
- const buildPayload = useMemo(() => {
80
- if (PayloadBuilder) {
81
- return new Promise((resolve) => setPayloadResolver(() => resolve))
82
- }
83
- return Promise.resolve()
84
- }, [PayloadBuilder])
85
46
  const confirmMessage =
86
47
  typeof confirm === "string" ? translate(confirm) : "Do you confirm this action?"
87
48
 
88
- return (
89
- <>
90
- <Button
91
- label={label}
92
- title={title}
93
- onClick={async () => {
94
- if (PayloadBuilder) setPayloadActive(true)
95
- const payload = await buildPayload
96
- const process = confirm ? await confirmation.confirm({ message: confirmMessage }) : true
97
- if (process) await mutate({ ids: selectedIds, payload })
98
- }}
99
- disabled={isPending}
100
- >
101
- {buildIcon(icon)}
102
- </Button>
103
- {PayloadBuilder && (
104
- <PayloadBuilder
105
- active={payloadActive}
106
- onHandle={payloadResolver}
107
- onClose={() => setPayloadActive(false)}
108
- />
109
- )}
110
- </>
111
- )
49
+ const onHandle = async (payload?) => {
50
+ const process = confirm ? await confirmation.confirm({ message: confirmMessage }) : true
51
+ if (process) await mutate({ ids: selectedIds, payload })
52
+ }
53
+
54
+ return <ActionButtonBase {...props} onHandle={onHandle} isPending={isPending} />
112
55
  }
113
56
 
114
57
  export function CommonPayload({ active, onClose, onHandle, schema, title }) {
@@ -133,3 +76,47 @@ export function CommonPayload({ active, onClose, onHandle, schema, title }) {
133
76
  </AdminModal>
134
77
  )
135
78
  }
79
+
80
+ function ActionButtonBase({
81
+ label,
82
+ title,
83
+ onHandle,
84
+ isPending,
85
+ icon,
86
+ schema,
87
+ id,
88
+ }: Omit<AdminAction, "action"> & {
89
+ isPending?: boolean
90
+ onHandle: (payload?: any) => void
91
+ }) {
92
+ const resource = useResourceContext()
93
+ const [payloadActive, setPayloadActive] = useState(false)
94
+
95
+ const PayloadBuilder =
96
+ findBuilder(["action", "payload", id, resource]) || (schema && CommonPayload)
97
+
98
+ return (
99
+ <>
100
+ <Button
101
+ label={label}
102
+ title={title}
103
+ onClick={() => {
104
+ if (PayloadBuilder) setPayloadActive(true)
105
+ else onHandle()
106
+ }}
107
+ disabled={isPending}
108
+ >
109
+ {buildIcon(icon)}
110
+ </Button>
111
+ {PayloadBuilder && (
112
+ <PayloadBuilder
113
+ active={payloadActive}
114
+ onHandle={onHandle}
115
+ onClose={() => setPayloadActive(false)}
116
+ title={title || label}
117
+ schema={schema}
118
+ />
119
+ )}
120
+ </>
121
+ )
122
+ }