Commit ec6848c0 authored by PiTrem's avatar PiTrem
Browse files

FE fix element synchronization while handling changing current element:

cancel fix: do not force reload

when the user navigate between the element details tabs of opened reactions and samples:

- fix synchronization of density/molarity
- fix synch e.g when navigating to one Reaction, to a non associated sample, then to an associated sample.
  the data are synchronized independently of next tab:
  -- when the previous tab was a reaction then the data in all opened associated samples tabs will be synchronized
  -- when the previous tab was a sample then the data in the opened reaction tab will be synchronized (w reaction.updateMaterial)

simplify synchronize element
parent d640ee51
......@@ -206,29 +206,25 @@ export default class SampleDetails extends React.Component {
handleSubmit(closeView = false) {
const { sample } = this.state;
if (sample.belongTo && sample.belongTo.type === 'reaction') {
let reaction = sample.belongTo;
const reaction = sample.belongTo;
reaction.editedSample = sample;
const materialGroup = sample.matGroup;
if(sample.isNew) {
if (sample.isNew) {
ElementActions.createSampleForReaction(sample, reaction, materialGroup);
} else {
if(closeView) {
ElementActions.updateSampleForReaction(sample, reaction);
} else {
ElementActions.updateSample(new Sample(sample));
}
ElementActions.updateSampleForReaction(sample, reaction, closeView);
}
} else if(sample.belongTo && sample.belongTo.type === 'wellplate') {
} else if (sample.belongTo && sample.belongTo.type === 'wellplate') {
const wellplate = sample.belongTo;
ElementActions.updateSampleForWellplate(sample, wellplate)
} else {
if(sample.isNew) {
if (sample.isNew) {
ElementActions.createSample(sample)
} else {
ElementActions.updateSample(new Sample(sample))
}
}
if(sample.is_new || closeView) {
if (sample.is_new || closeView) {
DetailActions.close(sample, true);
}
sample.updateChecksum();
......@@ -702,16 +698,18 @@ export default class SampleDetails extends React.Component {
)
}
sampleContainerTab(ind){
let sample = this.state.sample || {}
sampleContainerTab(ind) {
const { sample } = this.state;
return(
<Tab eventKey={ind} title={'Analyses'}
key={'Container' + sample.id.toString()}>
<ListGroupItem style={{paddingBottom: 20}}>
<SampleDetailsContainers
sample={sample} setState={(sample) => {this.setState(sample)}}
sample={sample}
setState={(sample) => {this.setState(sample)}}
handleSampleChanged={this.handleSampleChanged}
/>
fromSample
/>
</ListGroupItem>
</Tab>
)
......
......@@ -243,12 +243,12 @@ class ElementActions {
});};
}
updateSampleForReaction(sample, reaction) {
updateSampleForReaction(sample, reaction, closeView = true) {
return (dispatch) => { SamplesFetcher.update(sample)
.then((newSample) => {
reaction.updateMaterial(newSample);
reaction.changed = true;
dispatch(reaction)
dispatch({ reaction, sample: newSample, closeView })
}).catch((errorMessage) => {
console.log(errorMessage);
});};
......
......@@ -522,18 +522,20 @@ class ElementStore {
// -- Samples --
handleFetchSampleById(result) {
// workaround solution for 507:
// always reset currentElement and selecteds if currentElement.type is sample
// (because this is a listener for fetching Sample; and also checksum might not be changed under some conditions)
if (!this.state.currentElement || this.state.currentElement._checksum != result._checksum || this.state.currentElement.type === 'sample') {
// // workaround solution for 507:
// // always reset currentElement and selecteds if currentElement.type is sample
// // (because this is a listener for fetching Sample; and also checksum might not be changed under some conditions)
// if (!this.state.currentElement || this.state.currentElement._checksum != result._checksum || this.state.currentElement.type === 'sample') {
// this.state.currentElement = result;
// if (this.state.currentElement.type === 'sample') {
// const selecteds = this.state.selecteds;
// const idx = findIndex(selecteds, function(o) { return o.id == result.id; });
// if (idx === -1) {
// this.state.selecteds.splice(selecteds.length, 1, result);
// } else { this.state.selecteds.splice(idx, 1, result); }
// }
if (!this.state.currentElement || this.state.currentElement._checksum != result._checksum) {
this.state.currentElement = result;
if (this.state.currentElement.type === 'sample') {
const selecteds = this.state.selecteds;
const idx = findIndex(selecteds, function(o) { return o.id == result.id; });
if (idx === -1) {
this.state.selecteds.splice(selecteds.length, 1, result);
} else { this.state.selecteds.splice(idx, 1, result); }
}
}
}
......@@ -570,10 +572,15 @@ class ElementStore {
this.state.currentElement = sample;
}
handleUpdateSampleForReaction(reaction) {
handleUpdateSampleForReaction({ reaction, sample, closeView }) {
// UserActions.fetchCurrentUser();
this.state.currentElement = reaction;
this.handleRefreshElements('sample');
if (closeView) {
this.state.currentElement = reaction;
} else {
this.state.currentElement = sample;
}
this.handleUpdateElement(sample);
}
handleUpdateSampleForWellplate(wellplate) {
......@@ -934,9 +941,8 @@ class ElementStore {
const index = this.elementIndex(selecteds, nextEl);
let activeKey = index;
let newSelecteds = null;
const sync = this.synchronizeElements(oriEl, nextEl);
oriEl = sync.ori;
nextEl = sync.next;
oriEl = this.synchronizeElements(oriEl);
if (index === -1) {
activeKey = selecteds.length
newSelecteds = this.addElement(nextEl)
......@@ -1013,34 +1019,48 @@ class ElementStore {
}
this.state.selecteds = this.state.selecteds.map((e) => {
if (SameEleTypId(e, updatedElement)) { return updatedElement; }
if (SameEleTypId(e, updatedElement)) {
return updatedElement;
}
return e;
});
}
synchronizeElements(close, open) {
const associatedSampleFromReaction = (
close instanceof Reaction && open instanceof Sample &&
close.samples.map(s => s.id).includes(open.id)
);
this.synchronizeElements(updatedElement);
return true;
}
const associatedReactionFromSample = (
close instanceof Sample && open instanceof Reaction &&
open.samples.map(s => s.id).includes(close.id)
);
synchronizeElements(previous) {
const { selecteds } = this.state;
if (associatedSampleFromReaction) {
const s = close.samples.filter(x => x.id == open.id)[0];
if (previous instanceof Sample) {
const rId = previous.tag && previous.tag.taggable_data
&& previous.tag.taggable_data.reaction_id;
const openedReaction = selecteds.find(el => (el.type === 'reaction' && el.id === rId));
if (openedReaction) {
openedReaction.updateMaterial(previous);
if (previous.isPendingToSave) {
openedReaction.changed = previous.isPendingToSave;
}
}
}
open.amount_value = s.amount_value;
open.amount_unit = s.amount_unit;
open.container = s.container;
} else if (associatedReactionFromSample) {
open.updateMaterial(close);
if (close.isPendingToSave) { open.changed = close.isPendingToSave; }
if (previous instanceof Reaction) {
const samples = previous.samples;
selecteds.map((nextSample) => {
const previousSample = samples.find(s => SameEleTypId(nextSample, s));
if (previousSample) {
nextSample.amount_value = previousSample.amount_value;
nextSample.amount_unit = previousSample.amount_unit;
nextSample.container = previousSample.container;
nextSample.density = previousSample.density;
nextSample._molarity_unit = previousSample._molarity_unit;
nextSample._molarity_value = previousSample._molarity_value;
}
return nextSample;
});
}
return { ori: close, next: open };
return previous;
}
addElement(addEl) {
......@@ -1079,22 +1099,23 @@ class ElementStore {
} else {
this.state.currentElement = newCurrentElement;
}
if (this.state.currentElement && this.state.currentElement.type === 'reaction') {
// workaround solution for 507:
// update samples data of Reaction
// this is executed when curretn element is set, but, for some cases it will not be called z.B. open window(Sample) from Reaction
const currentElementProducts = this.state.currentElement.products;
currentElementProducts.map((p) => {
SamplesFetcher.fetchById(p.id)
.then((newSample) => {
const idx = findIndex(this.state.currentElement.products, function(o) { return o.id == newSample.id; });
this.state.currentElement.products.splice(idx, 1, newSample);
}).catch((errorMessage) => {
console.log(errorMessage);
});
});
}
// if (this.state.currentElement && this.state.currentElement.type === 'reaction') {
// // workaround solution for 507:
// // update samples data of Reaction
// // this is executed when curretn element is set, but, for some cases it will not be called z.B. open window(Sample) from Reaction
// const currentElementProducts = this.state.currentElement.products;
// currentElementProducts.map((p) => {
// SamplesFetcher.fetchById(p.id)
// .then((newSample) => {
// const idx = findIndex(this.state.currentElement.products, function(o) { return o.id == newSample.id; });
// this.state.currentElement.products.splice(idx, 1, newSample);
// }).catch((errorMessage) => {
// console.log(errorMessage);
// });
// });
// }
UrlSilentNavigation(newCurrentElement)
return true
}
deleteCurrentElement(deleteEl) {
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment