From d5b6dd83ebb478c19522f3781b65ee82787cc11b Mon Sep 17 00:00:00 2001 From: henrihaapalasiili Date: Wed, 28 Aug 2024 12:03:58 +0300 Subject: [PATCH] KAAV-1473 add to timeline logic changed and distances between phases from data --- .../ProjectTimeline/TimelineModal.js | 4 +-- .../ProjectTimeline/VisTimelineGroup.js | 20 +++++++++++--- .../EditProjectTimetableModal/index.js | 27 +++++++++++++++---- src/utils/{scandicUtil.js => textUtil.js} | 12 +++++++++ 4 files changed, 53 insertions(+), 10 deletions(-) rename src/utils/{scandicUtil.js => textUtil.js} (52%) diff --git a/src/components/ProjectTimeline/TimelineModal.js b/src/components/ProjectTimeline/TimelineModal.js index 5b7515611..e9dd9734a 100644 --- a/src/components/ProjectTimeline/TimelineModal.js +++ b/src/components/ProjectTimeline/TimelineModal.js @@ -5,7 +5,7 @@ import { EDIT_PROJECT_TIMETABLE_FORM } from '../../constants' import FormField from '../input/FormField' import { isArray } from 'lodash' import { showField } from '../../utils/projectVisibilityUtils' -import scandicUtil from '../../utils/scandicUtil' +import textUtil from '../../utils/textUtil' import PropTypes from 'prop-types' import './VisTimeline.css' @@ -110,7 +110,7 @@ const TimelineModal = ({ open,group,content,deadlinegroup,deadlines,openDialog,v const splitTitle = title.split('-').map(part => part.toLowerCase()) splitTitle[1] = splitTitle[1] === "1" ? "" : "_"+splitTitle[1] let confirmedValue = "vahvista_"+group.toLowerCase()+"_"+splitTitle[0]+"_alkaa"+splitTitle[1] - confirmedValue = scandicUtil.replaceScandics(confirmedValue) + confirmedValue = textUtil.replaceScandics(confirmedValue) if(group === "Ehdotus" && splitTitle[0] === "nahtavillaolo"){ splitTitle[0] = "esillaolo" confirmedValue = "vahvista_"+group.toLowerCase()+"_"+splitTitle[0]+splitTitle[1] diff --git a/src/components/ProjectTimeline/VisTimelineGroup.js b/src/components/ProjectTimeline/VisTimelineGroup.js index b1ea228f1..e590e69f1 100644 --- a/src/components/ProjectTimeline/VisTimelineGroup.js +++ b/src/components/ProjectTimeline/VisTimelineGroup.js @@ -90,13 +90,27 @@ const VisTimelineGroup = forwardRef(({ groups, items, deadlines, visValues, dead let lautakuntaReason = !lautakuntaConfirmed ? "noconfirmation" : ""; if (esillaoloConfirmed) { const deadlineEsillaolokertaKeys = data.maxEsillaolo - const esillaoloRegex = new RegExp(`${phase}_esillaolo_\\d+$`); + const esillaoloRegex = new RegExp(`jarjestetaan_${phase}_esillaolo_\\d+$`); const attributeEsillaoloKeys = Object.keys(visValRef).filter(key => esillaoloRegex.test(key)); - let esillaoloCount = attributeEsillaoloKeys.length - if(attributeEsillaoloKeys.length === 0 || data.content === "OAS" || data.content === "Ehdotus"){ + let largestIndex = 0; + //find largest index + attributeEsillaoloKeys.forEach(key => { + const match = key.match(/_(\d+)$/); + if (match) { + const number = parseInt(match[1], 10); + if (number > largestIndex) { + largestIndex = number; + } + } + }); + + let esillaoloCount = largestIndex + //If no index found add one + if(esillaoloCount === 0){ esillaoloCount += 1 } esillaoloCount = esillaoloCount + 1; + canAddEsillaolo = esillaoloCount <= deadlineEsillaolokertaKeys; const nextEsillaoloStr = canAddEsillaolo ? `jarjestetaan_${phase}_esillaolo_${esillaoloCount}$` : false; nextEsillaoloClean = nextEsillaoloStr ? nextEsillaoloStr.replace(/[/$]/g, '') : nextEsillaoloStr; diff --git a/src/components/project/EditProjectTimetableModal/index.js b/src/components/project/EditProjectTimetableModal/index.js index adfa1b9ac..6232332a0 100644 --- a/src/components/project/EditProjectTimetableModal/index.js +++ b/src/components/project/EditProjectTimetableModal/index.js @@ -542,18 +542,35 @@ class EditProjectTimeTableModal extends Component { } }; - processValuesSequentially = async (matchingValues) => { + processValuesSequentially = async (matchingValues,index,phase) => { const validValues = []; let foundItem = matchingValues.find(item => item?.key?.includes("_paattyy")) || matchingValues[0].value; + // Replace all underscores with spaces + let phaseNormalized = phase.replace(/_/g, ' '); + // Trim leading and trailing spaces just in case + phaseNormalized = phaseNormalized.trim(); + // Capitalize the first character and concatenate with the rest of the string + phaseNormalized = phaseNormalized.charAt(0).toUpperCase() + phaseNormalized.slice(1); + console.log(matchingValues) + let distanceArray = [] + for (let i = 0; i < this.props.deadlineSections.length; i++) { + if(this.props.deadlineSections[i].title === phaseNormalized){ + const sections = this.props.deadlineSections[i].sections[0].attributes + for (let x = 0; x < sections.length; x++) { + distanceArray.push({"name":sections[x].name,"distance":sections[x].distance_from_previous}) + console.log(sections[x].name,sections[x]?.distance_from_previous) + } + } + } for (const { key } of matchingValues) { try { let valueToCheck //Add required range between dates let newDate = new Date(foundItem.value ? foundItem.value : foundItem); - //TODO Oikeat etäisyydet tuoda excel->backend->frontend esilläolo/lautakunta +vaihe. date_types. dateTypes - let daysToAdd = key.includes("_maaraaika") ? 15 : 30; //Replace with excel info later - daysToAdd = key.includes("_paattyy") || key.includes("viimeistaan_lausunnot") ? daysToAdd + 30 : daysToAdd; + let matchingSection = distanceArray.find(section => section.name === key) + let daysToAdd = matchingSection.distance + while (daysToAdd > 0) { newDate.setDate(newDate.getDate() + 1); const dateStr = newDate.toISOString().split('T')[0]; @@ -716,7 +733,7 @@ class EditProjectTimeTableModal extends Component { } // Process the values sequentially and return new validated values from backend // waits for all values to be ready so vis does not give error if missing start or end date - this.processValuesSequentially(matchingValues, index).then(validValues => { + this.processValuesSequentially(matchingValues, index, phase).then(validValues => { if (validValues.length >= 2) { let newIndex let indexString diff --git a/src/utils/scandicUtil.js b/src/utils/textUtil.js similarity index 52% rename from src/utils/scandicUtil.js rename to src/utils/textUtil.js index d750503f5..84fe48143 100644 --- a/src/utils/scandicUtil.js +++ b/src/utils/textUtil.js @@ -21,6 +21,18 @@ const replaceScandics = (str) => { }); } +const capitalizeAndRemoveUnderscores = (str) => { + // Replace all underscores with spaces + let formattedStr = str.replace(/_/g, ' '); + // Trim leading and trailing spaces just in case + formattedStr = formattedStr.trim(); + // Capitalize the first character and concatenate with the rest of the string + formattedStr = formattedStr.charAt(0).toUpperCase() + formattedStr.slice(1); + + return formattedStr; +} + export default { replaceScandics, + capitalizeAndRemoveUnderscores } \ No newline at end of file