Commit c77f60e0 authored by Jaden's avatar Jaden

[js][plotterparameter] only save overriden properties

The plotterparameter editor's "save" function will now save to
the prefix only the properties that aren't set to the default
value. Tests have also been added for this.

Closes #213
parent d65987d9
Pipeline #30107 passed with stages
in 16 minutes and 15 seconds
......@@ -39,6 +39,14 @@ type Props = {
updateFunc: (BeatObject) => any,
};
// generate the default plotterparameter
// for a given plotter
const getDefaultsParameter = (plotter: any) =>
Object.entries(plotter.contents.parameters)
.map(([p, info]) => ({ [p]: getDefaultParameterValue(info) }))
.reduce((o, p) => ({ ...o, ...p }), {})
;
export class PlotterParameterEditor extends React.Component<Props> {
constructor(props: Props) {
//console.log(`Creating AlgDetail`);
......@@ -55,6 +63,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 = getDefaultsParameter(pl);
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,9 +105,7 @@ 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: getDefaultsParameter(pl)
});
}
......@@ -103,7 +129,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']
)
);
});
});
});
......@@ -121,6 +121,126 @@ describe('<PlotterParameterEditor />', () => {
}
});
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,
'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');
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,
'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');
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': {
'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');
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