@chakra-ui/icons#WarningIcon TypeScript Examples
The following examples show how to use
@chakra-ui/icons#WarningIcon.
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: AvatarInput.tsx From coindrop with GNU General Public License v3.0 | 4 votes |
AvatarInput: FunctionComponent = () => {
const inputRef = useRef<FileInputRef>(null);
const { piggybankDbData } = useContext(PublicPiggybankDataContext);
const currentAvatarStorageId = piggybankDbData.avatar_storage_id;
const { query: { piggybankName: piggybankNameQuery } } = useRouter();
const piggybankName = typeof piggybankNameQuery === 'string' ? piggybankNameQuery : piggybankNameQuery[0];
const { user } = useUser();
const uid = user?.id;
const piggybankRef = db.collection('piggybanks').doc(piggybankName);
const fileSizeError = "Image too large";
const contentTypeError = "Only images are accepted";
const imageDimensionsError = "Image height and width must be >= 250px";
const [fileSelectErrorMessage, setFileSelectErrorMessage] = useState("");
function clearInput() { inputRef.current.value = null; }
const setAvatar = async (newAvatarStorageId) => {
Promise.all([
piggybankRef.set({ avatar_storage_id: newAvatarStorageId }, { merge: true }),
deleteImage({
storageId: currentAvatarStorageId,
ownerUid: uid,
piggybankName,
}),
]);
mutate(['publicPiggybankData', piggybankName], { ...piggybankDbData, avatar_storage_id: newAvatarStorageId });
};
const onInputChange = async (event: React.ChangeEvent<HTMLInputElement>) => {
setFileSelectErrorMessage(null);
const file = event?.target?.files?.[0];
const storageRef = storage.ref();
const newAvatarStorageId = uuidV4();
const newAvatarPath = piggybankImageStoragePath({ ownerUid: uid, piggybankName, imageAs: "avatar", imageStorageId: newAvatarStorageId });
const newAvatarRef = storageRef.child(newAvatarPath);
if (file) {
try {
const contentType = file.type;
if (!contentType.startsWith("image/")) {
throw new Error(contentTypeError);
}
if (file.size > 1000000) {
throw new Error(fileSizeError);
}
const { width, height } = await getImageDimensions(file);
if (width < 250 || height < 250) {
throw new Error(imageDimensionsError);
}
await newAvatarRef.put(file);
clearInput();
setAvatar(newAvatarStorageId);
} catch (err) {
const { message } = err;
if (message === fileSizeError) {
setFileSelectErrorMessage("Image too large. Please resize image to < 1MB.");
} else if (message === contentTypeError) {
setFileSelectErrorMessage("Only .jpg, .png, and .webp images are accepted.");
} else if (message === imageDimensionsError) {
setFileSelectErrorMessage("Width and height must be at least 250px");
} else {
setFileSelectErrorMessage("Error during upload. Please try again.");
}
clearInput();
}
}
};
return (
<>
<FormLabel htmlFor="avatar-input">Image</FormLabel>
<Stack id="avatar-input-container">
<Box mx="auto">
{
currentAvatarStorageId
? <Avatar />
: (
<NextImage
id="avatar-img"
width={200}
height={200}
src="/avatar-placeholder.png"
alt="avatar placeholder"
data-cy="avatar-placeholder"
/>
)
}
</Box>
<Center>
<Flex wrap="wrap" justify="center">
<Box mt={1}>
<FileInput
text={currentAvatarStorageId ? "Upload new image" : "Upload image"}
id="avatar-input"
ref={inputRef}
accept="image/png, image/jpeg, image/webp"
onChange={onInputChange}
/>
</Box>
{currentAvatarStorageId && (
<Box ml={2} mt={1}>
<Button
onClick={() => {
setAvatar(null);
}}
colorScheme="red"
size="sm"
leftIcon={<DeleteIcon />}
data-cy="remove-image-btn"
>
Remove image
</Button>
</Box>
)}
</Flex>
</Center>
{fileSelectErrorMessage && (
<Text textAlign="center" color="red.500">
<WarningIcon mr={2} />
{fileSelectErrorMessage}
</Text>
)}
</Stack>
</>
);
}