Skip to content

Commit

Permalink
Merge pull request #43308 from Expensify/monil-preventAPICalls
Browse files Browse the repository at this point in the history
Return early and prevent calling API if value didn't change
  • Loading branch information
luacmartins authored Jun 7, 2024
2 parents a82dcb3 + 387097e commit 13fedce
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,13 @@ function PolicyDistanceRateEditPage({policy, route}: PolicyDistanceRateEditPageP
const customUnit = customUnits[Object.keys(customUnits)[0]];
const rate = customUnit?.rates[rateID];
const currency = rate?.currency ?? CONST.CURRENCY.USD;
const currentRateValue = (rate?.rate ?? 0).toString();
const currentRateValue = (parseFloat((rate?.rate ?? 0).toString()) / CONST.POLICY.CUSTOM_UNIT_RATE_BASE_OFFSET).toFixed(3);

const submitRate = (values: FormOnyxValues<typeof ONYXKEYS.FORMS.POLICY_DISTANCE_RATE_EDIT_FORM>) => {
if (currentRateValue === values.rate) {
Navigation.goBack();
return;
}
DistanceRate.updatePolicyDistanceRateValue(policyID, customUnit, [{...rate, rate: Number(values.rate) * CONST.POLICY.CUSTOM_UNIT_RATE_BASE_OFFSET}]);
Keyboard.dismiss();
Navigation.goBack();
Expand Down Expand Up @@ -92,7 +96,7 @@ function PolicyDistanceRateEditPage({policy, route}: PolicyDistanceRateEditPageP
InputComponent={AmountForm}
inputID={INPUT_IDS.RATE}
extraDecimals={1}
defaultValue={(parseFloat(currentRateValue) / CONST.POLICY.CUSTOM_UNIT_RATE_BASE_OFFSET).toFixed(3)}
defaultValue={currentRateValue}
isCurrencyPressable={false}
currency={currency}
ref={inputCallbackRef}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,10 @@ function PolicyDistanceRateTaxRateEditPage({route, policy}: PolicyDistanceRateTa
}, [policy, taxRateExternalID]);

const onTaxRateChange = (newTaxRate: ListItemType) => {
if (taxRateExternalID === newTaxRate.value) {
Navigation.goBack();
return;
}
DistanceRate.updateDistanceTaxRate(policyID, customUnit, [
{
...rate,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,10 @@ function PolicyDistanceRateTaxReclaimableEditPage({route, policy}: PolicyDistanc
const currentTaxReclaimableOnValue = rate.attributes?.taxClaimablePercentage && rate.rate ? ((rate.attributes.taxClaimablePercentage * rate.rate) / 100).toFixed(decimals) : '';

const submitTaxReclaimableOn = (values: FormOnyxValues<typeof ONYXKEYS.FORMS.POLICY_DISTANCE_RATE_TAX_RECLAIMABLE_ON_EDIT_FORM>) => {
if (values.taxClaimableValue === currentTaxReclaimableOnValue) {
Navigation.goBack();
return;
}
DistanceRate.updateDistanceTaxClaimableValue(policyID, customUnit, [
{
...rate,
Expand Down
6 changes: 5 additions & 1 deletion src/pages/workspace/taxes/ValuePage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,14 @@ function ValuePage({

const submit = useCallback(
(values: FormOnyxValues<typeof ONYXKEYS.FORMS.WORKSPACE_TAX_VALUE_FORM>) => {
if (defaultValue === values.value) {
goBack();
return;
}
updatePolicyTaxValue(policyID, taxID, Number(values.value));
goBack();
},
[goBack, policyID, taxID],
[goBack, policyID, taxID, defaultValue],
);

if (!currentTaxRate) {
Expand Down

0 comments on commit 13fedce

Please sign in to comment.