muffin-admin 11.2.10 → 11.3.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/utils.d.ts CHANGED
@@ -13,7 +13,7 @@ export type APIParams = {
13
13
  };
14
14
  data?: any;
15
15
  headers?: any;
16
- body?: string;
16
+ body?: any;
17
17
  };
18
18
  export declare const requestHeaders: Record<string, string>;
19
19
  export declare function makeRequest(url: string, params?: APIParams): Promise<{
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "muffin-admin",
3
- "version": "11.2.10",
3
+ "version": "11.3.0",
4
4
  "description": "NPM package for https://github.com/klen/muffin-admin",
5
5
  "files": [
6
6
  "src",
package/src/buildRA.tsx CHANGED
@@ -38,7 +38,7 @@ import {
38
38
 
39
39
  import { AvatarField, CopyField, EditableBooleanField, FKField, JsonField } from "./fields"
40
40
  import { DateRangeFilter, Filter, SearchFilter } from "./filters"
41
- import { FKInput, JsonInput, TimestampInput } from "./inputs"
41
+ import { FKInput, ImgInput, JsonInput, TimestampInput } from "./inputs"
42
42
 
43
43
  const UI: Record<string, JSX.ElementType> = {
44
44
  // Fields
@@ -74,6 +74,7 @@ const UI: Record<string, JSX.ElementType> = {
74
74
  FKInput,
75
75
  FileInput,
76
76
  ImageInput,
77
+ ImgInput,
77
78
  JsonInput,
78
79
  NullableBooleanInput,
79
80
  NumberInput,
@@ -81,11 +82,11 @@ const UI: Record<string, JSX.ElementType> = {
81
82
  RadioButtonGroupInput,
82
83
  ReferenceArrayInput,
83
84
  ReferenceInput,
85
+ SearchInput,
84
86
  SelectArrayInput,
85
87
  SelectInput,
86
88
  TextInput,
87
89
  TimestampInput,
88
- SearchInput,
89
90
 
90
91
  // Filters
91
92
  Filter,
@@ -60,15 +60,15 @@ export function MuffinDataprovider(apiUrl: string) {
60
60
 
61
61
  create: async (resource: string, { data }) => {
62
62
  return await request(resource, {
63
- data,
64
63
  method: "POST",
64
+ ...prepareDataParams(data),
65
65
  })
66
66
  },
67
67
 
68
68
  update: async (resource: string, { id, data }: { id: TID; data: any }) => {
69
69
  return await makeRequest(`${apiUrl}/${resource}/${id}`, {
70
- data,
71
70
  method: "PUT",
71
+ ...prepareDataParams(data),
72
72
  }).then(({ json }) => ({ data: json }))
73
73
  },
74
74
 
@@ -125,3 +125,26 @@ export type TActionProps<T = any> = {
125
125
  record?: T
126
126
  }
127
127
  setupAdmin(["data-provider"], MuffinDataprovider)
128
+
129
+ function isFileValue(value: any): boolean {
130
+ return value && typeof value === "object" && value.rawFile instanceof File
131
+ }
132
+
133
+ function prepareDataParams(data: Record<string, any>) {
134
+ const hasFiles = Object.values(data).some(isFileValue)
135
+
136
+ if (!hasFiles) return { data }
137
+
138
+ const formData = new FormData()
139
+ for (const key in data) {
140
+ const value = data[key]
141
+ if (isFileValue(value)) {
142
+ formData.append(key, value.rawFile)
143
+ } else if (typeof value === "object" && value !== null) {
144
+ formData.append(key, JSON.stringify(value))
145
+ } else if (value !== undefined && value !== null) {
146
+ formData.append(key, value)
147
+ }
148
+ }
149
+ return { body: formData }
150
+ }
@@ -1,4 +1,4 @@
1
- import { ReferenceField, TextField } from "react-admin"
1
+ import { ReferenceField, TextField, useFieldValue, UseFieldValueOptions } from "react-admin"
2
2
 
3
3
  export function FKField({ reference, refSource, refKey, link, source, ...props }) {
4
4
  refKey = refKey || "id"
@@ -10,18 +10,22 @@ export function FKField({ reference, refSource, refKey, link, source, ...props }
10
10
  queryOptions={{ meta: { key: refKey } }}
11
11
  {...props}
12
12
  >
13
- <>
14
- <TextField source={refSource} />
15
- {refSource !== refKey && (
16
- <>
17
- {" (#"}
18
- <TextField source={refKey} />
19
- {")"}
20
- </>
21
- )}
22
- </>
13
+ <TextField source={refSource} />
14
+ {refSource !== refKey && <FKFieldValue source={refKey} />}
23
15
  </ReferenceField>
24
16
  )
25
17
  }
26
18
 
19
+ function FKFieldValue(props: UseFieldValueOptions) {
20
+ const value = useFieldValue(props)
21
+ if (value === undefined) return null
22
+ return (
23
+ <>
24
+ {" (#"}
25
+ <TextField {...props} />
26
+ {")"}
27
+ </>
28
+ )
29
+ }
30
+
27
31
  FKField.displayName = "FKField"
package/src/index.tsx CHANGED
@@ -37,4 +37,4 @@ export async function initAdmin(prefix = "", containerId: string = "root") {
37
37
  )
38
38
  }
39
39
 
40
- export const VERSION = "11.2.10"
40
+ export const VERSION = "11.3.0"
@@ -0,0 +1,24 @@
1
+ import { startCase } from "lodash"
2
+ import { ImageField, ImageInput, RecordContext, useInput, useRecordContext } from "react-admin"
3
+
4
+ export function ImgInput({ source, label, ...props }) {
5
+ const { field } = useInput({ source })
6
+ label = label || field.name
7
+
8
+ return (
9
+ <ImageInput source={source} label={startCase(label || field.name)} {...props}>
10
+ <PreviewImage />
11
+ </ImageInput>
12
+ )
13
+ }
14
+
15
+ function PreviewImage() {
16
+ const value = useRecordContext()
17
+ const source = value?.src || value
18
+
19
+ return (
20
+ <RecordContext.Provider value={{ source }}>
21
+ <ImageField source="source" title="source" />
22
+ </RecordContext.Provider>
23
+ )
24
+ }
@@ -1,3 +1,4 @@
1
1
  export * from "./FKInput"
2
+ export * from "./ImgInput"
2
3
  export * from "./JsonInput"
3
4
  export * from "./TimestampInput"
package/src/utils.tsx CHANGED
@@ -40,7 +40,7 @@ export type APIParams = {
40
40
  }
41
41
  data?: any
42
42
  headers?: any
43
- body?: string
43
+ body?: any
44
44
  }
45
45
 
46
46
  export const requestHeaders: Record<string, string> = {}