Skip to content

Commit

Permalink
fixup! Feat(web-react): FileUploader - Support for crop image #DS-954
Browse files Browse the repository at this point in the history
  • Loading branch information
literat committed Oct 13, 2023
1 parent ada8d3e commit e68901b
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 20 deletions.
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
import React, { useRef, RefObject, MouseEvent, useState } from 'react';
import classNames from 'classnames';
import { SpiritFileUploaderAttachmentProps } from '../../types';
import React, { MouseEvent, RefObject, useRef, useState } from 'react';
import { useClassNamePrefix, useDeprecationMessage, useStyleProps } from '../../hooks';
import { useFileUploaderStyleProps } from './useFileUploaderStyleProps';
import { useFileUploaderAttachment } from './useFileUploaderAttachment';
import AttachmentImagePreview from './AttachmentImagePreview';
import { SpiritFileUploaderAttachmentProps } from '../../types';
import { Icon } from '../Icon';
import { DEFAULT_ICON_NAME, DEFAULT_BUTTON_LABEL, DEFAULT_EDIT_BUTTON_LABEL } from './constants';
import { image2Base64Preview } from './utils';
import AttachmentActionButton from './AttachmentActionButton';
import AttachmentDismissButton from './AttachmentDismissButton';
import AttachmentImagePreview from './AttachmentImagePreview';
import { DEFAULT_BUTTON_LABEL, DEFAULT_EDIT_BUTTON_LABEL, DEFAULT_ICON_NAME } from './constants';
import { useFileUploaderAttachment } from './useFileUploaderAttachment';
import { useFileUploaderStyleProps } from './useFileUploaderStyleProps';
import { image2Base64Preview } from './utils';

const FileUploaderAttachment = (props: SpiritFileUploaderAttachmentProps) => {
const {
Expand Down Expand Up @@ -52,7 +52,7 @@ const FileUploaderAttachment = (props: SpiritFileUploaderAttachmentProps) => {
image2Base64Preview(file, 100, (compressedDataURL) => setImagePreview(compressedDataURL));
}

useFileUploaderAttachment({ attachmentRef, file, name, onError });
useFileUploaderAttachment({ attachmentRef, file, name, meta, onError });

useDeprecationMessage({
method: 'property',
Expand All @@ -74,6 +74,8 @@ const FileUploaderAttachment = (props: SpiritFileUploaderAttachmentProps) => {
},
});

console.log(meta);

return (
<li
id={id}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { RefObject, useEffect } from 'react';
import { getAttachmentInput } from './utils';
import { RefObject, useLayoutEffect } from 'react';
import { FileQueueValueMetaType } from '../../types/fileUploader';
import { getAttachmentInput, getAttachmentMetaInput } from './utils';

export interface UseFileUploaderAttachmentProps {
attachmentRef: RefObject<HTMLLIElement>;
Expand All @@ -10,17 +10,25 @@ export interface UseFileUploaderAttachmentProps {
onError?: (error: string) => void;
}

export const useFileUploaderAttachment = ({ attachmentRef, file, name, onError }: UseFileUploaderAttachmentProps) => {
const createAttachmentInput = () => {
export const useFileUploaderAttachment = ({
attachmentRef,
file,
name,
meta,
onError,
}: UseFileUploaderAttachmentProps) => {
const createAttachmentInput = (metadata) => {
attachmentRef.current?.querySelectorAll('input').forEach((element) => element.remove());
const attachmentInputElement = getAttachmentInput(file, name, onError) as Node;

attachmentRef.current?.appendChild(attachmentInputElement);
};

useEffect(() => {
createAttachmentInput();
if (meta) {
const attachmentInputMetaElement = getAttachmentMetaInput(file, name, metadata, onError) as Node;
attachmentRef.current?.appendChild(attachmentInputMetaElement);
}
};

/* We want to call this hook only once */
// eslint-disable-next-line react-hooks/exhaustive-deps
}, []);
useLayoutEffect(() => {
createAttachmentInput(meta);
}, [createAttachmentInput, meta]);
};
13 changes: 12 additions & 1 deletion packages/web-react/src/components/FileUploader/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,17 @@ const getAttachmentInput = (file: File, name: string, onError?: (error: string)
return attachmentInputElement;
};

const getAttachmentMetaInput = (file: File, name: string, meta, onError?: (error: string) => void) => {
const attachmentInputElement = document.createElement('input');

attachmentInputElement.setAttribute('type', 'text');
attachmentInputElement.setAttribute('name', `${name}_meta`);
attachmentInputElement.setAttribute('value', JSON.stringify(meta));
attachmentInputElement.setAttribute('hidden', '');

return attachmentInputElement;
};

const image2Base64Preview = (file: File, maxWidth: number, callback: (base64Preview: string) => void) => {
const reader = new FileReader();

Expand Down Expand Up @@ -59,4 +70,4 @@ const base64ToByteArray = (base64Image: string) => {
return byteArray;
};

export { getAttachmentInput, image2Base64Preview, base64ToByteArray };
export { base64ToByteArray, getAttachmentInput, getAttachmentMetaInput, image2Base64Preview };

0 comments on commit e68901b

Please sign in to comment.