muffin-admin 3.19.0 → 4.0.0

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
@@ -1,6 +1,9 @@
1
+ export type TID = string | number;
1
2
  export type TProps = {
2
3
  [key: string]: any;
3
4
  };
5
+ export type TAdminType = "action-form" | "admin" | "appbar" | "auth-provider" | "auth-storage-get" | "auth-storage-set" | "create" | "create-inputs" | "create-toolbar" | "dashboard" | "dashboard-actions" | "dashboard-content" | "data-provider" | "edit" | "edit-actions" | "edit-inputs" | "edit-toolbar" | "layout" | "list" | "list-toolbar" | "list-grid" | "list-grid-buttons" | "list-filters" | "list-fields" | "list-bulk-actions" | "locale" | "loginpage" | "resource" | "show" | "show-actions" | "show-toolbar" | "show-fields" | "show-links";
6
+ export type TAdminPath = [type: TAdminType, ...ids: string[]];
4
7
  export type AdminAction = {
5
8
  view?: ("show" | "edit" | "list" | "bulk")[];
6
9
  help?: string;
package/dist/utils.d.ts CHANGED
@@ -1,6 +1,7 @@
1
- export declare function setupAdmin<T>(adminType: string[], fc: (props: any) => T): void;
2
- export declare function buildAdmin(adminType: string[], props?: {}): any;
3
- export declare function findBuilder(adminType: string[]): (props: any) => any;
1
+ import { TAdminPath } from "./types";
2
+ export declare function setupAdmin<T>(adminType: TAdminPath, fc: (props: any) => T): void;
3
+ export declare function buildAdmin(adminType: TAdminPath, props?: {}): any;
4
+ export declare function findBuilder(adminType: TAdminPath): (props: any) => any;
4
5
  export type APIParams = {
5
6
  method?: string;
6
7
  query?: {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "muffin-admin",
3
- "version": "3.19.0",
3
+ "version": "4.0.0",
4
4
  "description": "NPM package for https://github.com/klen/muffin-admin",
5
5
  "files": [
6
6
  "src",
@@ -31,6 +31,7 @@
31
31
  "validator": "^13.12.0"
32
32
  },
33
33
  "devDependencies": {
34
+ "@types/lodash": "^4.17.10",
34
35
  "@types/node": "20.14.2",
35
36
  "@types/react": "18.3.3",
36
37
  "@types/react-dom": "18.3.0",
@@ -37,8 +37,8 @@ export function MuffinAdmin(props: AdminProps) {
37
37
 
38
38
  const {
39
39
  basename,
40
- authProvider = findBuilder(["authprovider"])(auth),
41
- dataProvider = findBuilder(["dataprovider"])(apiUrl),
40
+ authProvider = findBuilder(["auth-provider"])(auth),
41
+ dataProvider = findBuilder(["data-provider"])(apiUrl),
42
42
  catchAll,
43
43
  dashboard = findBuilder(["dashboard"]),
44
44
  disableTelemetry,
@@ -65,9 +65,9 @@ function DashboardCard({ title, value }: AdminDashboardBlock) {
65
65
  const AdminTableView = ({ src }) => (
66
66
  <Table>
67
67
  <TableBody>
68
- {src.map((row, idx) => (
68
+ {src.map((row: any, idx: number) => (
69
69
  <TableRow key={idx} hover>
70
- {row.map((cell, idx) => (
70
+ {row.map((cell: any, idx: number) => (
71
71
  <TableCell key={idx}>{cell}</TableCell>
72
72
  ))}
73
73
  </TableRow>
@@ -89,4 +89,4 @@ export function MuffinAuthProvider(props: AdminOpts["auth"]) {
89
89
  }
90
90
  }
91
91
 
92
- setupAdmin(["authprovider"], MuffinAuthProvider)
92
+ setupAdmin(["auth-provider"], MuffinAuthProvider)
package/src/buildRA.tsx CHANGED
@@ -81,7 +81,7 @@ const UI: Record<string, JSX.ElementType> = {
81
81
  FKInput,
82
82
  }
83
83
 
84
- export function buildRAComponent(name, props) {
84
+ export function buildRAComponent(name: string, props: any) {
85
85
  const Item = UI[name]
86
86
  if (!Item) return null
87
87
 
@@ -99,6 +99,6 @@ export function buildRA(items: [string, Record<string, any>][]) {
99
99
  return items.map((item) => buildRAComponent(item[0], item[1]))
100
100
  }
101
101
 
102
- export function registerUI(name, component) {
102
+ export function registerUI(name: string, component: JSX.ElementType) {
103
103
  UI[name] = component
104
104
  }
@@ -76,7 +76,7 @@ export function BulkActionButton({ paths, confirm, ...props }: AdminAction) {
76
76
  const confirmMessage =
77
77
  typeof confirm === "string" ? translate(confirm) : "Do you confirm this action?"
78
78
 
79
- const onHandle = async (payload?) => {
79
+ const onHandle = async (payload?: any) => {
80
80
  const process = confirm ? await confirmation.confirm({ message: confirmMessage }) : true
81
81
  if (process) await mutate({ ids: selectedIds, payload })
82
82
  }
@@ -100,32 +100,32 @@ function ActionButtonBase({
100
100
  const resource = useResourceContext()
101
101
  const [payloadActive, setPayloadActive] = useState(false)
102
102
 
103
- const PayloadBuilder =
104
- findBuilder(["action", "payload", id, resource]) || (schema && CommonPayload)
103
+ const PayloadForm = findBuilder(["action-form", id, resource]) || (schema && CommonPayload)
104
+ if (!PayloadForm)
105
+ return (
106
+ <Button label={label} title={title} onClick={onHandle} disabled={isPending}>
107
+ {buildIcon(icon)}
108
+ </Button>
109
+ )
105
110
 
106
111
  return (
107
112
  <>
108
113
  <Button
109
114
  label={label}
110
115
  title={title}
111
- onClick={() => {
112
- if (PayloadBuilder) setPayloadActive(true)
113
- else onHandle()
114
- }}
116
+ onClick={() => setPayloadActive(true)}
115
117
  disabled={isPending}
116
118
  >
117
119
  {buildIcon(icon)}
118
120
  </Button>
119
- {PayloadBuilder && (
120
- <PayloadBuilder
121
- active={payloadActive}
122
- onHandle={onHandle}
123
- onClose={() => setPayloadActive(false)}
124
- title={title || label}
125
- schema={schema}
126
- help={help}
127
- />
128
- )}
121
+ <PayloadForm
122
+ active={payloadActive}
123
+ onHandle={onHandle}
124
+ onClose={() => setPayloadActive(false)}
125
+ title={title || label}
126
+ schema={schema}
127
+ help={help}
128
+ />
129
129
  </>
130
130
  )
131
131
  }
@@ -160,11 +160,11 @@ export function CommonPayload({
160
160
  title={
161
161
  help
162
162
  ? ((
163
- <Stack alignItems="flex-start">
164
- <span>{translate(title, { _: title })}</span>
165
- <HelpLink href={help} style={{ alignSelf: "flex-end" }} />
166
- </Stack>
167
- ) as any)
163
+ <Stack alignItems="flex-start">
164
+ <span>{translate(title, { _: title })}</span>
165
+ <HelpLink href={help} style={{ alignSelf: "flex-end" }} />
166
+ </Stack>
167
+ ) as any)
168
168
  : translate(title, { _: title })
169
169
  }
170
170
  actions={
@@ -1,3 +1,4 @@
1
+ import { TID } from "./types"
1
2
  import { APIParams, makeRequest, setupAdmin } from "./utils"
2
3
 
3
4
  export function MuffinDataprovider(apiUrl: string) {
@@ -26,35 +27,35 @@ export function MuffinDataprovider(apiUrl: string) {
26
27
  return { data, total: parseInt(headers.get("x-total"), 10) }
27
28
  },
28
29
 
29
- getOne: async (resource, { id }) => {
30
+ getOne: async (resource: string, { id }: { id: TID }) => {
30
31
  return await request(`${resource}/${id}`)
31
32
  },
32
33
 
33
- create: async (resource, { data }) => {
34
+ create: async (resource: string, { data }) => {
34
35
  return await request(resource, {
35
36
  data,
36
37
  method: "POST",
37
38
  })
38
39
  },
39
40
 
40
- update: async (resource, { id, data }) => {
41
+ update: async (resource: string, { id, data }: { id: TID; data: any }) => {
41
42
  return await makeRequest(`${apiUrl}/${resource}/${id}`, {
42
43
  data,
43
44
  method: "PUT",
44
45
  }).then(({ json }) => ({ data: json }))
45
46
  },
46
47
 
47
- updateMany: async (resource, { ids, data }) => {
48
+ updateMany: async (resource: string, { ids, data }: { ids: TID[]; data: any }) => {
48
49
  await Promise.all(ids.map((id) => methods.update(resource, { id, data })))
49
50
  return { data: ids }
50
51
  },
51
52
 
52
- delete: async (resource, { id }) => {
53
+ delete: async (resource: string, { id }: { id: TID }) => {
53
54
  await request(`${resource}/${id}`, { method: "DELETE" })
54
55
  return { data: { id } }
55
56
  },
56
57
 
57
- deleteMany: async (resource, { ids }) => {
58
+ deleteMany: async (resource: string, { ids }: { ids: TID[][] }) => {
58
59
  await request(resource, {
59
60
  data: ids,
60
61
  method: "DELETE",
@@ -62,13 +63,13 @@ export function MuffinDataprovider(apiUrl: string) {
62
63
  return { data: ids }
63
64
  },
64
65
 
65
- getMany: (resource, props) => {
66
+ getMany: (resource: string, props: { ids: TID[]; meta: any }) => {
66
67
  const { ids, meta } = props
67
68
  const key = meta?.key || "id"
68
69
  return methods.getList(resource, { filter: { [key]: { $in: ids } } })
69
70
  },
70
71
 
71
- getManyReference: async (resource, { target, id, filter, ...opts }) => {
72
+ getManyReference: async (resource: string, { target, id, filter, ...opts }) => {
72
73
  filter = filter || {}
73
74
  filter[target] = id
74
75
  return await methods.getList(resource, { filter, ...opts })
@@ -96,4 +97,4 @@ export type TActionProps<T = any> = {
96
97
  ids?: string[]
97
98
  record?: T
98
99
  }
99
- setupAdmin(["dataprovider"], MuffinDataprovider)
100
+ setupAdmin(["data-provider"], MuffinDataprovider)
@@ -12,7 +12,7 @@ export function AvatarField({ source, alt, style, nameProp, ...props }) {
12
12
  .trim(" ")
13
13
  .split(/\s+/)
14
14
  .slice(0, 2)
15
- .map((n) => n[0].toUpperCase())
15
+ .map((n: string) => n[0].toUpperCase())
16
16
  .join("")
17
17
  : ""
18
18
 
package/src/types.ts CHANGED
@@ -1,7 +1,45 @@
1
+ export type TID = string | number
2
+
1
3
  export type TProps = {
2
4
  [key: string]: any
3
5
  }
4
6
 
7
+ export type TAdminType =
8
+ | "action-form"
9
+ | "admin"
10
+ | "appbar"
11
+ | "auth-provider"
12
+ | "auth-storage-get"
13
+ | "auth-storage-set"
14
+ | "create"
15
+ | "create-inputs"
16
+ | "create-toolbar"
17
+ | "dashboard"
18
+ | "dashboard-actions"
19
+ | "dashboard-content"
20
+ | "data-provider"
21
+ | "edit"
22
+ | "edit-actions"
23
+ | "edit-inputs"
24
+ | "edit-toolbar"
25
+ | "layout"
26
+ | "list"
27
+ | "list-toolbar"
28
+ | "list-grid"
29
+ | "list-grid-buttons"
30
+ | "list-filters"
31
+ | "list-fields"
32
+ | "list-bulk-actions"
33
+ | "locale"
34
+ | "loginpage"
35
+ | "resource"
36
+ | "show"
37
+ | "show-actions"
38
+ | "show-toolbar"
39
+ | "show-fields"
40
+ | "show-links"
41
+ export type TAdminPath = [type: TAdminType, ...ids: string[]]
42
+
5
43
  export type AdminAction = {
6
44
  view?: ("show" | "edit" | "list" | "bulk")[]
7
45
  help?: string
package/src/utils.tsx CHANGED
@@ -1,15 +1,16 @@
1
1
  import * as icons from "@mui/icons-material"
2
2
  import { stringify } from "query-string"
3
3
  import { fetchUtils } from "ra-core"
4
+ import { TAdminPath } from "./types"
4
5
 
5
- const builders = new Map<string, (props) => any>()
6
+ const builders = new Map<string, (props: any) => any>()
6
7
 
7
- export function setupAdmin<T>(adminType: string[], fc: (props) => T) {
8
+ export function setupAdmin<T>(adminType: TAdminPath, fc: (props: any) => T) {
8
9
  const key = adminType.join("/")
9
10
  builders.set(key, fc)
10
11
  }
11
12
 
12
- export function buildAdmin(adminType: string[], props = {}) {
13
+ export function buildAdmin(adminType: TAdminPath, props = {}) {
13
14
  const builder = findBuilder(adminType)
14
15
  if (process.env.NODE_ENV == "development") console.log(props)
15
16
  if (builder) return builder(props)
@@ -18,7 +19,7 @@ export function buildAdmin(adminType: string[], props = {}) {
18
19
  return null
19
20
  }
20
21
 
21
- export function findBuilder(adminType: string[]): (props) => any {
22
+ export function findBuilder(adminType: TAdminPath): (props: any) => any {
22
23
  if (process.env.NODE_ENV == "development") console.log(adminType)
23
24
  const key = [...adminType]
24
25
  while (key.length) {