Skip to content

Commit

Permalink
Improve file download for image in sessions (file name + download but…
Browse files Browse the repository at this point in the history
…ton directly on the input)
  • Loading branch information
HugoGresse committed Jun 9, 2024
1 parent 2002417 commit 60676ff
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 7 deletions.
26 changes: 23 additions & 3 deletions src/components/form/ImageTextFieldElement.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,30 +3,50 @@ import { useState } from 'react'
import { FieldValues } from 'react-hook-form/dist/types/fields'
import { TextFieldElement, TextFieldElementProps, useWatch } from 'react-hook-form-mui'
import { Box, IconButton, InputAdornment } from '@mui/material'
import { Image } from '@mui/icons-material'
import { Download, Image } from '@mui/icons-material'
import { SidePanelImageUpload } from '../sidepanel/SidePanelImageUpload'
import { Event } from '../../types'
import { triggerFileDownload } from '../../utils/triggerFileDownload'

export const ImageTextFieldElement = <TFieldValues extends FieldValues = FieldValues>({
event,
maxImageSize = 500,
fileSuffix = '',
...otherProps
}: TextFieldElementProps<TFieldValues> & { event: Event; maxImageSize?: number }) => {
}: TextFieldElementProps<TFieldValues> & { event: Event; maxImageSize?: number; fileSuffix?: string }) => {
const fieldValue = useWatch({ name: otherProps.name })
const [isSidePanelOpen, setSidePanelOpen] = useState(false)

const openSidePanel = () => {
setSidePanelOpen(true)
}

const fieldValueExtension = fieldValue?.split('.').pop()

return (
<>
<TextFieldElement
{...otherProps}
InputProps={{
endAdornment: (
<Box display="flex" alignItems="center" ml={1} sx={{ cursor: 'pointer' }}>
{fieldValue && <img src={fieldValue} width={30} onClick={openSidePanel} alt="Preview" />}
{fieldValue && (
<>
<img src={fieldValue} width={30} onClick={openSidePanel} alt="Preview" />
<IconButton
aria-label="download image"
disabled={!fieldValue}
onClick={async () => {
await triggerFileDownload(
fieldValue,
`OpenPlanner-session-${fileSuffix}.${fieldValueExtension}`
)
}}
edge="end">
<Download />
</IconButton>
</>
)}
<InputAdornment position="end">
<IconButton
aria-label="add image"
Expand Down
5 changes: 1 addition & 4 deletions src/components/sidepanel/SidePanelVideoPreview.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,8 @@ export const SidePanelVideoPreview = ({
border: '4px solid #EEE',
}}
/>
<a href={videoUrl} download={'video.mp4'}>
Download
</a>
<Button
variant="contained"
variant="outlined"
endIcon={<Download />}
disabled={!videoUrl}
sx={{ mt: 2 }}
Expand Down
2 changes: 2 additions & 0 deletions src/events/page/sessions/EventSessionForm.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -266,6 +266,7 @@ export const EventSessionForm = ({ event, session, onSubmit }: EventSessionFormP
variant="filled"
disabled={isSubmitting}
maxImageSize={1500}
fileSuffix={session?.title}
/>
</Grid>
</Grid>
Expand Down Expand Up @@ -337,6 +338,7 @@ export const EventSessionForm = ({ event, session, onSubmit }: EventSessionFormP
label="Teaser image url"
name="teaserImageUrl"
variant="filled"
fileSuffix={session?.title}
disabled={false}
/>
</Grid>
Expand Down

0 comments on commit 60676ff

Please sign in to comment.