front-projects #27
@ -12,7 +12,7 @@ export const DEFAULT_PROJECT_CONFIG = {
|
||||
defaultValue: null,
|
||||
min: 0,
|
||||
step: 1,
|
||||
required_field: true,
|
||||
required_field: false,
|
||||
},
|
||||
'Тип проекта развития': {
|
||||
type: 'multiselect',
|
||||
|
||||
@ -1,4 +1,3 @@
|
||||
// src/components/common/ProjectCardComponent/ModalCreateProject.jsx
|
||||
import { useState, useEffect } from 'react';
|
||||
import { Box, Button, Divider, TextField, Typography } from '@mui/material';
|
||||
import SelectWithOptions from '../AdminPanel/components/SelectWithOptions';
|
||||
@ -11,43 +10,41 @@ export const ModalCreateProject = ({
|
||||
onCreate,
|
||||
config = {},
|
||||
initialData = null,
|
||||
isEdit = false,
|
||||
projectId = null,
|
||||
title = 'Создание проекта',
|
||||
buttonText = 'Создать',
|
||||
}) => {
|
||||
const [projectData, setProjectData] = useState(() => {
|
||||
// Если есть initialData, используем её, иначе создаем из конфига
|
||||
if (initialData) {
|
||||
return { ...initialData };
|
||||
}
|
||||
const initial = {};
|
||||
// Иначе создаем из конфига
|
||||
const data = {};
|
||||
Object.keys(config).forEach((key) => {
|
||||
initial[key] = config[key].defaultValue;
|
||||
data[key] = config[key].defaultValue;
|
||||
});
|
||||
return initial;
|
||||
return data;
|
||||
});
|
||||
|
||||
const [errors, setErrors] = useState({});
|
||||
|
||||
// Обновляем данные при изменении initialData
|
||||
useEffect(() => {
|
||||
if (initialData) {
|
||||
setProjectData({ ...initialData });
|
||||
} else {
|
||||
const initial = {};
|
||||
const data = {};
|
||||
Object.keys(config).forEach((key) => {
|
||||
initial[key] = config[key].defaultValue;
|
||||
data[key] = config[key].defaultValue;
|
||||
});
|
||||
setProjectData(initial);
|
||||
setProjectData(data);
|
||||
}
|
||||
setErrors({});
|
||||
}, [initialData, config]);
|
||||
}, [initialData, config, open]);
|
||||
|
||||
const handleChange = (field, value) => {
|
||||
setProjectData((prev) => ({
|
||||
...prev,
|
||||
[field]: value,
|
||||
}));
|
||||
// Очищаем ошибку для поля при изменении
|
||||
if (errors[field]) {
|
||||
setErrors((prev) => ({
|
||||
...prev,
|
||||
@ -59,8 +56,7 @@ export const ModalCreateProject = ({
|
||||
const validateField = (fieldName, config) => {
|
||||
if (config.required_field) {
|
||||
const value = projectData[fieldName];
|
||||
|
||||
// Проверка для разных типов полей
|
||||
|
||||
if (config.type === 'multiselect') {
|
||||
if (!value || (Array.isArray(value) && value.length === 0) || value === '') {
|
||||
return `Поле "${fieldName}" обязательно для заполнения`;
|
||||
@ -73,11 +69,9 @@ export const ModalCreateProject = ({
|
||||
if (value === null || value === undefined || value === '') {
|
||||
return `Поле "${fieldName}" обязательно для заполнения`;
|
||||
}
|
||||
// Проверка на минимальное значение
|
||||
if (config.min !== undefined && Number(value) < config.min) {
|
||||
return `Значение должно быть не меньше ${config.min}`;
|
||||
}
|
||||
// Проверка на максимальное значение
|
||||
if (config.max !== undefined && Number(value) > config.max) {
|
||||
return `Значение должно быть не больше ${config.max}`;
|
||||
}
|
||||
@ -105,7 +99,7 @@ export const ModalCreateProject = ({
|
||||
const handleClickCreate = () => {
|
||||
if (validateAllFields()) {
|
||||
onCreate(projectData);
|
||||
// Не закрываем модалку здесь, пусть это делает родительский компонент
|
||||
onClose();
|
||||
}
|
||||
};
|
||||
|
||||
@ -225,7 +219,6 @@ export const ModalCreateProject = ({
|
||||
value={projectData[fieldName] || ''}
|
||||
onChange={(e) => {
|
||||
const inputValue = e.target.value;
|
||||
//только числа
|
||||
const isValidInput =
|
||||
/^\d*\.?\d*$/.test(inputValue) || inputValue === '';
|
||||
if (isValidInput) {
|
||||
@ -260,7 +253,7 @@ export const ModalCreateProject = ({
|
||||
|
||||
return (
|
||||
<Modal
|
||||
title={isEdit ? 'Редактирование проекта' : 'Создание проекта'}
|
||||
title={title}
|
||||
open={open}
|
||||
onClose={handleClose}
|
||||
customSize={'43.75'}
|
||||
@ -278,7 +271,7 @@ export const ModalCreateProject = ({
|
||||
onClick={handleClickCreate}
|
||||
style={{ height: '2.5rem' }}
|
||||
>
|
||||
{isEdit ? 'Сохранить' : 'Создать'}
|
||||
{buttonText}
|
||||
</PrimaryButton>
|
||||
</>
|
||||
}
|
||||
|
||||
@ -151,7 +151,7 @@ export const ProjectCardComponent = ({
|
||||
id,
|
||||
name,
|
||||
org_unit_name,
|
||||
year,
|
||||
years,
|
||||
status,
|
||||
level,
|
||||
project_type,
|
||||
@ -180,7 +180,7 @@ export const ProjectCardComponent = ({
|
||||
const prepareProjectDataForEdit = () => {
|
||||
return {
|
||||
'ССП/РФ': project.org_unit_id || null,
|
||||
'Год': year || null,
|
||||
'Год': years || null,
|
||||
'Тип проекта развития': project_type || null,
|
||||
'Название проекта': currentName,
|
||||
'Формат ВСП': vsp_format || null,
|
||||
@ -316,7 +316,7 @@ export const ProjectCardComponent = ({
|
||||
>
|
||||
<CardBody>
|
||||
<IconWithContent icon={<DateIcon />}>
|
||||
{year || 'Год не указан'}
|
||||
{years.join(', ') || 'Год не указан'}
|
||||
</IconWithContent>
|
||||
<IconWithContent icon={<UserIcon />}>
|
||||
{org_unit_name || 'Организация не указана'}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user