muffin-admin 3.1.1 → 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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "muffin-admin",
3
- "version": "3.1.1",
3
+ "version": "3.1.2",
4
4
  "description": "NPM package for https://github.com/klen/muffin-admin",
5
5
  "files": [
6
6
  "src",
@@ -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 BulkActionButton({ label, icon, title, action, id }: AdminAction) {
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) : process())}
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) => process(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 ActionButton({ icon, label, title, action, id }: AdminAction) {
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 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
- },
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) : process())}
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) => process(payload)}
93
+ onHandle={(payload) => mutate({ ids: selectedIds, payload })}
108
94
  onClose={() => setPayloadActive(false)}
109
95
  />
110
96
  )}
@@ -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: any
97
+ record?: T
98
98
  }
99
99
  setupAdmin(["dataprovider"], MuffinDataprovider)
@@ -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(action) {
6
- const dataProvider = useDataProvider()
5
+ export function useAction(
6
+ action: string,
7
+ options?: UseMutationOptions<{ data: any }, any, TActionProps>
8
+ ) {
7
9
  const resource = useResourceContext()
8
- return useMutation<{ data: any }, { message: string } | string, any>((params: TActionProps) =>
9
- dataProvider.runAction(resource, action, params)
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
  }