fix edit
for /media
route
This commit is contained in:
parent
80bc179b69
commit
03aee7a232
@ -23,7 +23,7 @@ export const CountryCreate = () => {
|
||||
fullWidth
|
||||
InputLabelProps={{shrink: true}}
|
||||
type="text"
|
||||
label={'Code'}
|
||||
label={'Код'}
|
||||
name="code"
|
||||
/>
|
||||
<TextField
|
||||
@ -36,7 +36,7 @@ export const CountryCreate = () => {
|
||||
fullWidth
|
||||
InputLabelProps={{shrink: true}}
|
||||
type="text"
|
||||
label={'Name'}
|
||||
label={'Название'}
|
||||
name="name"
|
||||
/>
|
||||
</Box>
|
||||
|
@ -1,16 +1,32 @@
|
||||
import {Box, TextField, Button, Typography} from '@mui/material'
|
||||
import {Box, TextField, Button, Typography, FormControl, InputLabel, Select, MenuItem} from '@mui/material'
|
||||
import {Edit} from '@refinedev/mui'
|
||||
import {useForm} from '@refinedev/react-hook-form'
|
||||
import {useState, useEffect} from 'react'
|
||||
import {useShow} from '@refinedev/core'
|
||||
|
||||
import {MEDIA_TYPES} from '../../lib/constants'
|
||||
|
||||
type MediaFormValues = {
|
||||
filename: string
|
||||
media_type: number
|
||||
file?: File
|
||||
}
|
||||
|
||||
export const MediaEdit = () => {
|
||||
const {
|
||||
saveButtonProps,
|
||||
refineCore: {onFinish},
|
||||
register,
|
||||
formState: {errors},
|
||||
setValue,
|
||||
} = useForm({})
|
||||
handleSubmit,
|
||||
} = useForm<MediaFormValues>({
|
||||
defaultValues: {
|
||||
filename: '',
|
||||
media_type: '',
|
||||
file: undefined,
|
||||
},
|
||||
})
|
||||
|
||||
const {query} = useShow()
|
||||
const {data} = query
|
||||
@ -22,8 +38,10 @@ export const MediaEdit = () => {
|
||||
useEffect(() => {
|
||||
if (record?.id) {
|
||||
setPreviewUrl(`https://wn.krbl.ru/media/${record.id}/download`)
|
||||
setValue('filename', record?.filename || '')
|
||||
setValue('media_type', record?.media_type)
|
||||
}
|
||||
}, [record])
|
||||
}, [record, setValue])
|
||||
|
||||
const handleFileChange = (event: React.ChangeEvent<HTMLInputElement>) => {
|
||||
const file = event.target.files?.[0]
|
||||
@ -39,7 +57,19 @@ export const MediaEdit = () => {
|
||||
}
|
||||
|
||||
return (
|
||||
<Edit saveButtonProps={saveButtonProps}>
|
||||
<Edit
|
||||
saveButtonProps={{
|
||||
...saveButtonProps,
|
||||
onClick: handleSubmit((data) => {
|
||||
const formData = {
|
||||
filename: data.filename,
|
||||
type: Number(data.media_type),
|
||||
}
|
||||
|
||||
onFinish(formData)
|
||||
}),
|
||||
}}
|
||||
>
|
||||
<Box component="form" sx={{display: 'flex', flexDirection: 'column'}} autoComplete="off">
|
||||
<Box display="flex" flexDirection="column-reverse" alignItems="center" gap={6}>
|
||||
<Box display="flex" alignItems="center" gap={2}>
|
||||
@ -76,20 +106,30 @@ export const MediaEdit = () => {
|
||||
name="filename"
|
||||
/>
|
||||
|
||||
<TextField
|
||||
{...register('media_type', {
|
||||
required: 'Это поле является обязательным',
|
||||
valueAsNumber: true,
|
||||
})}
|
||||
error={!!(errors as any)?.media_type}
|
||||
helperText={(errors as any)?.media_type?.message}
|
||||
margin="normal"
|
||||
fullWidth
|
||||
InputLabelProps={{shrink: true}}
|
||||
type="number"
|
||||
label="Тип"
|
||||
name="media_type"
|
||||
/>
|
||||
<FormControl fullWidth margin="normal" error={!!errors.media_type}>
|
||||
<InputLabel id="media-type-label">Тип</InputLabel>
|
||||
<Select
|
||||
labelId="media-type-label"
|
||||
label="Тип"
|
||||
{...register('media_type', {
|
||||
required: 'Это поле является обязательным',
|
||||
valueAsNumber: true,
|
||||
})}
|
||||
defaultValue=""
|
||||
>
|
||||
{MEDIA_TYPES.map((option) => (
|
||||
<MenuItem key={option.value} value={option.value}>
|
||||
{option.label}
|
||||
</MenuItem>
|
||||
))}
|
||||
</Select>
|
||||
|
||||
{errors.media_type && (
|
||||
<Typography variant="caption" color="error">
|
||||
{(errors as any)?.message}
|
||||
</Typography>
|
||||
)}
|
||||
</FormControl>
|
||||
</Box>
|
||||
</Edit>
|
||||
)
|
||||
|
Loading…
Reference in New Issue
Block a user