muffin-admin 2.4.0 → 2.5.1
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.js +29 -23
- package/dist/dataprovider.d.ts +6 -1
- package/dist/hooks/index.d.ts +1 -0
- package/dist/hooks/index.js +1 -0
- package/dist/hooks/useAction.d.ts +1 -1
- package/dist/hooks/useAction.js +3 -2
- package/dist/index.d.ts +1 -0
- package/dist/index.js +1 -0
- package/dist/inputs/FKInput.js +1 -1
- package/dist/tsconfig.tsbuildinfo +1 -1
- package/package.json +13 -12
- package/src/buttons/ActionButton.tsx +46 -26
- package/src/dataprovider.ts +6 -1
- package/src/hooks/index.ts +1 -0
- package/src/hooks/useAction.ts +5 -3
- package/src/index.tsx +1 -0
- package/src/inputs/FKInput.tsx +1 -1
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "muffin-admin",
|
|
3
|
-
"version": "2.
|
|
3
|
+
"version": "2.5.1",
|
|
4
4
|
"description": "NPM package for https://github.com/klen/muffin-admin",
|
|
5
5
|
"files": [
|
|
6
6
|
"src",
|
|
@@ -27,23 +27,24 @@
|
|
|
27
27
|
"react": "^18.3.1",
|
|
28
28
|
"react-admin": "^4.16.18",
|
|
29
29
|
"react-dom": "^18.3.1",
|
|
30
|
-
"validator": "^13.
|
|
30
|
+
"validator": "^13.12.0"
|
|
31
31
|
},
|
|
32
32
|
"devDependencies": {
|
|
33
33
|
"@types/node": "20.14.2",
|
|
34
34
|
"@types/react": "18.3.3",
|
|
35
35
|
"@types/react-dom": "18.3.0",
|
|
36
36
|
"@types/react-window": "^1.8.8",
|
|
37
|
-
"@typescript-eslint/eslint-plugin": "^
|
|
38
|
-
"@typescript-eslint/parser": "^
|
|
39
|
-
"dotenv": "^16.
|
|
40
|
-
"eslint": "8
|
|
41
|
-
"eslint-config-prettier": "^9.
|
|
42
|
-
"eslint-plugin-prettier": "^5.
|
|
43
|
-
"
|
|
44
|
-
"
|
|
45
|
-
"
|
|
46
|
-
"prettier
|
|
37
|
+
"@typescript-eslint/eslint-plugin": "^7.12.0",
|
|
38
|
+
"@typescript-eslint/parser": "^7.12.0",
|
|
39
|
+
"dotenv": "^16.4.5",
|
|
40
|
+
"eslint": "^8",
|
|
41
|
+
"eslint-config-prettier": "^9.1.0",
|
|
42
|
+
"eslint-plugin-prettier": "^5.1.3",
|
|
43
|
+
"eslint-plugin-react": "^7.34.2",
|
|
44
|
+
"html-webpack-plugin": "^5.6.0",
|
|
45
|
+
"husky": "^9.0.11",
|
|
46
|
+
"prettier": "^3.2",
|
|
47
|
+
"prettier-plugin-organize-imports": "^3.2",
|
|
47
48
|
"ts-loader": "^9.5.1",
|
|
48
49
|
"typescript": "^5.4.5",
|
|
49
50
|
"webpack": "^5.91.0",
|
|
@@ -9,8 +9,9 @@ import {
|
|
|
9
9
|
useUnselectAll,
|
|
10
10
|
} from "react-admin"
|
|
11
11
|
|
|
12
|
+
import { useState } from "react"
|
|
12
13
|
import { useAction } from "../hooks/useAction"
|
|
13
|
-
import { buildIcon } from "../utils"
|
|
14
|
+
import { buildIcon, findBuilder } from "../utils"
|
|
14
15
|
|
|
15
16
|
export function BulkActionButton({ label, icon, title, action }) {
|
|
16
17
|
const resource = useResourceContext()
|
|
@@ -19,10 +20,10 @@ export function BulkActionButton({ label, icon, title, action }) {
|
|
|
19
20
|
unselectAll = useUnselectAll(resource),
|
|
20
21
|
notify = useNotify()
|
|
21
22
|
|
|
22
|
-
const
|
|
23
|
+
const { mutate, isLoading } = useAction(action)
|
|
23
24
|
|
|
24
25
|
const onClick = () => {
|
|
25
|
-
|
|
26
|
+
mutate(
|
|
26
27
|
{
|
|
27
28
|
action,
|
|
28
29
|
ids: selectedIds,
|
|
@@ -41,42 +42,61 @@ export function BulkActionButton({ label, icon, title, action }) {
|
|
|
41
42
|
}
|
|
42
43
|
|
|
43
44
|
return (
|
|
44
|
-
<Button label={label} title={title} onClick={onClick} disabled={
|
|
45
|
+
<Button label={label} title={title} onClick={onClick} disabled={isLoading}>
|
|
45
46
|
{buildIcon(icon)}
|
|
46
47
|
</Button>
|
|
47
48
|
)
|
|
48
49
|
}
|
|
49
50
|
|
|
50
51
|
export function ActionButton(props) {
|
|
51
|
-
const { icon, label, title,
|
|
52
|
+
const { icon, label, title, action } = props
|
|
53
|
+
|
|
54
|
+
const [payload, setPayload] = useState(null)
|
|
55
|
+
const [payloadActive, setPayloadActive] = useState(false)
|
|
56
|
+
|
|
52
57
|
const record = useRecordContext()
|
|
53
|
-
const {
|
|
58
|
+
const actionData = { record, payload }
|
|
59
|
+
const { mutate, isLoading } = useAction(action)
|
|
60
|
+
|
|
61
|
+
const PayloadBuilder = findBuilder(["action", "payload", action])
|
|
54
62
|
|
|
55
63
|
const refresh = useRefresh(),
|
|
56
64
|
notify = useNotify(),
|
|
57
65
|
Icon = icons[icon]
|
|
58
66
|
|
|
59
|
-
const
|
|
60
|
-
mutate(
|
|
61
|
-
{
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
}
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
type: "error",
|
|
71
|
-
})
|
|
72
|
-
},
|
|
73
|
-
}
|
|
74
|
-
)
|
|
75
|
-
}
|
|
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
|
+
})
|
|
76
78
|
|
|
77
79
|
return (
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
80
|
+
<>
|
|
81
|
+
<Button
|
|
82
|
+
label={label}
|
|
83
|
+
title={title}
|
|
84
|
+
onClick={PayloadBuilder ? () => setPayloadActive(true) : process}
|
|
85
|
+
disabled={isLoading}
|
|
86
|
+
>
|
|
87
|
+
{Icon && <Icon />}
|
|
88
|
+
</Button>
|
|
89
|
+
{PayloadBuilder && (
|
|
90
|
+
<PayloadBuilder
|
|
91
|
+
active={payloadActive}
|
|
92
|
+
onChange={(payload) => {
|
|
93
|
+
setPayloadActive(false)
|
|
94
|
+
setPayload(payload)
|
|
95
|
+
process()
|
|
96
|
+
}}
|
|
97
|
+
onClose={() => setPayloadActive(false)}
|
|
98
|
+
/>
|
|
99
|
+
)}
|
|
100
|
+
</>
|
|
81
101
|
)
|
|
82
102
|
}
|
package/src/dataprovider.ts
CHANGED
|
@@ -75,7 +75,7 @@ export function MuffinDataprovider({ apiUrl }: AdminOpts) {
|
|
|
75
75
|
return await methods.getList(resource, { filter, ...opts })
|
|
76
76
|
},
|
|
77
77
|
|
|
78
|
-
runAction: async (resource, action, props) => {
|
|
78
|
+
runAction: async (resource: string, action: string, props: TActionProps) => {
|
|
79
79
|
const { payload, ids, record } = props
|
|
80
80
|
action = action.replace(/^\/+/, "")
|
|
81
81
|
if (record) {
|
|
@@ -92,4 +92,9 @@ export function MuffinDataprovider({ apiUrl }: AdminOpts) {
|
|
|
92
92
|
return methods
|
|
93
93
|
}
|
|
94
94
|
|
|
95
|
+
export type TActionProps = {
|
|
96
|
+
payload?: any
|
|
97
|
+
ids?: string[]
|
|
98
|
+
record: any
|
|
99
|
+
}
|
|
95
100
|
setupAdmin(["dataprovider"], MuffinDataprovider)
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from './useAction'
|
package/src/hooks/useAction.ts
CHANGED
|
@@ -1,9 +1,11 @@
|
|
|
1
|
-
import { useDataProvider } from "react-admin"
|
|
1
|
+
import { useDataProvider, useResourceContext } from "react-admin"
|
|
2
2
|
import { useMutation } from "react-query"
|
|
3
|
+
import { TActionProps } from "../dataprovider"
|
|
3
4
|
|
|
4
|
-
export function useAction(
|
|
5
|
+
export function useAction(action) {
|
|
5
6
|
const dataProvider = useDataProvider()
|
|
6
|
-
|
|
7
|
+
const resource = useResourceContext()
|
|
8
|
+
return useMutation<{ data: any }, { message: string } | string, any>((params: TActionProps) =>
|
|
7
9
|
dataProvider.runAction(resource, action, params)
|
|
8
10
|
)
|
|
9
11
|
}
|
package/src/index.tsx
CHANGED
package/src/inputs/FKInput.tsx
CHANGED
|
@@ -16,7 +16,7 @@ export function FKInput({ refKey, refSource, fullWidth, reference, source, ...pr
|
|
|
16
16
|
}
|
|
17
17
|
|
|
18
18
|
return (
|
|
19
|
-
<ReferenceInput reference={reference} source={source}
|
|
19
|
+
<ReferenceInput reference={reference} source={source}>
|
|
20
20
|
<AutocompleteInput source={refKey} optionText={renderText} fullWidth={fullWidth} {...props} />
|
|
21
21
|
</ReferenceInput>
|
|
22
22
|
)
|