From d21f0ff686561e5e8e65af4e740c9cb835c5d8ea Mon Sep 17 00:00:00 2001 From: mikkojamG Date: Wed, 12 Feb 2025 09:52:05 +0200 Subject: [PATCH 1/2] fix: hearing project fields KER-422 --- src/actions/hearingEditor.js | 2 +- src/components/admin/HearingFormStep5.jsx | 139 ++++++------------ src/components/admin/Phase.jsx | 54 +++++-- src/components/admin/Project.jsx | 129 ++++++++++++++++ .../admin/__tests__/HearingFormStep5.test.jsx | 94 ++++++++---- src/reducers/hearingEditor/hearing.js | 18 ++- 6 files changed, 293 insertions(+), 143 deletions(-) create mode 100644 src/components/admin/Project.jsx diff --git a/src/actions/hearingEditor.js b/src/actions/hearingEditor.js index 1e33a6376..6c0b07daf 100644 --- a/src/actions/hearingEditor.js +++ b/src/actions/hearingEditor.js @@ -106,7 +106,7 @@ export const deleteSectionAttachment = (sectionId, attachment) => (dispatch) => .then(() => dispatch(createAction(EditorActions.DELETE_ATTACHMENT)({ sectionId, attachment }))); }; -export const changeProject = (projectId, projectLists) => createAction(EditorActions.CHANGE_PROJECT)(projectId, projectLists); +export const changeProject = (hearingSlug, projectId, projectLists) => createAction(EditorActions.CHANGE_PROJECT)(hearingSlug, projectId, projectLists); export const updateProjectLanguage = (languages) => createAction(EditorActions.UPDATE_PROJECT_LANGUAGE)({ languages }); diff --git a/src/components/admin/HearingFormStep5.jsx b/src/components/admin/HearingFormStep5.jsx index eacfcb069..aac0bfe23 100644 --- a/src/components/admin/HearingFormStep5.jsx +++ b/src/components/admin/HearingFormStep5.jsx @@ -1,17 +1,14 @@ /* eslint-disable react/forbid-prop-types */ -import React from 'react'; +import React, { useEffect, useState } from 'react'; import PropTypes from 'prop-types'; import { v1 as uuid } from 'uuid'; import { connect, useDispatch } from 'react-redux'; import { isEmpty } from 'lodash'; -import { Button, Notification, Select, TextInput } from 'hds-react'; -import { injectIntl, FormattedMessage } from 'react-intl'; -import classNames from 'classnames'; +import { Select } from 'hds-react'; +import { injectIntl, FormattedMessage, useIntl } from 'react-intl'; -import Icon from '../../utils/Icon'; import { createNotificationPayload, NOTIFICATION_TYPES } from '../../utils/notify'; import * as ProjectsSelector from '../../selectors/projectLists'; -import Phase from './Phase'; import { hearingShape } from '../../types'; import { changeProjectName, @@ -22,17 +19,41 @@ import { changePhase, } from '../../actions/hearingEditor'; import { addToast } from '../../actions/toast'; +import Project from './Project'; -const HearingFormStep5 = ({ errors, hearing, hearingLanguages, language, projects, intl }) => { +const HearingFormStep5 = ({ errors, hearing, hearingLanguages, language, projects }) => { const dispatch = useDispatch(); + const intl = useIntl(); - const onChangeProject = (selected) => + const defaultProjectOptions = [ + { value: uuid(), label: intl.formatMessage({ id: 'noProject' }) }, + { value: '', label: intl.formatMessage({ id: 'defaultProject' }) }, + ]; + + const projectsOptions = projects.map((project) => ({ + value: project.id, + label: `${ + project.title[language] || project.title.fi || project.title.en || project.title.sv || 'Default project' + }`, + })); + + const options = [...defaultProjectOptions, ...projectsOptions]; + + const [selectedProject, setSelectedProject] = useState(hearing.project); + + useEffect(() => { + setSelectedProject(hearing.project); + }, [hearing.project]); + + const onChangeProject = (selected) => { dispatch( changeProject({ + hearingSlug: hearing.slug, projectId: selected.value, projectLists: projects, }), ); + }; const addPhase = () => { if (!isEmpty(hearingLanguages)) { @@ -51,100 +72,31 @@ const HearingFormStep5 = ({ errors, hearing, hearingLanguages, language, project const onActivePhase = (phaseId) => dispatch(activePhase(phaseId)); - const renderProject = (selectedProject) => { - const phasesLength = hearing.project ? hearing.project.phases.length : null; - const errorStyle = !errors.project_phase_active && phasesLength === 0 ? 'has-error' : null; - - return ( -
- {selectedProject && - hearingLanguages.map((usedLanguage) => ( -
- - ({usedLanguage}) - - } - maxLength={100} - value={selectedProject.title[usedLanguage]} - onBlur={(event) => onChangeProjectName(usedLanguage, event.target.value)} - invalid={!!errors.project_title} - errorText={errors.project_title} - style={{ marginBottom: 'var(--spacing-s)' }} - required - /> -
- ))} -
- {selectedProject && - selectedProject.phases.map((phase, index) => { - const key = index; - - return ( - - ); - })} -
- {selectedProject && ( -
- -
- )} - {!!errors.project_phase_active && phasesLength === 0 && ( - - {errors.project_phase_active} - - )} -
- ); - }; - - const selectedProject = hearing.project; - - const defaultProjectOptions = [ - { value: uuid(), label: intl.formatMessage({ id: 'noProject' }) }, - { value: '', label: intl.formatMessage({ id: 'defaultProject' }) }, - ]; - - const projectsOptions = projects.map((project) => ({ - value: project.id, - label: `${ - project.title[language] || project.title.fi || project.title.en || project.title.sv || 'Default project' - }`, - })); - - const options = [...defaultProjectOptions, ...projectsOptions]; - - const projectsInitialValue = selectedProject?.id ? selectedProject.id : options[0]; + const projectValue = options.find((option) => option.value === hearing.project?.id); return (