сделала год в проект оптимальным

This commit is contained in:
PotapovaA 2026-07-03 15:07:44 +03:00
parent 6f0b3eb0cc
commit 88e12d1a8b
3 changed files with 18 additions and 25 deletions

View File

@ -12,7 +12,7 @@ export const DEFAULT_PROJECT_CONFIG = {
defaultValue: null, defaultValue: null,
min: 0, min: 0,
step: 1, step: 1,
required_field: true, required_field: false,
}, },
'Тип проекта развития': { 'Тип проекта развития': {
type: 'multiselect', type: 'multiselect',

View File

@ -1,4 +1,3 @@
// src/components/common/ProjectCardComponent/ModalCreateProject.jsx
import { useState, useEffect } from 'react'; import { useState, useEffect } from 'react';
import { Box, Button, Divider, TextField, Typography } from '@mui/material'; import { Box, Button, Divider, TextField, Typography } from '@mui/material';
import SelectWithOptions from '../AdminPanel/components/SelectWithOptions'; import SelectWithOptions from '../AdminPanel/components/SelectWithOptions';
@ -11,43 +10,41 @@ export const ModalCreateProject = ({
onCreate, onCreate,
config = {}, config = {},
initialData = null, initialData = null,
isEdit = false, title = 'Создание проекта',
projectId = null, buttonText = 'Создать',
}) => { }) => {
const [projectData, setProjectData] = useState(() => { const [projectData, setProjectData] = useState(() => {
// Если есть initialData, используем её, иначе создаем из конфига
if (initialData) { if (initialData) {
return { ...initialData }; return { ...initialData };
} }
const initial = {}; // Иначе создаем из конфига
const data = {};
Object.keys(config).forEach((key) => { Object.keys(config).forEach((key) => {
initial[key] = config[key].defaultValue; data[key] = config[key].defaultValue;
}); });
return initial; return data;
}); });
const [errors, setErrors] = useState({}); const [errors, setErrors] = useState({});
// Обновляем данные при изменении initialData
useEffect(() => { useEffect(() => {
if (initialData) { if (initialData) {
setProjectData({ ...initialData }); setProjectData({ ...initialData });
} else { } else {
const initial = {}; const data = {};
Object.keys(config).forEach((key) => { Object.keys(config).forEach((key) => {
initial[key] = config[key].defaultValue; data[key] = config[key].defaultValue;
}); });
setProjectData(initial); setProjectData(data);
} }
setErrors({}); setErrors({});
}, [initialData, config]); }, [initialData, config, open]);
const handleChange = (field, value) => { const handleChange = (field, value) => {
setProjectData((prev) => ({ setProjectData((prev) => ({
...prev, ...prev,
[field]: value, [field]: value,
})); }));
// Очищаем ошибку для поля при изменении
if (errors[field]) { if (errors[field]) {
setErrors((prev) => ({ setErrors((prev) => ({
...prev, ...prev,
@ -59,8 +56,7 @@ export const ModalCreateProject = ({
const validateField = (fieldName, config) => { const validateField = (fieldName, config) => {
if (config.required_field) { if (config.required_field) {
const value = projectData[fieldName]; const value = projectData[fieldName];
// Проверка для разных типов полей
if (config.type === 'multiselect') { if (config.type === 'multiselect') {
if (!value || (Array.isArray(value) && value.length === 0) || value === '') { if (!value || (Array.isArray(value) && value.length === 0) || value === '') {
return `Поле "${fieldName}" обязательно для заполнения`; return `Поле "${fieldName}" обязательно для заполнения`;
@ -73,11 +69,9 @@ export const ModalCreateProject = ({
if (value === null || value === undefined || value === '') { if (value === null || value === undefined || value === '') {
return `Поле "${fieldName}" обязательно для заполнения`; return `Поле "${fieldName}" обязательно для заполнения`;
} }
// Проверка на минимальное значение
if (config.min !== undefined && Number(value) < config.min) { if (config.min !== undefined && Number(value) < config.min) {
return `Значение должно быть не меньше ${config.min}`; return `Значение должно быть не меньше ${config.min}`;
} }
// Проверка на максимальное значение
if (config.max !== undefined && Number(value) > config.max) { if (config.max !== undefined && Number(value) > config.max) {
return `Значение должно быть не больше ${config.max}`; return `Значение должно быть не больше ${config.max}`;
} }
@ -105,7 +99,7 @@ export const ModalCreateProject = ({
const handleClickCreate = () => { const handleClickCreate = () => {
if (validateAllFields()) { if (validateAllFields()) {
onCreate(projectData); onCreate(projectData);
// Не закрываем модалку здесь, пусть это делает родительский компонент onClose();
} }
}; };
@ -225,7 +219,6 @@ export const ModalCreateProject = ({
value={projectData[fieldName] || ''} value={projectData[fieldName] || ''}
onChange={(e) => { onChange={(e) => {
const inputValue = e.target.value; const inputValue = e.target.value;
//только числа
const isValidInput = const isValidInput =
/^\d*\.?\d*$/.test(inputValue) || inputValue === ''; /^\d*\.?\d*$/.test(inputValue) || inputValue === '';
if (isValidInput) { if (isValidInput) {
@ -260,7 +253,7 @@ export const ModalCreateProject = ({
return ( return (
<Modal <Modal
title={isEdit ? 'Редактирование проекта' : 'Создание проекта'} title={title}
open={open} open={open}
onClose={handleClose} onClose={handleClose}
customSize={'43.75'} customSize={'43.75'}
@ -278,7 +271,7 @@ export const ModalCreateProject = ({
onClick={handleClickCreate} onClick={handleClickCreate}
style={{ height: '2.5rem' }} style={{ height: '2.5rem' }}
> >
{isEdit ? 'Сохранить' : 'Создать'} {buttonText}
</PrimaryButton> </PrimaryButton>
</> </>
} }

View File

@ -151,7 +151,7 @@ export const ProjectCardComponent = ({
id, id,
name, name,
org_unit_name, org_unit_name,
year, years,
status, status,
level, level,
project_type, project_type,
@ -180,7 +180,7 @@ export const ProjectCardComponent = ({
const prepareProjectDataForEdit = () => { const prepareProjectDataForEdit = () => {
return { return {
'ССП/РФ': project.org_unit_id || null, 'ССП/РФ': project.org_unit_id || null,
'Год': year || null, 'Год': years || null,
'Тип проекта развития': project_type || null, 'Тип проекта развития': project_type || null,
'Название проекта': currentName, 'Название проекта': currentName,
'Формат ВСП': vsp_format || null, 'Формат ВСП': vsp_format || null,
@ -316,7 +316,7 @@ export const ProjectCardComponent = ({
> >
<CardBody> <CardBody>
<IconWithContent icon={<DateIcon />}> <IconWithContent icon={<DateIcon />}>
{year || 'Год не указан'} {years.join(', ') || 'Год не указан'}
</IconWithContent> </IconWithContent>
<IconWithContent icon={<UserIcon />}> <IconWithContent icon={<UserIcon />}>
{org_unit_name || 'Организация не указана'} {org_unit_name || 'Организация не указана'}