@applica-software-guru/react-admin 1.5.337 → 1.5.339

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/package.json CHANGED
@@ -108,5 +108,5 @@
108
108
  "type": "module",
109
109
  "types": "dist/index.d.ts",
110
110
  "typings": "dist/index.d.ts",
111
- "version": "1.5.337"
111
+ "version": "1.5.339"
112
112
  }
@@ -0,0 +1,58 @@
1
+ import _ from 'lodash';
2
+ import { useEffect, useState } from 'react';
3
+ import { FieldProps, ImageField, useDataProvider, useRecordContext, useResourceContext } from 'react-admin';
4
+
5
+ type ImageAttachmentFieldProps<RecordType extends Record<string, any> = Record<string, any>> = FieldProps<RecordType>;
6
+ type ContextValueImage = { url: string; title?: string };
7
+
8
+ function ImageAttachmentField(props: ImageAttachmentFieldProps) {
9
+ const { resource: propResource, source } = props;
10
+ const dataProvider = useDataProvider();
11
+ const defaultResource = useResourceContext();
12
+ const entity = !_.isNil(propResource) ? propResource : defaultResource?.match(/(?<=\/)[^\/]+$/)?.[0]; //eslint-disable-line
13
+ const recordContext = useRecordContext(props);
14
+ const entityId = recordContext?.id;
15
+ const attachment = recordContext?.[source ?? ''];
16
+ const [nestedContextValue, setNestedContextValue] = useState<{
17
+ image: ContextValueImage | Array<ContextValueImage>;
18
+ } | null>(null);
19
+
20
+ useEffect(() => {
21
+ if (_.isNil(attachment) || _.isNil(dataProvider) || _.isNil(entity) || _.isNil(entityId) || _.isNil(source)) {
22
+ return;
23
+ }
24
+ const basePath = `/attachments/${entity}/${entityId}/${source}/`;
25
+ function getAttachment(attachment: { id: string; name: string }): Promise<ContextValueImage> {
26
+ return dataProvider
27
+ .getFile(`${basePath}${attachment?.id}`)
28
+ .then((url: string) => ({ url, title: attachment?.name }));
29
+ }
30
+
31
+ if (_.isArray(attachment)) {
32
+ Promise.all(attachment.map(getAttachment)).then((images: Array<ContextValueImage>) => {
33
+ setNestedContextValue({ image: images });
34
+ });
35
+ } else {
36
+ getAttachment(attachment).then((image: ContextValueImage) => {
37
+ setNestedContextValue({ image });
38
+ });
39
+ }
40
+ }, [attachment, dataProvider, entity, entityId, source]);
41
+
42
+ if (_.isNil(nestedContextValue)) {
43
+ return null;
44
+ }
45
+
46
+ return (
47
+ <ImageField
48
+ {...props}
49
+ src="url"
50
+ source={_.isArray(attachment) ? 'image' : 'image.url'}
51
+ title="image.title"
52
+ record={nestedContextValue}
53
+ />
54
+ );
55
+ }
56
+
57
+ export { ImageAttachmentField };
58
+ export type { ImageAttachmentFieldProps };
@@ -6,6 +6,7 @@ export * from './DateField';
6
6
  export * from './EmailField';
7
7
  export * from './FileField';
8
8
  export * from './FunctionField';
9
+ export * from './ImageAttachmentField';
9
10
  export * from './ImageField';
10
11
  export * from './LocalizedTextField';
11
12
  export * from './ReadonlyField';
@@ -0,0 +1,47 @@
1
+ import { ImageAttachmentField, ImageAttachmentFieldProps } from '@/components/ra-fields';
2
+ import _ from 'lodash';
3
+ import { RecordContextProvider, useRecordContext } from 'ra-core';
4
+ import { ImageField } from 'ra-ui-materialui';
5
+ import { useEffect, useState } from 'react';
6
+ import { AttachmentInput, AttachmentInputProps } from './AttachmentInput';
7
+
8
+ type ImageAttachmentInputProps = Omit<AttachmentInputProps, 'children'>;
9
+
10
+ function ImageAttachmentInput(props: ImageAttachmentInputProps) {
11
+ const { source } = props;
12
+ const { id: entityId } = useRecordContext(props);
13
+ return (
14
+ <AttachmentInput {...props}>
15
+ <Field entityId={String(entityId)} source={source} />
16
+ </AttachmentInput>
17
+ );
18
+ }
19
+
20
+ function Field(props: { entityId?: string; source?: string }) {
21
+ const value = useRecordContext();
22
+
23
+ return _.isNil(value?.rawFile) ? (
24
+ <AttachmentPreview {...props} />
25
+ ) : (
26
+ <ImageField {...props} title="title" source={'src'} />
27
+ );
28
+ }
29
+
30
+ function AttachmentPreview(props: ImageAttachmentFieldProps & { entityId?: string }) {
31
+ const { entityId, ...rest } = props;
32
+ const source = props.source ?? '';
33
+ const value = useRecordContext();
34
+ const [context, setContext] = useState(source ? { id: entityId, [source]: value } : { id: entityId });
35
+
36
+ useEffect(() => {
37
+ setContext({ id: entityId, [source]: value });
38
+ }, [value, entityId, source]);
39
+
40
+ return (
41
+ <RecordContextProvider value={context}>
42
+ <ImageAttachmentField {...rest} />
43
+ </RecordContextProvider>
44
+ );
45
+ }
46
+
47
+ export { ImageAttachmentInput };
@@ -6,6 +6,7 @@ export * from './BooleanInput';
6
6
  export * from './DateInput';
7
7
  export * from './DateTimeInput';
8
8
  export * from './FileInput';
9
+ export * from './ImageAttachmentInput';
9
10
  export * from './ImageInput';
10
11
  export * from './LabeledArrayInput';
11
12
  export * from './LabeledInput';
package/src/index.ts CHANGED
@@ -91,6 +91,7 @@ export {
91
91
  useResourceDefinition,
92
92
  useResourceDefinitions,
93
93
  useSafeSetState,
94
+ useSaveContext,
94
95
  useShowContext,
95
96
  useShowController,
96
97
  useSimpleFormIterator,