Commit cd72e32c authored by Flavio TARSETTI's avatar Flavio TARSETTI

Merge branch '213-incremental-plotterparameters' into 'master'

Plotterparameter editor: Only save changed fields

Closes #213

See merge request !77
parents d65987d9 1ec48820
Pipeline #31397 passed with stages
in 48 minutes and 45 seconds
......@@ -39,10 +39,30 @@ type Props = {
updateFunc: (BeatObject) => any,
};
export class PlotterParameterEditor extends React.Component<Props> {
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 }), {});
};
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) => {
......@@ -55,6 +75,26 @@ export class PlotterParameterEditor extends React.Component<Props> {
});
}
// provide custom save func -
// don't save default properties to plotterparmaeter!
// only save properties with values that arent
// equal to the default value.
saveParameter = () => {
const pl = this.getPlotter();
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
.filter(([field, dval]) => curr.hasOwnProperty(field) && dval !== curr[field])
.map(([field, dval]) => [field, curr[field]])
.reduce((o, [field, val]) => ({...o, [field]: val}), {})
;
const paramToSave = copyObj(this.props.data);
paramToSave.contents.data = diff;
this.props.saveFunc(paramToSave);
}
updateDescription = (desc: string) => {
this.setContents({ description: desc });
}
......@@ -77,18 +117,28 @@ export class PlotterParameterEditor extends React.Component<Props> {
this.setContents({
plotter: pl.name,
data: Object.entries(pl.contents.parameters)
.map(([p, info]) => ({ [p]: getDefaultParameterValue(info) }))
.reduce((o, p) => ({ ...o, ...p }), {})
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
});
}
......@@ -103,7 +153,7 @@ export class PlotterParameterEditor extends React.Component<Props> {
className='mx-auto'
outline
color='secondary'
onClick={() => this.props.saveFunc(this.props.data)}
onClick={() => this.saveParameter()}
>
Save Changes (Changes are <ValidSchemaBadge entity='plotterparameter' obj={this.props.data} />)
</Button>
......
......@@ -30,7 +30,7 @@ describe('<PlotterParameterEditor />', () => {
data: {
legend_loc: 'string'
},
plotter: "plot/bar/1"
plotter: 'plot/bar/1'
}
}
].concat(testPlotterParameters.map(a => getValidObj(a)));
......@@ -52,7 +52,7 @@ describe('<PlotterParameterEditor />', () => {
);
expect(wrapper).to.have.props(
['data', 'plotterparameters', 'plotters', 'saveFunc', 'updateFunc']
)
);
});
});
});
......@@ -98,29 +98,73 @@ 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': ''
}
});
wrapper.find('button.mx-auto.btn.btn-outline-secondary').simulate('click');
let savedObj = saveFunc.args[0][0].contents;
expect(savedObj).to.deep.equal({
'description': '',
'plotter': 'plot/bar/1',
'data': {
}
});
wrapper.find('ParameterConsume').at(0).find('input').simulate('change', { target: { value: '11'}});
expect(wrapper.props().data.contents).to.deep.equal({
'description': '',
'plotter': 'plot/bar/1',
'data': {
'axis-fontsize': 11,
}
});
wrapper.find('button.mx-auto.btn.btn-outline-secondary').simulate('click');
savedObj = saveFunc.args[1][0].contents;
expect(savedObj).to.deep.equal({
'description': '',
'plotter': 'plot/bar/1',
'data': {
'axis-fontsize': 11,
}
});
wrapper.find('ParameterConsume').at(8).find('input').simulate('change', { target: { value: 'plot legend'}});
expect(wrapper.props().data.contents).to.deep.equal({
'description': '',
'plotter': 'plot/bar/1',
'data': {
'axis-fontsize': 11,
'legend': 'plot legend',
}
});
wrapper.find('button.mx-auto.btn.btn-outline-secondary').simulate('click');
savedObj = saveFunc.args[2][0].contents;
expect(savedObj).to.deep.equal({
'description': '',
'plotter': 'plot/bar/1',
'data': {
'axis-fontsize': 11,
'legend': 'plot legend',
}
});
wrapper.find('ParameterConsume').at(0).find('input').simulate('change', { target: { value: '10'}});
expect(wrapper.props().data.contents).to.deep.equal({
'description': '',
'plotter': 'plot/bar/1',
'data': {
'legend': 'plot legend',
}
});
wrapper.find('button.mx-auto.btn.btn-outline-secondary').simulate('click');
savedObj = saveFunc.args[3][0].contents;
expect(savedObj).to.deep.equal({
'description': '',
'plotter': 'plot/bar/1',
'data': {
'legend': 'plot legend',
}
});
});
});
});
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