Skip to content

Commit

Permalink
Backmerge: #5313 - Creation preset without phosphate causes R3less su…
Browse files Browse the repository at this point in the history
…gars disabled in the library FOREVER (#5489)

- added clearing of rna components validations in case of NOT edit mode
- refactored rna builder validation
  • Loading branch information
rrodionov91 authored and Guch1g0v committed Oct 17, 2024
1 parent efb8c13 commit 0f33195
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 31 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ import {
import { Summary } from './Summary';
import {
createNewPreset,
recalculateRnaBuilderValidations,
RnaBuilderItem,
RnaBuilderPresetsItem,
selectActivePreset,
Expand All @@ -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';
Expand All @@ -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;
Expand Down Expand Up @@ -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 }),
);
}
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import {
import { useAppDispatch, useAppSelector } from 'hooks';
import {
createNewPreset,
recalculateRnaBuilderValidations,
RnaBuilderPresetsItem,
selectActivePreset,
selectIsEditMode,
Expand Down Expand Up @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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;
Expand Down Expand Up @@ -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<HTMLInputElement>) => {
Expand Down
12 changes: 12 additions & 0 deletions packages/ketcher-macromolecules/src/helpers/rnaValidations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { IRnaPreset } from 'components/monomerLibrary/RnaBuilder/types';

export const getValidations = (
newPreset: IRnaPreset,
isEditMode: boolean,
): {
sugarValidations: string[];
phosphateValidations: string[];
Expand All @@ -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');
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down Expand Up @@ -141,14 +142,17 @@ export const rnaBuilderSlice = createSlice({
) => {
state.activeRnaBuilderItem = action.payload;
},
setSugarValidations: (state, action: PayloadAction<string[]>) => {
state.groupItemValidations[MonomerGroups.SUGARS] = action.payload;
},
setBaseValidations: (state, action: PayloadAction<string[]>) => {
state.groupItemValidations[MonomerGroups.BASES] = action.payload;
},
setPhosphateValidations: (state, action: PayloadAction<string[]>) => {
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,
Expand Down Expand Up @@ -506,9 +510,7 @@ export const {
setIsSequenceFirstsOnlyNucleoelementsSelected,
setActivePresetName,
setActiveRnaBuilderItem,
setSugarValidations,
setBaseValidations,
setPhosphateValidations,
recalculateRnaBuilderValidations,
setActivePresetMonomerGroup,
savePreset,
deletePreset,
Expand Down

0 comments on commit 0f33195

Please sign in to comment.