muffin-admin 11.2.11 → 11.3.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/buildRA.js +3 -2
- package/dist/dataprovider.js +24 -8
- package/dist/fields/AvatarField.js +12 -1
- package/dist/index.d.ts +1 -1
- package/dist/index.js +1 -1
- package/dist/inputs/ImgInput.d.ts +5 -0
- package/dist/inputs/ImgInput.js +25 -0
- package/dist/inputs/index.d.ts +1 -0
- package/dist/inputs/index.js +1 -0
- package/dist/tsconfig.tsbuildinfo +1 -1
- package/dist/utils.d.ts +1 -1
- package/package.json +1 -1
- package/src/buildRA.tsx +3 -2
- package/src/dataprovider.ts +25 -2
- package/src/fields/AvatarField.tsx +14 -1
- package/src/index.tsx +1 -1
- package/src/inputs/ImgInput.tsx +24 -0
- package/src/inputs/index.ts +1 -0
- package/src/utils.tsx +1 -1
package/dist/utils.d.ts
CHANGED
package/package.json
CHANGED
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,
|
package/src/dataprovider.ts
CHANGED
|
@@ -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,12 +1,25 @@
|
|
|
1
1
|
import Avatar from "@mui/material/Avatar"
|
|
2
|
+
import CircularProgress from "@mui/material/CircularProgress"
|
|
2
3
|
import get from "lodash/get"
|
|
3
4
|
import { useRecordContext } from "ra-core"
|
|
4
5
|
|
|
5
6
|
export function AvatarField({ source, alt, style, nameProp, ...props }) {
|
|
6
7
|
const record = useRecordContext(props)
|
|
7
|
-
|
|
8
|
+
let value = get(record, source),
|
|
8
9
|
name = record[nameProp]
|
|
9
10
|
|
|
11
|
+
// Check value is a valid URL
|
|
12
|
+
if (value) {
|
|
13
|
+
try {
|
|
14
|
+
new URL(value);
|
|
15
|
+
} catch {
|
|
16
|
+
// Not a valid URL, return loader
|
|
17
|
+
return (
|
|
18
|
+
<CircularProgress size={24} />
|
|
19
|
+
)
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
|
|
10
23
|
const letters = name
|
|
11
24
|
? name
|
|
12
25
|
.trim(" ")
|
package/src/index.tsx
CHANGED
|
@@ -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
|
+
}
|
package/src/inputs/index.ts
CHANGED