-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Upload and preview photo in AddOrEditNoteFlow
- Loading branch information
Showing
5 changed files
with
105 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
56 changes: 56 additions & 0 deletions
56
src/frontend/src/features/note/addEdit/ui/NoteInputFromPhotoFlow.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
import { Box, ImageList, ImageListItem, ImageListItemBar, Stack } from '@mui/material'; | ||
import { useState, type FC } from 'react'; | ||
import { convertToBase64String, resizeImage } from '@/shared/lib'; | ||
import { UploadButton } from '@/shared/ui'; | ||
import { type UploadedPhoto } from '../model'; | ||
|
||
interface Props { | ||
onUploadSuccess: (file: File) => Promise<void>; | ||
} | ||
|
||
export const NoteInputFromPhotoFlow: FC<Props> = ({ onUploadSuccess }) => { | ||
const [uploadedPhotos, setUploadedPhotos] = useState<UploadedPhoto[]>([]); | ||
|
||
const handleUpload = async (file: File): Promise<void> => { | ||
const base64 = await convertToBase64String(file); | ||
const resizedFile = await resizeImage(base64, 512, file.name); | ||
const resizedBase64 = await convertToBase64String(resizedFile); | ||
|
||
setUploadedPhotos([ | ||
{ | ||
src: resizedBase64, | ||
name: file.name, | ||
file, | ||
}, | ||
]); | ||
|
||
await onUploadSuccess(resizedFile); | ||
}; | ||
|
||
if (uploadedPhotos.length === 0) { | ||
return ( | ||
<UploadButton name="photos" accept="image/*" onUpload={handleUpload}> | ||
Upload photo | ||
</UploadButton> | ||
); | ||
} | ||
|
||
return ( | ||
<Stack spacing={3}> | ||
<ImageList cols={2} gap={16}> | ||
{uploadedPhotos.map((photo, index) => ( | ||
<ImageListItem key={index}> | ||
<Box | ||
component="img" | ||
height="194px" | ||
src={photo.src} | ||
alt={photo.name} | ||
sx={{ objectFit: 'cover' }} | ||
/> | ||
<ImageListItemBar title={photo.name} /> | ||
</ImageListItem> | ||
))} | ||
</ImageList> | ||
</Stack> | ||
); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
import { Button, styled } from '@mui/material'; | ||
import { visuallyHidden } from '@mui/utils'; | ||
import { type PropsWithChildren, type ChangeEventHandler, type FC } from 'react'; | ||
|
||
const FileInputStyled = styled('input')(() => ({ ...visuallyHidden })); | ||
|
||
interface Props { | ||
name: string; | ||
accept: string; | ||
onUpload: (file: File) => Promise<void>; | ||
} | ||
|
||
export const UploadButton: FC<PropsWithChildren<Props>> = ({ | ||
children, | ||
name, | ||
accept, | ||
onUpload, | ||
}) => { | ||
const handleUpload: ChangeEventHandler<HTMLInputElement> = async event => { | ||
try { | ||
const file = event.target?.files?.item(0); | ||
|
||
if (file) { | ||
await onUpload(file); | ||
} | ||
} finally { | ||
event.target.value = ''; | ||
} | ||
}; | ||
|
||
return ( | ||
<Button role={undefined} component="label" variant="outlined" fullWidth> | ||
{children} | ||
<FileInputStyled type="file" name={name} accept={accept} onChange={handleUpload} /> | ||
</Button> | ||
); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters