So I have code here that is supposed to calculate a price based off of property sqft and the package selected.
The code used to work with class components when I had it written like so... but Im now using react JS and Functional components.
The current issue I am having right now is getting the setBasePrice(1234);
to actually set the base price so it can be calculated in the function.
Im also having an issue with getting the basePrice += basePrice * (percentage / 100);
to work properly in the functional component.
I know I have this totally wrong right now just hoping someone can point me in the right direction and possibly show me how to clean up my code.
UPDATED
I added the useEffect hook and the total price is coming up incorrectly. at 5500 sqft the price should be $272 but comes up as $112. My old code doesnt do this so im not sure whats going on.
Class Component
this.state = {
propertySqft: "",
packageSelected: this.props.location.state,
totalPrice: null,
basePrice: null
};
}
checkPrice = () => {
debugger;
if(this.state.packageSelected === "Photography"){
this.state.basePrice = 159.998;
}else if(this.state.packageSelected === "Video"){
this.state.basePrice = 416.998;
}else if(this.state.packageSelected === "Drone Aerial Photos Only"){
this.state.basePrice = 199.998;
}else if(this.state.packageSelected === "Drone Aerial Video Only"){
this.state.basePrice = 249.998;
}else if(this.state.packageSelected === "Drone Aerials Photo & Video"){
this.state.basePrice = 299.998;
}else if(this.state.packageSelected === "Photography, Video, Drone Aerials"){
this.state.basePrice = 559.998;
}if(this.state.propertySqft > 2500){
let overage = this.state.propertySqft - 2000;
let percentage = Math.floor(overage / 500) * 10;
this.state.basePrice += this.state.basePrice * (percentage / 100);
}
this.setState({ totalPrice: Math.round(this.state.basePrice * 100) / 100 });
}
Functional Component
const [basePrice, setBasePrice] = useState(0);
const [newPrice, setNewPrice] = useState(null);
const [totalPrice, setTotalPrice] = useState(null);
useEffect(() => {
setTotalPrice(basePrice => (Math.round(basePrice * 100) / 100));
}, [basePrice]);
const checkPrice = () => {
debugger;
if(packageSelected.packages === "Photography"){
setBasePrice(159.998);
}else if(packageSelected.packages === "Video"){
setBasePrice(516.998);
}else if(packageSelected.packages === "Drone Aerial Photos Only"){
setBasePrice(119.998);
}else if(packageSelected.packages === "Drone Aerial Video Only"){
setBasePrice(269.998);
}else if(packageSelected.packages === "Drone Aerials Photo & Video"){
setBasePrice(549.998);
}else if(packageSelected.packages === "Photography, Video, Drone Aerials"){
setBasePrice(566.998);
}if(propertySqft > 2000){
const overage = propertySqft - 2000;
const percentage = Math.floor(overage / 500) * 10;
setBasePrice(basePrice => (basePrice * (percentage / 100)));
}
};