Skip to content

Commit

Permalink
- fixed problem with tags not visually updating on observations
Browse files Browse the repository at this point in the history
  • Loading branch information
boriskovar-m2ms committed Mar 4, 2024
1 parent 13b675c commit 5d3e7d1
Show file tree
Hide file tree
Showing 6 changed files with 94 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -524,28 +524,41 @@ const ObservationCmpView = memo(
setTagPopoverOpen(null);
};

const resolveTagBackgroundColor = tag => {
let color = DEFAULT_TAG_COLOR;
const resolveTagBackgroundColor = useCallback(
tag => {
let color = DEFAULT_TAG_COLOR;

if (tag.colour && tag.colour !== '') {
color = tag.colour;
} else {
const category = dispatch(getCategoryById(tag.category));
if (category) {
color = `#${category.colour}`;
if (tag.colour && tag.colour !== '') {
color = tag.colour;
} else {
const category = dispatch(getCategoryById(tag.category));
if (category) {
color = `#${category.colour}`;
}
}
}

return color;
};
return color;
},
[dispatch]
);

const resolveTagForegroundColor = tag => {
const bgColor = resolveTagBackgroundColor(tag);
return getFontColorByBackgroundColor(bgColor);
};
const resolveTagForegroundColor = useCallback(
tag => {
const bgColor = resolveTagBackgroundColor(tag);
return getFontColorByBackgroundColor(bgColor);
},
[resolveTagBackgroundColor]
);

const generateTagPopover = () => {
const generateTagPopover = useCallback(() => {
// console.log('generateTagPopover');
const allData = getAllTagsForLHSCmp(observations, tagList, tagCategories);
// console.log(
// `generateTagPopover ${observations[0].compound_code} assigned tags: ${observations[0].tags_set} count: ` +
// allData?.length +
// ' ' +
// JSON.stringify(allData)
// );
// const sortedData = [...allData].sort((a, b) => a.tag.localeCompare(b.tag));

const modifiedObjects = allData.map((obj, index) => {
Expand Down Expand Up @@ -772,7 +785,23 @@ const ObservationCmpView = memo(
</Tooltip>
</IconButton>
);
};
}, [
classes.editButtonIcon,
classes.paper,
classes.popover,
classes.tagPopover,
dispatch,
observations,
open,
resolveTagBackgroundColor,
resolveTagForegroundColor,
setRef,
tagCategories,
tagEditModalOpenNew,
tagEditorOpen,
tagList,
tagPopoverOpen
]);

// componentDidMount
useEffect(() => {
Expand Down
21 changes: 20 additions & 1 deletion js/components/preview/tags/modal/tagEditor.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { Grid, Popper, IconButton, Tooltip, makeStyles, FormControlLabel, Switch
import { Panel } from '../../../common';
import { Close } from '@material-ui/icons';
import { useDispatch, useSelector } from 'react-redux';
import { updateMoleculeInMolLists, updateMoleculeTag } from '../../../../reducers/api/actions';
import { updateLHSCompound, updateMoleculeInMolLists, updateMoleculeTag } from '../../../../reducers/api/actions';
import { getMoleculeForId } from '../redux/dispatchActions';
import {
setMoleculeForTagEdit,
Expand Down Expand Up @@ -130,6 +130,8 @@ export const TagEditor = memo(

const moleculesToEditIdsSt = useSelector(state => state.selectionReducers.moleculesToEdit) || [];

const lhsCompounds = useSelector(state => state.apiReducers.lhs_compounds_list);

const [taggingInProgress, setTaggingInProgress] = useState(false);
const [isError, setIsError] = useState(false);
const [molsLeftForTagging, setMolsLeftForTagging] = useState(0);
Expand All @@ -142,6 +144,12 @@ export const TagEditor = memo(
}

const moleculesToEdit = moleculesToEditIds.map(id => dispatch(getMoleculeForId(id)));
let lhsCmp = null;
if (moleculesToEdit?.length > 0) {
const firstMolToEdit = moleculesToEdit[0];
const cmpId = firstMolToEdit.cmpd;
lhsCmp = lhsCompounds?.find(c => c.origId === cmpId && firstMolToEdit.canon_site_conf === c.canonSiteConf);
}
moleculeTags = moleculeTags.sort(compareTagsAsc);
const assignTagEditorOpen = useSelector(state => state.selectionReducers.tagEditorOpened);

Expand Down Expand Up @@ -170,6 +178,15 @@ export const TagEditor = memo(
}
};

const updateCmp = (cmp, obs) => {
let newCmp = { ...cmp };
const index = newCmp.associatedObs.findIndex(o => o.id === obs.id);
if (index >= 0) {
newCmp.associatedObs[index] = obs;
dispatch(updateLHSCompound(newCmp));
}
};

const handleTagClick = async (selected, tag) => {
try {
setTaggingInProgress(true);
Expand All @@ -189,6 +206,7 @@ export const TagEditor = memo(
moleculesToEdit.forEach(m => {
let newMol = { ...m };
newMol.tags_set = newMol.tags_set.filter(id => id !== tag.id);
updateCmp(lhsCmp, newMol);
dispatch(updateMoleculeInMolLists(newMol));
const moleculeTag = getMoleculeTagForTag(moleculeTags, tag.id);

Expand Down Expand Up @@ -217,6 +235,7 @@ export const TagEditor = memo(
if (!m.tags_set.some(id => id === tag.id)) {
let newMol = { ...m };
newMol.tags_set.push(tag.id);
updateCmp(lhsCmp, newMol);
dispatch(updateMoleculeInMolLists(newMol));
const moleculeTag = getMoleculeTagForTag(moleculeTags, tag.id);
let mtObject = molTagObjects.find(mto => mto.tag === tag.tag);
Expand Down
7 changes: 7 additions & 0 deletions js/constants/constants.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,13 @@ export const CATEGORY_TYPE_BY_ID = {
export const OBSERVATION_TAG_CATEGORIES = ['ConformerSites', 'CrystalformSites', 'Quatassemblies', 'Crystalforms'];
export const COMPOUND_PRIO_TAG_CATEGORIES = ['CanonSites'];
export const TAG_DETAILS_REMOVED_CATEGORIES = ['CrystalformSites', 'ConformerSites'];
export const NON_ASSIGNABLE_CATEGORIES = [
'ConformerSites',
'CanonSites',
'CrystalformSites',
'Quatassemblies',
'Crystalforms'
];

export const TAG_TYPE = {
ALL: 'ALL',
Expand Down
7 changes: 7 additions & 0 deletions js/reducers/api/actions.js
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,13 @@ export const updateMoleculeInMolLists = mol => {
};
};

export const updateLHSCompound = cmp => {
return {
type: constants.UPDATE_LHS_COMPOUND,
cmp: cmp
};
};

export const setSavingState = function(savingState) {
return {
type: constants.SET_SAVING_STATE,
Expand Down
13 changes: 12 additions & 1 deletion js/reducers/api/apiReducers.js
Original file line number Diff line number Diff line change
Expand Up @@ -176,14 +176,25 @@ export default function apiReducers(state = INITIAL_STATE, action = {}) {
const indexOfMol = newList.findIndex(m => m.id === action.mol.id);
if (indexOfMol >= 0) {
newList[indexOfMol] = { ...action.mol };
return { ...state, all_mol_lists: newList };
return { ...state, all_mol_lists: [...newList] };
} else {
return state;
}

case constants.SET_LHS_COMPOUNDS_LIST:
return { ...state, lhs_compounds_list: action.lhs_compounds_list };

case constants.UPDATE_LHS_COMPOUND: {
let newList = [...state.lhs_compounds_list];
const indexOfCmp = newList.findIndex(c => c.id === action.cmp.id);
if (indexOfCmp >= 0) {
newList[indexOfCmp] = { ...action.cmp };
return { ...state, lhs_compounds_list: [...newList] };
} else {
return state;
}
}

case constants.SET_PANNDA_EVENT_LIST:
return Object.assign({}, state, {
pandda_event_list: action.pandda_event_list
Expand Down
3 changes: 2 additions & 1 deletion js/reducers/api/constants.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,5 +53,6 @@ export const constants = {
SET_ALL_DATA_LOADED: prefix + 'SET_ALL_DATA_LOADED',
SET_SNAPSHOT_LOADING_IN_PROGRESS: prefix + 'SET_SNAPSHOT_LOADING_IN_PROGRESS',
SET_IS_SNAPSHOT: prefix + 'SET_IS_SNAPSHOT',
SET_LHS_COMPOUNDS_LIST: prefix + 'SET_LHS_COMPOUNDS_LIST'
SET_LHS_COMPOUNDS_LIST: prefix + 'SET_LHS_COMPOUNDS_LIST',
UPDATE_LHS_COMPOUND: prefix + 'UPDATE_LHS_COMPOUND'
};

0 comments on commit 5d3e7d1

Please sign in to comment.