Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Return early and prevent calling API if value didn't change #43308

Merged
merged 2 commits into from
Jun 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading