muffin-admin 3.5.0 → 3.6.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/dist/types.d.ts CHANGED
@@ -2,13 +2,14 @@ export type TProps = {
2
2
  [key: string]: any;
3
3
  };
4
4
  export type AdminAction = {
5
- view: string[];
5
+ view: ("show" | "edit" | "list" | "bulk")[];
6
6
  id: string;
7
7
  label: string;
8
8
  title: string;
9
9
  icon: string;
10
10
  action: string;
11
11
  confirm?: string | false;
12
+ schema?: AdminInput[];
12
13
  };
13
14
  export type AdminField = [string, {
14
15
  source: string;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "muffin-admin",
3
- "version": "3.5.0",
3
+ "version": "3.6.2",
4
4
  "description": "NPM package for https://github.com/klen/muffin-admin",
5
5
  "files": [
6
6
  "src",
@@ -14,7 +14,7 @@ import {
14
14
  TopToolbar,
15
15
  } from "react-admin"
16
16
  import { buildRA } from "./buildRA"
17
- import { BulkActionButton } from "./buttons"
17
+ import { ActionButton, BulkActionButton } from "./buttons"
18
18
  import { useMuffinResourceOpts } from "./hooks"
19
19
  import { buildAdmin, findBuilder, setupAdmin } from "./utils"
20
20
 
@@ -72,12 +72,15 @@ setupAdmin(["list-grid-buttons"], MuffinListGridButtons)
72
72
 
73
73
  function MuffinListToolbar() {
74
74
  const {
75
+ actions: baseActions = [],
75
76
  list: { create },
76
77
  } = useMuffinResourceOpts()
78
+ const actions = baseActions.filter((a) => a.view.includes("list"))
77
79
  return (
78
80
  <TopToolbar>
79
81
  <SelectColumnsButton />
80
82
  <FilterButton />
83
+ {actions.length && actions.map((props) => <ActionButton key={props.id} {...props} />)}
81
84
  {create && <CreateButton />}
82
85
  <ExportButton />
83
86
  </TopToolbar>
@@ -90,7 +93,7 @@ function MuffinListBulkActions() {
90
93
  actions: baseActions = [],
91
94
  list: { remove },
92
95
  } = useMuffinResourceOpts()
93
- const actions = baseActions.filter((a) => a.view.includes("list"))
96
+ const actions = baseActions.filter((a) => a.view.includes("bulk"))
94
97
  return (
95
98
  <>
96
99
  {actions.map((props) => (
@@ -1,13 +1,15 @@
1
1
  import {
2
2
  Button,
3
+ Form,
3
4
  useListContext,
4
5
  useRecordContext,
5
6
  useResourceContext,
6
7
  useTranslate,
7
8
  } from "react-admin"
8
9
 
9
- import { useMemo, useState } from "react"
10
- import { useConfirmation } from "../common"
10
+ import { useState } from "react"
11
+ import { buildRA } from "../buildRA"
12
+ import { AdminModal, PayloadButtons, useConfirmation } from "../common"
11
13
  import { useAction } from "../hooks/useAction"
12
14
  import { AdminAction } from "../types"
13
15
  import { buildIcon, findBuilder } from "../utils"
@@ -18,81 +20,89 @@ export type ActionPayloadProps = {
18
20
  onHandle: (payload: any) => void
19
21
  }
20
22
 
21
- export function ActionButton({ icon, label, title, action, id, confirm }: AdminAction) {
23
+ export function ActionButton({ action, confirm, ...props }: AdminAction) {
22
24
  const translate = useTranslate()
23
25
  const record = useRecordContext()
24
- const resource = useResourceContext()
25
26
  const confirmation = useConfirmation()
26
27
  const { mutate, isPending } = useAction(action)
27
28
 
28
- const [payloadActive, setPayloadActive] = useState(false)
29
- const [payloadResolver, setPayloadResolver] = useState<(data: any) => void>()
29
+ const confirmMessage =
30
+ typeof confirm === "string" ? translate(confirm) : "Do you confirm this action?"
31
+
32
+ const onHandle = async (payload?) => {
33
+ const process = confirm ? await confirmation.confirm({ message: confirmMessage }) : true
34
+ if (process) await mutate({ record, payload })
35
+ }
36
+
37
+ return <ActionButtonBase {...props} isPending={isPending} onHandle={onHandle} />
38
+ }
39
+
40
+ export function BulkActionButton({ action, confirm, ...props }: AdminAction) {
41
+ const translate = useTranslate()
42
+ const { selectedIds } = useListContext()
43
+ const { mutate, isPending } = useAction(action)
44
+ const confirmation = useConfirmation()
30
45
 
31
- const PayloadBuilder = findBuilder(["action", "payload", id, resource])
32
- const buildPayload = useMemo(() => {
33
- if (PayloadBuilder) {
34
- return new Promise((resolve) => setPayloadResolver(() => resolve))
35
- }
36
- return Promise.resolve()
37
- }, [PayloadBuilder])
38
46
  const confirmMessage =
39
47
  typeof confirm === "string" ? translate(confirm) : "Do you confirm this action?"
40
48
 
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} />
55
+ }
56
+
57
+ export function CommonPayload({ active, onClose, onHandle, schema, title }) {
58
+ const inputs = buildRA(schema)
59
+ const translate = useTranslate()
41
60
  return (
42
- <>
43
- <Button
44
- label={label}
45
- title={title}
46
- onClick={async () => {
47
- if (PayloadBuilder) setPayloadActive(true)
48
- const payload = await buildPayload
49
- const process = confirm ? await confirmation.confirm({ message: confirmMessage }) : true
50
- if (process) await mutate({ record, payload })
61
+ <AdminModal
62
+ maxWidth="sm"
63
+ open={active}
64
+ onClose={onClose}
65
+ title={translate(title, { _: title })}
66
+ >
67
+ <Form
68
+ onSubmit={(data) => {
69
+ onClose()
70
+ onHandle(data)
51
71
  }}
52
- disabled={isPending}
53
72
  >
54
- {buildIcon(icon)}
55
- </Button>
56
- {PayloadBuilder && (
57
- <PayloadBuilder
58
- active={payloadActive}
59
- onHandle={payloadResolver}
60
- onClose={() => setPayloadActive(false)}
61
- />
62
- )}
63
- </>
73
+ {inputs}
74
+ <PayloadButtons onClose={onClose} />
75
+ </Form>
76
+ </AdminModal>
64
77
  )
65
78
  }
66
79
 
67
- export function BulkActionButton({ label, icon, title, action, id, confirm }: AdminAction) {
68
- const translate = useTranslate()
69
- const { selectedIds } = useListContext()
70
- const { mutate, isPending } = useAction(action)
71
- const confirmation = useConfirmation()
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
+ }) {
72
92
  const resource = useResourceContext()
73
93
  const [payloadActive, setPayloadActive] = useState(false)
74
- const [payloadResolver, setPayloadResolver] = useState<(data: any) => void>()
75
94
 
76
- const PayloadBuilder = findBuilder(["action", "payload", id, resource])
77
- const buildPayload = useMemo(() => {
78
- if (PayloadBuilder) {
79
- return new Promise((resolve) => setPayloadResolver(() => resolve))
80
- }
81
- return Promise.resolve()
82
- }, [PayloadBuilder])
83
- const confirmMessage =
84
- typeof confirm === "string" ? translate(confirm) : "Do you confirm this action?"
95
+ const PayloadBuilder =
96
+ findBuilder(["action", "payload", id, resource]) || (schema && CommonPayload)
85
97
 
86
98
  return (
87
99
  <>
88
100
  <Button
89
101
  label={label}
90
102
  title={title}
91
- onClick={async () => {
103
+ onClick={() => {
92
104
  if (PayloadBuilder) setPayloadActive(true)
93
- const payload = await buildPayload
94
- const process = confirm ? await confirmation.confirm({ message: confirmMessage }) : true
95
- if (process) await mutate({ ids: selectedIds, payload })
105
+ else onHandle()
96
106
  }}
97
107
  disabled={isPending}
98
108
  >
@@ -101,8 +111,10 @@ export function BulkActionButton({ label, icon, title, action, id, confirm }: Ad
101
111
  {PayloadBuilder && (
102
112
  <PayloadBuilder
103
113
  active={payloadActive}
104
- onHandle={payloadResolver}
114
+ onHandle={onHandle}
105
115
  onClose={() => setPayloadActive(false)}
116
+ title={title || label}
117
+ schema={schema}
106
118
  />
107
119
  )}
108
120
  </>
@@ -0,0 +1,19 @@
1
+ import { Stack } from "@mui/material"
2
+ import { Button } from "react-admin"
3
+ import { useFormContext } from "react-hook-form"
4
+
5
+ export function PayloadButtons({ onClose }) {
6
+ const { formState } = useFormContext()
7
+ return (
8
+ <Stack direction="row" alignItems="center" justifyContent="flex-end" spacing={1}>
9
+ <Button
10
+ type="submit"
11
+ label="ra.action.confirm"
12
+ variant="contained"
13
+ disabled={!formState.isValid}
14
+ size="medium"
15
+ />
16
+ <Button label="ra.action.cancel" variant="outlined" size="medium" onClick={onClose} />
17
+ </Stack>
18
+ )
19
+ }
@@ -1,2 +1,3 @@
1
1
  export * from "./AdminModal"
2
2
  export * from "./ConfirmationProvider"
3
+ export * from "./PayloadButtons"
package/src/types.ts CHANGED
@@ -3,13 +3,14 @@ export type TProps = {
3
3
  }
4
4
 
5
5
  export type AdminAction = {
6
- view: string[]
6
+ view: ("show" | "edit" | "list" | "bulk")[]
7
7
  id: string
8
8
  label: string
9
9
  title: string
10
10
  icon: string
11
11
  action: string
12
12
  confirm?: string | false
13
+ schema?: AdminInput[]
13
14
  }
14
15
 
15
16
  export type AdminField = [string, { source: string }]
@@ -34,11 +35,11 @@ export type AdminResourceProps = {
34
35
  actions: AdminAction[]
35
36
  create: AdminInput[] | false
36
37
  edit:
37
- | {
38
- inputs: AdminInput[]
39
- remove?: boolean
40
- }
41
- | false
38
+ | {
39
+ inputs: AdminInput[]
40
+ remove?: boolean
41
+ }
42
+ | false
42
43
  list: {
43
44
  limit: number
44
45
  limitMax: number