computer-scienceangular-roadmapbackend-roadmapblockchain-roadmapdba-roadmapdeveloper-roadmapdevops-roadmapfrontend-roadmapgo-roadmaphactoberfestjava-roadmapjavascript-roadmapnodejs-roadmappython-roadmapqa-roadmapreact-roadmaproadmapstudy-planvue-roadmapweb3-roadmap
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
207 lines
5.5 KiB
207 lines
5.5 KiB
2 years ago
|
import Cookies from 'js-cookie';
|
||
2 years ago
|
import { useEffect, useRef, useState } from 'preact/hooks';
|
||
2 years ago
|
import { TOKEN_COOKIE_NAME } from '../../lib/jwt';
|
||
|
|
||
|
interface PreviewFile extends File {
|
||
|
preview: string;
|
||
|
}
|
||
2 years ago
|
|
||
|
type UploadProfilePictureProps = {
|
||
|
avatarUrl: string;
|
||
|
};
|
||
|
|
||
|
function getDimensions(file: File) {
|
||
|
return new Promise<{
|
||
|
width: number;
|
||
|
height: number;
|
||
|
}>((resolve) => {
|
||
|
const img = new Image();
|
||
|
|
||
|
img.onload = () => {
|
||
|
resolve({ width: img.width, height: img.height });
|
||
|
};
|
||
|
|
||
|
img.onerror = () => {
|
||
|
resolve({ width: 0, height: 0 });
|
||
|
};
|
||
|
|
||
|
img.src = URL.createObjectURL(file);
|
||
|
});
|
||
|
}
|
||
|
|
||
|
async function validateImage(file: File): Promise<string | null> {
|
||
|
const dimensions = await getDimensions(file);
|
||
|
|
||
|
if (dimensions.width > 3000 || dimensions.height > 3000) {
|
||
|
return 'Image dimensions are too big. Maximum 3000x3000 pixels.';
|
||
|
}
|
||
|
|
||
|
if (dimensions.width < 100 || dimensions.height < 100) {
|
||
|
return 'Image dimensions are too small. Minimum 100x100 pixels.';
|
||
|
}
|
||
|
|
||
|
if (file.size > 1024 * 1024) {
|
||
|
return 'Image size is too big. Maximum 1MB.';
|
||
|
}
|
||
|
|
||
|
return null;
|
||
|
}
|
||
|
|
||
|
export default function UploadProfilePicture(props: UploadProfilePictureProps) {
|
||
|
const { avatarUrl } = props;
|
||
|
|
||
2 years ago
|
const [file, setFile] = useState<PreviewFile | null>(null);
|
||
|
const [error, setError] = useState('');
|
||
|
const [isLoading, setIsLoading] = useState(false);
|
||
2 years ago
|
|
||
2 years ago
|
const inputRef = useRef<HTMLInputElement>(null);
|
||
|
|
||
2 years ago
|
const onImageChange = async (e: Event) => {
|
||
2 years ago
|
setError('');
|
||
2 years ago
|
|
||
2 years ago
|
const file = (e.target as HTMLInputElement).files?.[0];
|
||
2 years ago
|
if (!file) {
|
||
2 years ago
|
return;
|
||
|
}
|
||
|
|
||
2 years ago
|
const error = await validateImage(file);
|
||
|
if (error) {
|
||
|
setError(error);
|
||
2 years ago
|
return;
|
||
|
}
|
||
|
|
||
|
setFile(
|
||
|
Object.assign(file, {
|
||
|
preview: URL.createObjectURL(file),
|
||
|
})
|
||
|
);
|
||
|
};
|
||
|
|
||
|
const handleSubmit = async (e: Event) => {
|
||
|
e.preventDefault();
|
||
|
setError('');
|
||
|
setIsLoading(true);
|
||
2 years ago
|
|
||
|
if (!file) {
|
||
|
return;
|
||
|
}
|
||
2 years ago
|
|
||
|
const formData = new FormData();
|
||
|
formData.append('name', 'avatar');
|
||
|
formData.append('avatar', file);
|
||
2 years ago
|
|
||
|
// FIXME: Use `httpCall` helper instead of fetch
|
||
2 years ago
|
const res = await fetch(
|
||
|
`${import.meta.env.PUBLIC_API_URL}/v1-upload-profile-picture`,
|
||
|
{
|
||
|
method: 'POST',
|
||
|
body: formData,
|
||
|
credentials: 'include',
|
||
|
}
|
||
|
);
|
||
|
|
||
2 years ago
|
if (res.ok) {
|
||
|
window.location.reload();
|
||
|
return;
|
||
|
}
|
||
|
|
||
2 years ago
|
const data = await res.json();
|
||
|
|
||
2 years ago
|
setError(data?.message || 'Something went wrong');
|
||
|
setIsLoading(false);
|
||
|
|
||
2 years ago
|
// Logout user if token is invalid
|
||
|
if (data.status === 401) {
|
||
|
Cookies.remove(TOKEN_COOKIE_NAME);
|
||
|
window.location.reload();
|
||
|
}
|
||
|
};
|
||
|
|
||
|
useEffect(() => {
|
||
|
// Necessary to revoke the preview URL when the component unmounts for avoiding memory leaks
|
||
|
return () => {
|
||
2 years ago
|
if (file) {
|
||
|
URL.revokeObjectURL(file.preview);
|
||
|
}
|
||
2 years ago
|
};
|
||
|
}, [file]);
|
||
|
|
||
|
return (
|
||
|
<form
|
||
|
onSubmit={handleSubmit}
|
||
|
encType="multipart/form-data"
|
||
2 years ago
|
className="flex flex-col gap-2"
|
||
2 years ago
|
>
|
||
2 years ago
|
<label htmlFor="avatar" className="text-sm leading-none text-slate-500">
|
||
2 years ago
|
Profile Picture
|
||
|
</label>
|
||
2 years ago
|
<div className="mb-2 mt-2 flex items-center gap-2">
|
||
2 years ago
|
<label
|
||
|
htmlFor="avatar"
|
||
|
title="Change profile picture"
|
||
|
className="relative cursor-pointer"
|
||
|
>
|
||
2 years ago
|
<div className="relative block h-24 w-24 items-center overflow-hidden rounded-full">
|
||
2 years ago
|
<img
|
||
2 years ago
|
className="absolute inset-0 h-full w-full bg-gray-100 object-cover text-sm leading-8 text-red-700"
|
||
|
src={file?.preview || avatarUrl}
|
||
|
alt={file?.name ?? 'Error!'}
|
||
2 years ago
|
loading="lazy"
|
||
|
decoding="async"
|
||
|
onLoad={() => file && URL.revokeObjectURL(file.preview)}
|
||
|
/>
|
||
|
</div>
|
||
|
|
||
|
{!file && (
|
||
|
<button
|
||
|
type="button"
|
||
|
className="absolute bottom-1 right-0 rounded bg-gray-600 px-2 py-1 text-xs leading-none text-gray-50 ring-2 ring-white"
|
||
|
onClick={() => {
|
||
|
if (isLoading) return;
|
||
|
inputRef.current?.click();
|
||
|
}}
|
||
|
>
|
||
|
Edit
|
||
|
</button>
|
||
|
)}
|
||
|
</label>
|
||
|
<input
|
||
|
ref={inputRef}
|
||
|
id="avatar"
|
||
|
type="file"
|
||
|
name="avatar"
|
||
|
accept="image/png, image/jpeg, image/jpg, image/pjpeg"
|
||
|
className="hidden"
|
||
2 years ago
|
onChange={onImageChange}
|
||
2 years ago
|
/>
|
||
|
|
||
|
{file && (
|
||
|
<div className="ml-5 flex items-center gap-2">
|
||
|
<button
|
||
|
type="button"
|
||
|
onClick={() => {
|
||
|
setFile(null);
|
||
|
inputRef.current?.value && (inputRef.current.value = '');
|
||
|
}}
|
||
|
className="flex h-9 min-w-[96px] items-center justify-center rounded-md border border-red-300 bg-red-100 text-sm font-medium text-red-700 disabled:cursor-not-allowed disabled:opacity-60"
|
||
|
disabled={isLoading}
|
||
|
>
|
||
|
Cancel
|
||
|
</button>
|
||
|
<button
|
||
|
type="submit"
|
||
|
className="flex h-9 min-w-[96px] items-center justify-center rounded-md border border-gray-300 text-sm font-medium text-black disabled:cursor-not-allowed disabled:opacity-60"
|
||
|
disabled={isLoading}
|
||
|
>
|
||
2 years ago
|
{isLoading ? 'Uploading..' : 'Upload'}
|
||
2 years ago
|
</button>
|
||
|
</div>
|
||
|
)}
|
||
|
</div>
|
||
|
{error && (
|
||
|
<p className="mt-2 rounded-lg bg-red-100 p-2 text-red-700">{error}</p>
|
||
|
)}
|
||
|
</form>
|
||
|
);
|
||
|
}
|