Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow for customised file uploads #146

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/fuzzy-zebras-change.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@newfrontdoor/s3-file-upload": minor
---

Add `use-s3-dropzone` hook for S3 file uploads
2 changes: 1 addition & 1 deletion packages/s3-file-upload/__tests__/s3-dropzone.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ test('it renders a dropzone', async () => {
[
'Upload Audio',
'Drag ‘n’ drop some files here, or click to select files',
'Try dropping an audio file here, or click to select file for upload.'
'Try dropping a file here, or click to select file for upload.'
].join('')
);

Expand Down
3 changes: 2 additions & 1 deletion packages/s3-file-upload/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
export {S3Dropzone} from './s3-dropzone';
export {useS3FileUpload} from './use-s3-file-upload';
export * from './use-s3-file-upload';
export * from './use-s3-dropzone';
32 changes: 14 additions & 18 deletions packages/s3-file-upload/src/s3-dropzone.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,8 @@
import {jsx} from 'theme-ui';
import {cloneElement, FC} from 'react';
import PropTypes from 'prop-types';
import {useDropzone} from 'react-dropzone';
import {ScaleLoader} from 'react-spinners';
import {useS3FileUpload, UploadFileResult} from './use-s3-file-upload';
import {useS3Dropzone, UploadFileResult} from './use-s3-dropzone';

const baseStyle = {
borderWidth: 2,
Expand All @@ -29,6 +28,10 @@ export type S3DropzoneProps = {
uploadUrl: string;
title?: string;
initialFileName?: string;
errorJSX?: React.ReactElement;
loadingJSX?: React.ReactElement;
accept?: string | string[];
customElementTitle?: string;
onChange(result?: UploadFileResult): void;
};

Expand All @@ -47,25 +50,18 @@ export const S3Dropzone: FC<S3DropzoneProps> = ({
isError,
isSuccess,
isIdle,
startFileUpload
} = useS3FileUpload({
getRootProps,
getInputProps,
isDragActive,
isDragReject
} = useS3Dropzone({
host,
uploadUrl,
initialFileName
initialFileName,
accept: 'audio/*',
onChange,
});

const {getRootProps, getInputProps, isDragActive, isDragReject} = useDropzone(
{
accept: 'audio/*',
onDrop(acceptedFiles) {
const [firstFile] = acceptedFiles;
void startFileUpload(firstFile).then((result) => {
onChange(result);
});
}
}
);

let styles = {...baseStyle};
styles = isDragActive ? {...styles, ...activeStyle} : styles;
styles = isDragReject ? {...styles, ...rejectStyle} : styles;
Expand Down Expand Up @@ -123,7 +119,7 @@ export const S3Dropzone: FC<S3DropzoneProps> = ({
<p>Unsupported file type...</p>
) : (
<p>
Try dropping an audio file here, or click to select file for
Try dropping a file here, or click to select file for
upload.
</p>
)}
Expand Down
68 changes: 68 additions & 0 deletions packages/s3-file-upload/src/use-s3-dropzone.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
import {useDropzone, DropzoneState} from 'react-dropzone';
import {useS3FileUpload, UploadFileResult} from './use-s3-file-upload';
export type {UploadFileResult} from './use-s3-file-upload';

export type S3DropzoneOptions = {
host: string;
uploadUrl: string;
title?: string;
initialFileName?: string;
errorJSX?: React.ReactElement;
loadingJSX?: React.ReactElement;
accept?: string | string[];
customElementTitle?: string;
onChange(result?: UploadFileResult): void;
};

export type S3DropzoneState = DropzoneState & {
fileUrl: URL | null,
fileName?: string,
isLoading: boolean,
isError: boolean,
isSuccess: boolean,
isIdle: boolean,
}

export function useS3Dropzone({
host,
uploadUrl,
initialFileName,
accept,
onChange
}: S3DropzoneOptions): S3DropzoneState {
const {
fileUrl,
fileName,
isLoading,
isError,
isSuccess,
isIdle,
startFileUpload
} = useS3FileUpload({
host,
uploadUrl,
initialFileName
});

const dropzoneState = useDropzone(
{
accept,
onDrop(acceptedFiles) {
const [firstFile] = acceptedFiles;
void startFileUpload(firstFile).then((result) => {
onChange(result);
});
}
}
);

return {
fileUrl,
fileName,
isLoading,
isError,
isSuccess,
isIdle,
...dropzoneState
}
}