-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add folder redesign amendments (#1207)
* feat: add description label * feat: hide ThumbnailSetting input * feat: modify tab label text * feat: add folder thumbnail comp * feat: add FolderForm * feat: modify NewItemModal to update folder with thumbnail * feat: modify thumbnail style * fix: change how the folder thumbnail is handled * fix: make proposal * fix: make changes * fix: thumbnail test not passing --------- Co-authored-by: ztlee042 <[email protected]>
- Loading branch information
Showing
18 changed files
with
369 additions
and
172 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
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
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
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,47 @@ | ||
import { Stack } from '@mui/material'; | ||
|
||
import { DiscriminatedItem } from '@graasp/sdk'; | ||
|
||
import { FOLDER_FORM_DESCRIPTION_ID } from '../../../config/selectors'; | ||
import DescriptionForm from './DescriptionForm'; | ||
import FolderThumbnail from './FolderThumbnail'; | ||
import NameForm from './NameForm'; | ||
|
||
export type FolderFormProps = { | ||
item?: DiscriminatedItem; | ||
setChanges: ( | ||
payload: Partial<DiscriminatedItem> & { thumbnail?: Blob }, | ||
) => void; | ||
updatedProperties: Partial<DiscriminatedItem> & { thumbnail?: Blob }; | ||
}; | ||
|
||
const FolderForm = ({ | ||
item, | ||
updatedProperties, | ||
setChanges, | ||
}: FolderFormProps): JSX.Element => ( | ||
<Stack direction="column" gap={2}> | ||
<Stack | ||
direction="row" | ||
justifyContent="flex-start" | ||
alignItems="flex-end" | ||
gap={3} | ||
> | ||
<FolderThumbnail setChanges={setChanges} /> | ||
<NameForm | ||
required | ||
setChanges={setChanges} | ||
item={item} | ||
updatedProperties={updatedProperties} | ||
/> | ||
</Stack> | ||
<DescriptionForm | ||
id={FOLDER_FORM_DESCRIPTION_ID} | ||
item={item} | ||
updatedProperties={updatedProperties} | ||
setChanges={setChanges} | ||
/> | ||
</Stack> | ||
); | ||
|
||
export default FolderForm; |
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,140 @@ | ||
import { FormEventHandler, useRef, useState } from 'react'; | ||
|
||
import { Dialog, Stack, styled, useTheme } from '@mui/material'; | ||
|
||
import { DiscriminatedItem } from '@graasp/sdk'; | ||
|
||
import { ImageUp as ImageUpIcon } from 'lucide-react'; | ||
|
||
import CropModal, { | ||
MODAL_TITLE_ARIA_LABEL_ID, | ||
} from '@/components/common/CropModal'; | ||
|
||
const THUMBNAIL_DIMENSION = 60; | ||
|
||
const VisuallyHiddenInput = styled('input')({ | ||
clip: 'rect(0 0 0 0)', | ||
clipPath: 'inset(50%)', | ||
height: 1, | ||
overflow: 'hidden', | ||
position: 'absolute', | ||
bottom: 0, | ||
left: 0, | ||
whiteSpace: 'nowrap', | ||
width: 1, | ||
}); | ||
|
||
export type FolderThumbnailProps = { | ||
setChanges: ( | ||
payload: Partial<DiscriminatedItem> & { thumbnail?: Blob }, | ||
) => void; | ||
}; | ||
|
||
const FolderThumbnail = ({ setChanges }: FolderThumbnailProps): JSX.Element => { | ||
const inputRef = useRef<HTMLInputElement>(null); | ||
const [showCropModal, setShowCropModal] = useState(false); | ||
const [newAvatar, setNewAvatar] = useState<string>(); | ||
const [fileSource, setFileSource] = useState<string>(); | ||
const theme = useTheme(); | ||
|
||
const onSelectFile: FormEventHandler<HTMLInputElement> = (e) => { | ||
const t = e.target as HTMLInputElement; | ||
if (t.files && t.files?.length > 0) { | ||
const reader = new FileReader(); | ||
reader.addEventListener('load', () => | ||
setFileSource(reader.result as string), | ||
); | ||
reader.readAsDataURL(t.files?.[0]); | ||
setShowCropModal(true); | ||
} | ||
}; | ||
|
||
const onClose = () => { | ||
setShowCropModal(false); | ||
if (inputRef.current) { | ||
// eslint-disable-next-line @typescript-eslint/ban-ts-comment | ||
// @ts-ignore | ||
inputRef.current.value = null; | ||
} | ||
}; | ||
|
||
const onConfirmCrop = (croppedImage: Blob | null) => { | ||
onClose(); | ||
|
||
if (!croppedImage) { | ||
return console.error('croppedImage is not defined'); | ||
} | ||
// submit cropped image | ||
try { | ||
setChanges({ thumbnail: croppedImage }); | ||
// replace img src with croppedImage | ||
const url = URL.createObjectURL(croppedImage); | ||
setNewAvatar(url); | ||
} catch (error) { | ||
console.error(error); | ||
} | ||
|
||
return true; | ||
}; | ||
|
||
const onEdit = () => { | ||
inputRef.current?.click(); | ||
}; | ||
|
||
return ( | ||
<Stack justifyContent="flex-start" direction="column"> | ||
<Stack | ||
onClick={onEdit} | ||
onKeyDown={(event) => { | ||
if (['Enter', ' '].includes(event.key)) { | ||
onEdit(); | ||
} | ||
}} | ||
aria-label="change folder avatar" | ||
role="button" | ||
tabIndex={0} | ||
height={THUMBNAIL_DIMENSION} | ||
width={THUMBNAIL_DIMENSION} | ||
borderRadius={2} | ||
bgcolor="#E4DFFF" | ||
alignItems="center" | ||
justifyContent="center" | ||
overflow="hidden" | ||
position="relative" | ||
sx={{ cursor: 'pointer' }} | ||
> | ||
{newAvatar ? ( | ||
<img | ||
alt="folder thumbnail" | ||
src={newAvatar} | ||
height={THUMBNAIL_DIMENSION} | ||
width={THUMBNAIL_DIMENSION} | ||
/> | ||
) : ( | ||
<ImageUpIcon color={theme.palette.primary.main} /> | ||
)} | ||
</Stack> | ||
<VisuallyHiddenInput | ||
type="file" | ||
accept="image/*" | ||
onChange={onSelectFile} | ||
ref={inputRef} | ||
/> | ||
{fileSource && ( | ||
<Dialog | ||
open={showCropModal} | ||
onClose={onClose} | ||
aria-labelledby={MODAL_TITLE_ARIA_LABEL_ID} | ||
> | ||
<CropModal | ||
onClose={onClose} | ||
src={fileSource} | ||
onConfirm={onConfirmCrop} | ||
/> | ||
</Dialog> | ||
)} | ||
</Stack> | ||
); | ||
}; | ||
|
||
export default FolderThumbnail; |
Oops, something went wrong.