i have a method and i want to perform two different things based on the arguments passed.
i have the method defined like below,
create_at = (x, y, point_3d) => {
if (!point_3d) {
this.props.pick_point([x, y]).then(picked => {
let next;
let some_var1 = this.state.some_var;
if (some_var1 === undefined) {
this.setState({
some_var1: some_var1,
point: {x: x, y: y},
});
} else {
next = this.some_drawings;
this.setState({
var_1: var_1,
});
}
next = this.add_path(
picked[0], picked[1], picked[2]);//this statement varies as
//well
this.props.something_changed(next);
}).catch(no_op);
} else {
let next;
let some_var1 = this.state.some_var;
if (some_var1 === undefined) {
this.setState({
some_var1: some_var1,
});
} else {
next = this.some_drawings;
this.setState({
var_1: var_1,
});
}
next = this.add_path(
point_3d[0], point_3d[1], point_3d[2]);
this.props.something_changed(next);
}
};
i call this create_at method like below...
this.create_at(x, y)
and other time like this
this.create_at(point_3d)
now calling this.create_at(x,y) works fine.
but calling this.create_at(point_3d) doesn't work meaning point_3d is undefined and gets into if statement of create_at method...
i want to change this create_at method such that should work when user passes only x, y values like this.create_at(x,y) and also when user passes only point_3d like this.create_at(point_3d)
what is expected?
when user has x,y values then he calls this.create_at(x,y) and the code inside if statement should execute.
when user has only point_3d value he calls this.create_at(point_3d) and the code inside else statement should execute.
if you take a closer look into code all the statements inside if and else are same except updating state for point is skipped in else clause.
How can i make this code work with less repetitive statements. could someone help me with this. thanks.