Robust Photo Uploader, handling unsupported file types, upload error, apollo uploader (#1400)
* added uploaded controller, handled unsupported image formatting and error uploading styling * remove callbacks
This commit is contained in:
@ -1,3 +1,4 @@
|
|||||||
|
import { useState } from 'react';
|
||||||
import { getOperationName } from '@apollo/client/utilities';
|
import { getOperationName } from '@apollo/client/utilities';
|
||||||
import { useRecoilState } from 'recoil';
|
import { useRecoilState } from 'recoil';
|
||||||
|
|
||||||
@ -14,19 +15,47 @@ export function ProfilePictureUploader() {
|
|||||||
const [uploadPicture] = useUploadProfilePictureMutation();
|
const [uploadPicture] = useUploadProfilePictureMutation();
|
||||||
const [removePicture] = useRemoveProfilePictureMutation();
|
const [removePicture] = useRemoveProfilePictureMutation();
|
||||||
const [currentUser] = useRecoilState(currentUserState);
|
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) {
|
if (!file) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
await uploadPicture({
|
|
||||||
variables: {
|
const controller = new AbortController();
|
||||||
file,
|
setUploadController(controller);
|
||||||
},
|
|
||||||
refetchQueries: [getOperationName(GET_CURRENT_USER) ?? ''],
|
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 onRemove() {
|
async function handleAbort() {
|
||||||
|
if (uploadController) {
|
||||||
|
uploadController.abort();
|
||||||
|
setUploadController(null);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
async function handleRemove() {
|
||||||
await removePicture({
|
await removePicture({
|
||||||
variables: {
|
variables: {
|
||||||
where: {
|
where: {
|
||||||
@ -40,8 +69,10 @@ export function ProfilePictureUploader() {
|
|||||||
return (
|
return (
|
||||||
<ImageInput
|
<ImageInput
|
||||||
picture={getImageAbsoluteURIOrBase64(currentUser?.avatarUrl)}
|
picture={getImageAbsoluteURIOrBase64(currentUser?.avatarUrl)}
|
||||||
onUpload={onUpload}
|
onUpload={handleUpload}
|
||||||
onRemove={onRemove}
|
onRemove={handleRemove}
|
||||||
|
onAbort={handleAbort}
|
||||||
|
error={error}
|
||||||
/>
|
/>
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|||||||
@ -75,6 +75,8 @@ type Props = Omit<React.ComponentProps<'div'>, 'children'> & {
|
|||||||
picture: string | null | undefined;
|
picture: string | null | undefined;
|
||||||
onUpload?: (file: File) => void;
|
onUpload?: (file: File) => void;
|
||||||
onRemove?: () => void;
|
onRemove?: () => void;
|
||||||
|
onAbort?: () => void;
|
||||||
|
error?: Error | null;
|
||||||
disabled?: boolean;
|
disabled?: boolean;
|
||||||
};
|
};
|
||||||
|
|
||||||
@ -82,6 +84,8 @@ export function ImageInput({
|
|||||||
picture,
|
picture,
|
||||||
onUpload,
|
onUpload,
|
||||||
onRemove,
|
onRemove,
|
||||||
|
onAbort,
|
||||||
|
error,
|
||||||
disabled = false,
|
disabled = false,
|
||||||
...restProps
|
...restProps
|
||||||
}: Props) {
|
}: Props) {
|
||||||
@ -112,6 +116,7 @@ export function ImageInput({
|
|||||||
<StyledHiddenFileInput
|
<StyledHiddenFileInput
|
||||||
type="file"
|
type="file"
|
||||||
ref={hiddenFileInput}
|
ref={hiddenFileInput}
|
||||||
|
accept="image/jpeg, image/png, image/gif" // to desired specification
|
||||||
onChange={(event) => {
|
onChange={(event) => {
|
||||||
if (onUpload) {
|
if (onUpload) {
|
||||||
if (event.target.files) {
|
if (event.target.files) {
|
||||||
@ -136,10 +141,21 @@ export function ImageInput({
|
|||||||
disabled={!picture || disabled}
|
disabled={!picture || disabled}
|
||||||
fullWidth
|
fullWidth
|
||||||
/>
|
/>
|
||||||
|
{onAbort && (
|
||||||
|
<Button
|
||||||
|
icon={<IconTrash size={theme.icon.size.sm} />}
|
||||||
|
onClick={onAbort}
|
||||||
|
variant="secondary"
|
||||||
|
title="Abort"
|
||||||
|
disabled={!picture || disabled}
|
||||||
|
fullWidth
|
||||||
|
/>
|
||||||
|
)}
|
||||||
</StyledButtonContainer>
|
</StyledButtonContainer>
|
||||||
<StyledText>
|
<StyledText>
|
||||||
We support your best PNGs, JPEGs and GIFs portraits under 10MB
|
We support your best PNGs, JPEGs and GIFs portraits under 10MB
|
||||||
</StyledText>
|
</StyledText>
|
||||||
|
{error && <StyledText>{error.message}</StyledText>}
|
||||||
</StyledContent>
|
</StyledContent>
|
||||||
</StyledContainer>
|
</StyledContainer>
|
||||||
);
|
);
|
||||||
|
|||||||
Reference in New Issue
Block a user