From 0f33195b26e9f0e47e0f70d9b85c460fc637a26f Mon Sep 17 00:00:00 2001 From: Roman Rodionov Date: Thu, 12 Sep 2024 11:29:34 +0200 Subject: [PATCH] Backmerge: #5313 - Creation preset without phosphate causes R3less sugars disabled in the library FOREVER (#5489) - added clearing of rna components validations in case of NOT edit mode - refactored rna builder validation --- .../RnaBuilder/RnaAccordion/RnaAccordion.tsx | 14 ++++------- .../RnaBuilder/RnaEditor/RnaEditor.tsx | 7 ++++++ .../RnaEditorExpanded/RnaEditorExpanded.tsx | 14 ++++------- .../src/helpers/rnaValidations.ts | 12 ++++++++++ .../src/state/rna-builder/rnaBuilderSlice.ts | 24 ++++++++++--------- 5 files changed, 40 insertions(+), 31 deletions(-) diff --git a/packages/ketcher-macromolecules/src/components/monomerLibrary/RnaBuilder/RnaAccordion/RnaAccordion.tsx b/packages/ketcher-macromolecules/src/components/monomerLibrary/RnaBuilder/RnaAccordion/RnaAccordion.tsx index c2b7c93779..a26d6dd501 100644 --- a/packages/ketcher-macromolecules/src/components/monomerLibrary/RnaBuilder/RnaAccordion/RnaAccordion.tsx +++ b/packages/ketcher-macromolecules/src/components/monomerLibrary/RnaBuilder/RnaAccordion/RnaAccordion.tsx @@ -42,6 +42,7 @@ import { import { Summary } from './Summary'; import { createNewPreset, + recalculateRnaBuilderValidations, RnaBuilderItem, RnaBuilderPresetsItem, selectActivePreset, @@ -51,10 +52,7 @@ import { selectIsEditMode, setActivePresetMonomerGroup, setActiveRnaBuilderItem, - setBaseValidations, setIsEditMode, - setPhosphateValidations, - setSugarValidations, } from 'state/rna-builder'; import { useDispatch } from 'react-redux'; import { IRnaPreset } from '../types'; @@ -68,7 +66,6 @@ import { selectIsSequenceEditInRNABuilderMode, } from 'state/common'; import { RnaPresetGroup } from 'components/monomerLibrary/RnaPresetGroup/RnaPresetGroup'; -import { getValidations } from 'helpers/rnaValidations'; interface IGroupsDataItem { groupName: MonomerGroups | RnaBuilderPresetsItem; @@ -108,12 +105,9 @@ export const RnaAccordion = ({ libraryName, duplicatePreset, editPreset }) => { setExpandedAccordion(null); } else { setExpandedAccordion(rnaBuilderItem); - const { sugarValidations, phosphateValidations, baseValidations } = - getValidations(newPreset); - - dispatch(setSugarValidations(sugarValidations)); - dispatch(setPhosphateValidations(phosphateValidations)); - dispatch(setBaseValidations(baseValidations)); + dispatch( + recalculateRnaBuilderValidations({ rnaPreset: newPreset, isEditMode }), + ); } }; diff --git a/packages/ketcher-macromolecules/src/components/monomerLibrary/RnaBuilder/RnaEditor/RnaEditor.tsx b/packages/ketcher-macromolecules/src/components/monomerLibrary/RnaBuilder/RnaEditor/RnaEditor.tsx index 708dda27c0..46d302b22c 100644 --- a/packages/ketcher-macromolecules/src/components/monomerLibrary/RnaBuilder/RnaEditor/RnaEditor.tsx +++ b/packages/ketcher-macromolecules/src/components/monomerLibrary/RnaBuilder/RnaEditor/RnaEditor.tsx @@ -26,6 +26,7 @@ import { import { useAppDispatch, useAppSelector } from 'hooks'; import { createNewPreset, + recalculateRnaBuilderValidations, RnaBuilderPresetsItem, selectActivePreset, selectIsEditMode, @@ -66,6 +67,12 @@ export const RnaEditor = ({ duplicatePreset }) => { dispatch(setActiveRnaBuilderItem(RnaBuilderPresetsItem.Presets)); }, [activePreset]); + useEffect(() => { + dispatch( + recalculateRnaBuilderValidations({ rnaPreset: activePreset, isEditMode }), + ); + }, [isEditMode]); + const expandEditor = () => { setExpanded(!expanded); if (!activePreset?.nameInList) { diff --git a/packages/ketcher-macromolecules/src/components/monomerLibrary/RnaBuilder/RnaEditor/RnaEditorExpanded/RnaEditorExpanded.tsx b/packages/ketcher-macromolecules/src/components/monomerLibrary/RnaBuilder/RnaEditor/RnaEditorExpanded/RnaEditorExpanded.tsx index f3652284c3..c508021e29 100644 --- a/packages/ketcher-macromolecules/src/components/monomerLibrary/RnaBuilder/RnaEditor/RnaEditorExpanded/RnaEditorExpanded.tsx +++ b/packages/ketcher-macromolecules/src/components/monomerLibrary/RnaBuilder/RnaEditor/RnaEditorExpanded/RnaEditorExpanded.tsx @@ -43,15 +43,13 @@ import { selectAllPresets, setActivePreset, setActiveRnaBuilderItem, - setSugarValidations, - setBaseValidations, - setPhosphateValidations, setIsEditMode, selectPresetFullName, setUniqueNameError, setSequenceSelection, setSequenceSelectionName, selectIsActivePresetNewAndEmpty, + recalculateRnaBuilderValidations, } from 'state/rna-builder'; import { useAppSelector, useLayoutMode } from 'hooks'; import { @@ -72,7 +70,6 @@ import { } from 'components/monomerLibrary/RnaBuilder/RnaEditor/RnaEditorExpanded/helpers'; import { openModal } from 'state/modal'; import { getCountOfNucleoelements } from 'helpers/countNucleoelents'; -import { getValidations } from 'helpers/rnaValidations'; type SequenceSelectionGroupNames = { [MonomerGroups.SUGARS]: string; @@ -224,12 +221,9 @@ export const RnaEditorExpanded = ({ scrollToActiveItemInLibrary(selectedGroup); dispatch(setActiveRnaBuilderItem(selectedGroup)); - const { sugarValidations, phosphateValidations, baseValidations } = - getValidations(newPreset); - - dispatch(setSugarValidations(sugarValidations)); - dispatch(setPhosphateValidations(phosphateValidations)); - dispatch(setBaseValidations(baseValidations)); + dispatch( + recalculateRnaBuilderValidations({ rnaPreset: newPreset, isEditMode }), + ); }; const onChangeName = (event: ChangeEvent) => { diff --git a/packages/ketcher-macromolecules/src/helpers/rnaValidations.ts b/packages/ketcher-macromolecules/src/helpers/rnaValidations.ts index 1c5b39bba1..03cd3061d8 100644 --- a/packages/ketcher-macromolecules/src/helpers/rnaValidations.ts +++ b/packages/ketcher-macromolecules/src/helpers/rnaValidations.ts @@ -2,6 +2,7 @@ import { IRnaPreset } from 'components/monomerLibrary/RnaBuilder/types'; export const getValidations = ( newPreset: IRnaPreset, + isEditMode: boolean, ): { sugarValidations: string[]; phosphateValidations: string[]; @@ -11,6 +12,17 @@ export const getValidations = ( const phosphateValidations: string[] = []; const baseValidations: string[] = []; + if ( + !isEditMode || + (!newPreset?.sugar && !newPreset?.phosphate && !newPreset?.base) + ) { + return { + sugarValidations, + phosphateValidations, + baseValidations, + }; + } + if (newPreset?.phosphate) { sugarValidations.push('R2'); } diff --git a/packages/ketcher-macromolecules/src/state/rna-builder/rnaBuilderSlice.ts b/packages/ketcher-macromolecules/src/state/rna-builder/rnaBuilderSlice.ts index dd62b5f7e5..07f5d4b9a4 100644 --- a/packages/ketcher-macromolecules/src/state/rna-builder/rnaBuilderSlice.ts +++ b/packages/ketcher-macromolecules/src/state/rna-builder/rnaBuilderSlice.ts @@ -32,6 +32,7 @@ import { import { transformRnaPresetToRnaLabeledPreset } from './rnaBuilderSlice.helper'; import { selectEditorPosition } from 'state/common'; import { PresetPosition } from 'ketcher-react'; +import { getValidations } from 'helpers/rnaValidations'; export enum RnaBuilderPresetsItem { Presets = 'Presets', @@ -141,14 +142,17 @@ export const rnaBuilderSlice = createSlice({ ) => { state.activeRnaBuilderItem = action.payload; }, - setSugarValidations: (state, action: PayloadAction) => { - state.groupItemValidations[MonomerGroups.SUGARS] = action.payload; - }, - setBaseValidations: (state, action: PayloadAction) => { - state.groupItemValidations[MonomerGroups.BASES] = action.payload; - }, - setPhosphateValidations: (state, action: PayloadAction) => { - state.groupItemValidations[MonomerGroups.PHOSPHATES] = action.payload; + recalculateRnaBuilderValidations: ( + state, + action: PayloadAction<{ rnaPreset: IRnaPreset; isEditMode: boolean }>, + ) => { + const { sugarValidations, phosphateValidations, baseValidations } = + getValidations(action.payload.rnaPreset, action.payload.isEditMode); + + state.groupItemValidations[MonomerGroups.SUGARS] = sugarValidations; + state.groupItemValidations[MonomerGroups.BASES] = baseValidations; + state.groupItemValidations[MonomerGroups.PHOSPHATES] = + phosphateValidations; }, setActivePresetMonomerGroup: ( state, @@ -506,9 +510,7 @@ export const { setIsSequenceFirstsOnlyNucleoelementsSelected, setActivePresetName, setActiveRnaBuilderItem, - setSugarValidations, - setBaseValidations, - setPhosphateValidations, + recalculateRnaBuilderValidations, setActivePresetMonomerGroup, savePreset, deletePreset,