/
TimurIsm
/
OpenEyes
Обзор
Документация
Войти
/
TimurIsm
/
OpenEyes
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
CI/CD
Аналитика
Безопасность
main
front/src/adminOption/CreateTest.jsx
97 строк
3 KB
TimurIsm
first_commit
16 май 2026, 15:34
16 май 2026, 15:34
4d3f198
Код
Авторство
О чём код?
import React, { useState, useContext } from 'react'; import { observer } from 'mobx-react-lite'; import { Context } from '../main'; import { createTest, fetchTests } from '../http/testsAPI'; import TestForm from '../components/TestForm'; import { useTestForm } from '../hooks/useTestForm'; const CreateTest = observer(() => { const { tests, user } = useContext(Context); const { formData, alertMessage, setAlertMessage, handleInputChange, handleResultChange, getResultText, validateImagePath, handleMainImageFileChange, handleMainImageManualChange, handleQuestionFileChange, handleQuestionChange, handleAddQuestion, handleRemoveQuestion, validateForm, resetForm, } = useTestForm(); const [localAlert, setLocalAlert] = useState(null); const checkTestExists = async (type) => { const testsList = await fetchTests(); return testsList.some(test => test.type.toLowerCase() === type.toLowerCase()); }; const handleSubmit = async () => { const errors = validateForm(); if (errors.length > 0) { setAlertMessage({ type: 'error', message: errors[0] }); return; } try { const testExists = await checkTestExists(formData.type); if (testExists) { setAlertMessage({ type: 'error', message: 'Тест с таким названием уже существует!' }); return; } const testData = { ...formData, adminId: user.user.id, }; await tests.createTest(testData); setLocalAlert({ type: 'success', message: 'Новый тест успешно добавлен!' }); resetForm(); setTimeout(() => { setLocalAlert(null); }, 3000); } catch (error) { setAlertMessage({ type: 'error', message: 'Ошибка при создании теста!' }); console.error('Ошибка при создании теста:', error); } }; const combinedAlert = localAlert || alertMessage; return ( <div className="flex items-center justify-center p-4"> <div className="space-y-4 w-full max-w-2xl"> <TestForm formData={formData} alertMessage={combinedAlert} mode="create" onSubmit={handleSubmit} onCancel={() => {}} onInputChange={handleInputChange} onResultChange={handleResultChange} getResultText={getResultText} onMainImageFileChange={handleMainImageFileChange} onMainImageManualChange={handleMainImageManualChange} onQuestionChange={handleQuestionChange} onQuestionFileChange={handleQuestionFileChange} onAddQuestion={handleAddQuestion} onRemoveQuestion={handleRemoveQuestion} validateImagePath={validateImagePath} submitButtonText="Создать тест" showCancelButton={false} /> </div> </div> ); }); export default CreateTest;