Skip to content

Commit

Permalink
Robust Photo Uploader, handling unsupported file types, upload error,…
Browse files Browse the repository at this point in the history
… apollo uploader (twentyhq#1400)

* added uploaded controller, handled unsupported image formatting and error uploading styling

* remove callbacks
  • Loading branch information
friendlymatthew authored Sep 1, 2023
1 parent 5653b89 commit aa47579
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 10 deletions.
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { useState } from 'react';
import { getOperationName } from '@apollo/client/utilities';
import { useRecoilState } from 'recoil';

Expand All @@ -14,19 +15,47 @@ export function ProfilePictureUploader() {
const [uploadPicture] = useUploadProfilePictureMutation();
const [removePicture] = useRemoveProfilePictureMutation();
const [currentUser] = useRecoilState(currentUserState);
async function onUpload(file: File) {
const [uploadController, setUploadController] =
useState<AbortController | null>(null);
const [error, setError] = useState<Error | null>(null);

async function handleUpload(file: File) {
if (!file) {
return;
}
await uploadPicture({
variables: {
file,
},
refetchQueries: [getOperationName(GET_CURRENT_USER) ?? ''],
});

const controller = new AbortController();
setUploadController(controller);

try {
const result = await uploadPicture({
variables: {
file,
},
context: {
fetchOptions: {
signal: controller.signal,
},
},
refetchQueries: [getOperationName(GET_CURRENT_USER) ?? ''],
});

setUploadController(null);
setError(null);
return result;
} catch (error) {
setError(error as Error);
}
}

async function handleAbort() {
if (uploadController) {
uploadController.abort();
setUploadController(null);
}
}

async function onRemove() {
async function handleRemove() {
await removePicture({
variables: {
where: {
Expand All @@ -40,8 +69,10 @@ export function ProfilePictureUploader() {
return (
<ImageInput
picture={getImageAbsoluteURIOrBase64(currentUser?.avatarUrl)}
onUpload={onUpload}
onRemove={onRemove}
onUpload={handleUpload}
onRemove={handleRemove}
onAbort={handleAbort}
error={error}
/>
);
}
16 changes: 16 additions & 0 deletions front/src/modules/ui/input/image/components/ImageInput.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -75,13 +75,17 @@ type Props = Omit<React.ComponentProps<'div'>, 'children'> & {
picture: string | null | undefined;
onUpload?: (file: File) => void;
onRemove?: () => void;
onAbort?: () => void;
error?: Error | null;
disabled?: boolean;
};

export function ImageInput({
picture,
onUpload,
onRemove,
onAbort,
error,
disabled = false,
...restProps
}: Props) {
Expand Down Expand Up @@ -112,6 +116,7 @@ export function ImageInput({
<StyledHiddenFileInput
type="file"
ref={hiddenFileInput}
accept="image/jpeg, image/png, image/gif" // to desired specification
onChange={(event) => {
if (onUpload) {
if (event.target.files) {
Expand All @@ -136,10 +141,21 @@ export function ImageInput({
disabled={!picture || disabled}
fullWidth
/>
{onAbort && (
<Button
icon={<IconTrash size={theme.icon.size.sm} />}
onClick={onAbort}
variant="secondary"
title="Abort"
disabled={!picture || disabled}
fullWidth
/>
)}
</StyledButtonContainer>
<StyledText>
We support your best PNGs, JPEGs and GIFs portraits under 10MB
</StyledText>
{error && <StyledText>{error.message}</StyledText>}
</StyledContent>
</StyledContainer>
);
Expand Down

0 comments on commit aa47579

Please sign in to comment.