Skip to content

Commit 91e146e

Browse files
Fix Profile picture uploader (#1471)
1 parent cbcb49c commit 91e146e

File tree

2 files changed

+38
-27
lines changed

2 files changed

+38
-27
lines changed

front/src/modules/settings/profile/components/ProfilePictureUploader.tsx

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,13 @@ import {
1212
} from '~/generated/graphql';
1313

1414
export function ProfilePictureUploader() {
15-
const [uploadPicture] = useUploadProfilePictureMutation();
15+
const [uploadPicture, { loading: isUploading }] =
16+
useUploadProfilePictureMutation();
1617
const [removePicture] = useRemoveProfilePictureMutation();
1718
const [currentUser] = useRecoilState(currentUserState);
1819
const [uploadController, setUploadController] =
1920
useState<AbortController | null>(null);
20-
const [error, setError] = useState<Error | null>(null);
21+
const [errorMessage, setErrorMessage] = useState<string | null>(null);
2122

2223
async function handleUpload(file: File) {
2324
if (!file) {
@@ -41,10 +42,10 @@ export function ProfilePictureUploader() {
4142
});
4243

4344
setUploadController(null);
44-
setError(null);
45+
setErrorMessage(null);
4546
return result;
4647
} catch (error) {
47-
setError(error as Error);
48+
setErrorMessage('An error occured while uploading the picture.');
4849
}
4950
}
5051

@@ -72,7 +73,8 @@ export function ProfilePictureUploader() {
7273
onUpload={handleUpload}
7374
onRemove={handleRemove}
7475
onAbort={handleAbort}
75-
error={error}
76+
isUploading={isUploading}
77+
errorMessage={errorMessage}
7678
/>
7779
);
7880
}

front/src/modules/ui/input/image/components/ImageInput.tsx

Lines changed: 31 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { useTheme } from '@emotion/react';
33
import styled from '@emotion/styled';
44

55
import { Button } from '@/ui/button/components/Button';
6-
import { IconFileUpload, IconTrash, IconUpload } from '@/ui/icon';
6+
import { IconFileUpload, IconTrash, IconUpload, IconX } from '@/ui/icon';
77

88
const StyledContainer = styled.div`
99
display: flex;
@@ -67,6 +67,12 @@ const StyledText = styled.span`
6767
font-size: ${({ theme }) => theme.font.size.xs};
6868
`;
6969

70+
const StyledErrorText = styled.span`
71+
color: ${({ theme }) => theme.font.color.danger};
72+
font-size: ${({ theme }) => theme.font.size.xs};
73+
margin-top: ${({ theme }) => theme.spacing(1)};
74+
`;
75+
7076
const StyledHiddenFileInput = styled.input`
7177
display: none;
7278
`;
@@ -76,7 +82,8 @@ type Props = Omit<React.ComponentProps<'div'>, 'children'> & {
7682
onUpload?: (file: File) => void;
7783
onRemove?: () => void;
7884
onAbort?: () => void;
79-
error?: Error | null;
85+
isUploading?: boolean;
86+
errorMessage?: string | null;
8087
disabled?: boolean;
8188
};
8289

@@ -85,7 +92,8 @@ export function ImageInput({
8592
onUpload,
8693
onRemove,
8794
onAbort,
88-
error,
95+
isUploading = false,
96+
errorMessage,
8997
disabled = false,
9098
...restProps
9199
}: Props) {
@@ -125,37 +133,38 @@ export function ImageInput({
125133
}
126134
}}
127135
/>
128-
<Button
129-
icon={<IconUpload size={theme.icon.size.sm} />}
130-
onClick={onUploadButtonClick}
131-
variant="secondary"
132-
title="Upload"
133-
disabled={disabled}
134-
fullWidth
135-
/>
136-
<Button
137-
icon={<IconTrash size={theme.icon.size.sm} />}
138-
onClick={onRemove}
139-
variant="secondary"
140-
title="Remove"
141-
disabled={!picture || disabled}
142-
fullWidth
143-
/>
144-
{onAbort && (
136+
{isUploading && onAbort ? (
145137
<Button
146-
icon={<IconTrash size={theme.icon.size.sm} />}
138+
icon={<IconX />}
147139
onClick={onAbort}
148140
variant="secondary"
149141
title="Abort"
150142
disabled={!picture || disabled}
151143
fullWidth
152144
/>
145+
) : (
146+
<Button
147+
icon={<IconUpload />}
148+
onClick={onUploadButtonClick}
149+
variant="secondary"
150+
title="Upload"
151+
disabled={disabled}
152+
fullWidth
153+
/>
153154
)}
155+
<Button
156+
icon={<IconTrash />}
157+
onClick={onRemove}
158+
variant="secondary"
159+
title="Remove"
160+
disabled={!picture || disabled}
161+
fullWidth
162+
/>
154163
</StyledButtonContainer>
155164
<StyledText>
156165
We support your best PNGs, JPEGs and GIFs portraits under 10MB
157166
</StyledText>
158-
{error && <StyledText>{error.message}</StyledText>}
167+
{errorMessage && <StyledErrorText>{errorMessage}</StyledErrorText>}
159168
</StyledContent>
160169
</StyledContainer>
161170
);

0 commit comments

Comments
 (0)