Commit 1ec48820 authored by Jaden's avatar Jaden

[plotterparameter] dont cache default-valued fields

Avoids ever caching/saving default-valued fields in the
plotterparameter editor
parent 1dd6d831
Pipeline #30874 passed with stages
in 23 minutes and 54 seconds
......@@ -39,18 +39,30 @@ type Props = {
updateFunc: (BeatObject) => any,
};
// generate the default plotterparameter
// for a given plotter
const getDefaultsParameter = (plotter: any) =>
Object.entries(plotter.contents.parameters)
type State = {
defaults: {[string]: any},
};
// generate the default plotterparameter for a given plotter
// return empty if the given plotter is empty
const getDefaultsParameter = (plotter: any): {[string]: any} => {
if(Object.entries(plotter).length === 0){
return {};
}
return Object.entries(plotter.contents.parameters)
.map(([p, info]) => ({ [p]: getDefaultParameterValue(info) }))
.reduce((o, p) => ({ ...o, ...p }), {})
;
.reduce((o, p) => ({ ...o, ...p }), {});
};
export class PlotterParameterEditor extends React.Component<Props> {
export class PlotterParameterEditor extends React.Component<Props, State> {
constructor(props: Props) {
//console.log(`Creating AlgDetail`);
super(props);
this.state = {
// calculate the defaults for the pp
defaults: getDefaultsParameter(this.getPlotter()),
};
}
setContents = (newContents: any) => {
......@@ -69,7 +81,7 @@ export class PlotterParameterEditor extends React.Component<Props> {
// equal to the default value.
saveParameter = () => {
const pl = this.getPlotter();
const defaults = getDefaultsParameter(pl);
const defaults = this.state.defaults;
const curr = this.props.data.contents.data;
const diff = Object.entries(defaults)
// only save fields that arent the default value
......@@ -107,14 +119,26 @@ export class PlotterParameterEditor extends React.Component<Props> {
plotter: pl.name,
data: {}
});
this.setState({
defaults: getDefaultsParameter(pl),
});
}
// update the value of a parameter
updateParameter = (pName: string, v: number | string | boolean) => {
const newData = copyObj(this.props.data.contents.data);
// if the new value is equal to the default value,
// dont save the field
// else update the field value
if(v === this.state.defaults[pName]){
delete newData[pName];
} else {
newData[pName] = v;
}
this.setContents({
data: {
...this.props.data.contents.data,
[pName]: v
}
data: newData
});
}
......
......@@ -98,26 +98,6 @@ describe('<PlotterParameterEditor />', () => {
'description': '',
'plotter': 'plot/bar/1',
'data': {
'axis-fontsize': 10,
'bar-alpha': 0.75,
'bar-norm': true,
'bar_attributes': '',
'content_type': 'image/png',
'dpi': 60,
'grid': false,
'height': 300,
'legend': '',
'legend-bbox-to-anchor': 1,
'legend-fontsize': 12,
'legend-loc': 'best',
'title': 'Bar plot',
'title-fontsize': 10,
'width': 400,
'xaxis_multiplier': 1,
'xlabel': '',
'yaxis_log': false,
'yaxis_multiplier': 1,
'ylabel': ''
}
});
......@@ -136,25 +116,6 @@ describe('<PlotterParameterEditor />', () => {
'plotter': 'plot/bar/1',
'data': {
'axis-fontsize': 11,
'bar-alpha': 0.75,
'bar-norm': true,
'bar_attributes': '',
'content_type': 'image/png',
'dpi': 60,
'grid': false,
'height': 300,
'legend': '',
'legend-bbox-to-anchor': 1,
'legend-fontsize': 12,
'legend-loc': 'best',
'title': 'Bar plot',
'title-fontsize': 10,
'width': 400,
'xaxis_multiplier': 1,
'xlabel': '',
'yaxis_log': false,
'yaxis_multiplier': 1,
'ylabel': ''
}
});
wrapper.find('button.mx-auto.btn.btn-outline-secondary').simulate('click');
......@@ -173,25 +134,7 @@ describe('<PlotterParameterEditor />', () => {
'plotter': 'plot/bar/1',
'data': {
'axis-fontsize': 11,
'bar-alpha': 0.75,
'bar-norm': true,
'bar_attributes': '',
'content_type': 'image/png',
'dpi': 60,
'grid': false,
'height': 300,
'legend': 'plot legend',
'legend-bbox-to-anchor': 1,
'legend-fontsize': 12,
'legend-loc': 'best',
'title': 'Bar plot',
'title-fontsize': 10,
'width': 400,
'xaxis_multiplier': 1,
'xlabel': '',
'yaxis_log': false,
'yaxis_multiplier': 1,
'ylabel': ''
}
});
wrapper.find('button.mx-auto.btn.btn-outline-secondary').simulate('click');
......@@ -210,26 +153,7 @@ describe('<PlotterParameterEditor />', () => {
'description': '',
'plotter': 'plot/bar/1',
'data': {
'axis-fontsize': 10,
'bar-alpha': 0.75,
'bar-norm': true,
'bar_attributes': '',
'content_type': 'image/png',
'dpi': 60,
'grid': false,
'height': 300,
'legend': 'plot legend',
'legend-bbox-to-anchor': 1,
'legend-fontsize': 12,
'legend-loc': 'best',
'title': 'Bar plot',
'title-fontsize': 10,
'width': 400,
'xaxis_multiplier': 1,
'xlabel': '',
'yaxis_log': false,
'yaxis_multiplier': 1,
'ylabel': ''
}
});
wrapper.find('button.mx-auto.btn.btn-outline-secondary').simulate('click');
......
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