Lab_6
This commit is contained in:
@@ -4,44 +4,15 @@ const EventForm = ({ initialData, onSubmit, onClose }) => {
|
||||
const [formData, setFormData] = useState({
|
||||
title: '',
|
||||
description: '',
|
||||
imageUrl: '',
|
||||
categoryId: 1,
|
||||
typeId: 1
|
||||
imageUrl: ''
|
||||
});
|
||||
|
||||
const [categories, setCategories] = useState([]);
|
||||
const [types, setTypes] = useState([]);
|
||||
|
||||
// Загружаем категории и типы при монтировании
|
||||
useEffect(() => {
|
||||
const fetchCategoriesAndTypes = async () => {
|
||||
try {
|
||||
const [categoriesResponse, typesResponse] = await Promise.all([
|
||||
fetch('http://localhost:8080/api/categories'),
|
||||
fetch('http://localhost:8080/api/types')
|
||||
]);
|
||||
|
||||
const categoriesData = await categoriesResponse.json();
|
||||
const typesData = await typesResponse.json();
|
||||
|
||||
setCategories(categoriesData);
|
||||
setTypes(typesData);
|
||||
} catch (error) {
|
||||
console.error('Ошибка загрузки категорий и типов:', error);
|
||||
}
|
||||
};
|
||||
|
||||
fetchCategoriesAndTypes();
|
||||
}, []);
|
||||
|
||||
useEffect(() => {
|
||||
if (initialData) {
|
||||
setFormData({
|
||||
title: initialData.title || '',
|
||||
description: initialData.description || '',
|
||||
imageUrl: initialData.imageUrl || '',
|
||||
categoryId: initialData.categoryId || 1,
|
||||
typeId: initialData.typeId || 1
|
||||
imageUrl: initialData.imageUrl || ''
|
||||
});
|
||||
}
|
||||
}, [initialData]);
|
||||
@@ -50,15 +21,32 @@ const EventForm = ({ initialData, onSubmit, onClose }) => {
|
||||
const { name, value } = e.target;
|
||||
setFormData(prev => ({
|
||||
...prev,
|
||||
[name]: name === 'categoryId' || name === 'typeId' ? parseInt(value) : value
|
||||
[name]: value
|
||||
}));
|
||||
};
|
||||
|
||||
const handleSubmit = (e) => {
|
||||
e.preventDefault();
|
||||
|
||||
const eventData = {
|
||||
title: formData.title,
|
||||
description: formData.description,
|
||||
imageUrl: formData.imageUrl,
|
||||
category: {
|
||||
id: 1,
|
||||
name: "Киберспорт",
|
||||
description: "Соревнования по компьютерным играм"
|
||||
},
|
||||
type: {
|
||||
id: 1,
|
||||
name: "Трансляция",
|
||||
description: "Прямая трансляция в реальном времени"
|
||||
}
|
||||
};
|
||||
|
||||
const dataToSubmit = initialData
|
||||
? { ...formData, id: initialData.id }
|
||||
: formData;
|
||||
? { ...eventData, id: initialData.id }
|
||||
: eventData;
|
||||
|
||||
onSubmit(dataToSubmit);
|
||||
};
|
||||
@@ -120,46 +108,6 @@ const EventForm = ({ initialData, onSubmit, onClose }) => {
|
||||
/>
|
||||
</div>
|
||||
|
||||
{/* Выбор категории */}
|
||||
<div className="mb-3">
|
||||
<label htmlFor="eventCategory" className="form-label">Категория</label>
|
||||
<select
|
||||
className="form-control"
|
||||
id="eventCategory"
|
||||
name="categoryId"
|
||||
value={formData.categoryId}
|
||||
onChange={handleChange}
|
||||
required
|
||||
>
|
||||
<option value="">Выберите категорию</option>
|
||||
{categories.map(category => (
|
||||
<option key={category.id} value={category.id}>
|
||||
{category.name}
|
||||
</option>
|
||||
))}
|
||||
</select>
|
||||
</div>
|
||||
|
||||
{/* Выбор типа */}
|
||||
<div className="mb-3">
|
||||
<label htmlFor="eventType" className="form-label">Тип</label>
|
||||
<select
|
||||
className="form-control"
|
||||
id="eventType"
|
||||
name="typeId"
|
||||
value={formData.typeId}
|
||||
onChange={handleChange}
|
||||
required
|
||||
>
|
||||
<option value="">Выберите тип</option>
|
||||
{types.map(type => (
|
||||
<option key={type.id} value={type.id}>
|
||||
{type.name}
|
||||
</option>
|
||||
))}
|
||||
</select>
|
||||
</div>
|
||||
|
||||
<div className="form-buttons">
|
||||
<button type="submit" className="btn btn-form-submit">
|
||||
{initialData ? 'Сохранить изменения' : 'Создать событие'}
|
||||
|
||||
Reference in New Issue
Block a user