Repository navigation
Add mileage rate edit form with date fields and UpdatePolicyDistanceRate support #91518
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
Changes from all commits
2cb2d3f
a9dc1c6
ce92f3d
25ed324
2eef6be
2f1e67b
34150ef
2bb9ce9
e8a5b2f
b0fdba4
d55277e
bb23d71
953854b
f90d67b
2c01bcd
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,7 @@ | ||
| type UpdatePolicyDistanceRateParams = { | ||
| policyID: string; | ||
| customUnitID: string; | ||
| customUnitRateArray: string; | ||
| }; | ||
|
|
||
| export default UpdatePolicyDistanceRateParams; |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -8,6 +8,7 @@ import type { | |
| OpenPolicyDistanceRatesPageParams, | ||
| SetPolicyDistanceRatesEnabledParams, | ||
| SetPolicyDistanceRatesUnitParams, | ||
| UpdatePolicyDistanceRateParams, | ||
| UpdatePolicyDistanceRateValueParams, | ||
| } from '@libs/API/parameters'; | ||
| import {READ_COMMANDS, WRITE_COMMANDS} from '@libs/API/types'; | ||
|
|
@@ -319,6 +320,18 @@ function updatePolicyDistanceRateName(policyID: string, customUnit: CustomUnit, | |
| API.write(WRITE_COMMANDS.UPDATE_POLICY_DISTANCE_RATE_NAME, params, {optimisticData, successData, failureData}); | ||
| } | ||
|
|
||
| function updatePolicyDistanceRate(policyID: string, customUnit: CustomUnit, rateToUpdate: Rate, fieldName: keyof Pick<Rate, 'startDate' | 'endDate'>) { | ||
| const {optimisticData, successData, failureData} = buildOnyxDataForPolicyDistanceRateUpdates(policyID, customUnit, [rateToUpdate], fieldName); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. NAB: No need to have this as a separate var, you could call it inline |
||
|
|
||
| const params: UpdatePolicyDistanceRateParams = { | ||
| policyID, | ||
| customUnitID: customUnit.customUnitID, | ||
| customUnitRateArray: JSON.stringify(prepareCustomUnitRatesArray([rateToUpdate])), | ||
| }; | ||
|
|
||
| API.write(WRITE_COMMANDS.UPDATE_POLICY_DISTANCE_RATE, params, {optimisticData, successData, failureData}); | ||
| } | ||
|
|
||
| function setPolicyDistanceRatesEnabled(policyID: string, customUnit: CustomUnit, customUnitRates: Rate[]) { | ||
| const currentRates = customUnit.rates; | ||
| const optimisticRates: Record<string, NullishDeep<Rate>> = {}; | ||
|
|
@@ -517,6 +530,7 @@ export { | |
| clearPolicyDistanceRateErrorFields, | ||
| updatePolicyDistanceRateValue, | ||
| updatePolicyDistanceRateName, | ||
| updatePolicyDistanceRate, | ||
| setPolicyDistanceRatesEnabled, | ||
| deletePolicyDistanceRates, | ||
| updateDistanceTaxClaimableValue, | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,125 @@ | ||
| import React from 'react'; | ||
| import {View} from 'react-native'; | ||
| import DatePicker from '@components/DatePicker'; | ||
| import FormProvider from '@components/Form/FormProvider'; | ||
| import InputWrapper from '@components/Form/InputWrapper'; | ||
| import type {FormInputErrors, FormOnyxValues} from '@components/Form/types'; | ||
| import HeaderWithBackButton from '@components/HeaderWithBackButton'; | ||
| import ScreenWrapper from '@components/ScreenWrapper'; | ||
| import useLocalize from '@hooks/useLocalize'; | ||
| import usePermissions from '@hooks/usePermissions'; | ||
| import usePolicy from '@hooks/usePolicy'; | ||
| import useThemeStyles from '@hooks/useThemeStyles'; | ||
| import Navigation from '@libs/Navigation/Navigation'; | ||
| import type {PlatformStackScreenProps} from '@libs/Navigation/PlatformStackNavigation/types'; | ||
| import {getDistanceRateCustomUnit} from '@libs/PolicyUtils'; | ||
| import type {SettingsNavigatorParamList} from '@navigation/types'; | ||
| import NotFoundPage from '@pages/ErrorPage/NotFoundPage'; | ||
| import AccessOrNotFoundWrapper from '@pages/workspace/AccessOrNotFoundWrapper'; | ||
| import {updatePolicyDistanceRate} from '@userActions/Policy/DistanceRate'; | ||
| import CONST from '@src/CONST'; | ||
| import ONYXKEYS from '@src/ONYXKEYS'; | ||
| import SCREENS from '@src/SCREENS'; | ||
| import INPUT_IDS from '@src/types/form/PolicyDistanceRateEditForm'; | ||
|
|
||
| type PolicyDistanceRateDateEditPageProps = PlatformStackScreenProps< | ||
| SettingsNavigatorParamList, | ||
| typeof SCREENS.WORKSPACE.DISTANCE_RATE_START_DATE_EDIT, | ||
| typeof SCREENS.WORKSPACE.DISTANCE_RATE_END_DATE_EDIT | ||
| >; | ||
|
Comment on lines
+25
to
+29
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why are we passing a route name as the NavigatorID? I think it should be like this instead: |
||
|
|
||
| function PolicyDistanceRateDateEditPage({route}: PolicyDistanceRateDateEditPageProps) { | ||
| const styles = useThemeStyles(); | ||
| const {translate} = useLocalize(); | ||
| const {isBetaEnabled} = usePermissions(); | ||
| const isDateBoundMileageRateEnabled = isBetaEnabled(CONST.BETAS.DATE_BOUND_MILEAGE_RATE); | ||
| const policyID = route.params.policyID; | ||
| const rateID = route.params.rateID; | ||
| const policy = usePolicy(policyID); | ||
| const customUnit = getDistanceRateCustomUnit(policy); | ||
| const rate = customUnit?.rates[rateID]; | ||
|
|
||
| const isStartDate = route.name === SCREENS.WORKSPACE.DISTANCE_RATE_START_DATE_EDIT; | ||
| const fieldName = isStartDate ? CONST.CUSTOM_UNITS.RATE_FIELD.START_DATE : CONST.CUSTOM_UNITS.RATE_FIELD.END_DATE; | ||
| const inputID = isStartDate ? INPUT_IDS.START_DATE : INPUT_IDS.END_DATE; | ||
| const titleKey = isStartDate ? 'workspace.distanceRates.startDate' : 'workspace.distanceRates.endDate'; | ||
| const otherDate = isStartDate ? rate?.endDate : rate?.startDate; | ||
|
|
||
| const validate = (values: FormOnyxValues<typeof ONYXKEYS.FORMS.POLICY_DISTANCE_RATE_EDIT_FORM>) => { | ||
| const errors: FormInputErrors<typeof ONYXKEYS.FORMS.POLICY_DISTANCE_RATE_EDIT_FORM> = {}; | ||
| if (isStartDate && values.startDate && otherDate && values.startDate > otherDate) { | ||
| errors.startDate = translate('workspace.distanceRates.errors.startDateMustBeBeforeEndDate'); | ||
| } | ||
| if (!isStartDate && otherDate && values.endDate && otherDate > values.endDate) { | ||
| errors.endDate = translate('workspace.distanceRates.errors.startDateMustBeBeforeEndDate'); | ||
| } | ||
| return errors; | ||
| }; | ||
|
|
||
| const submit = (values: FormOnyxValues<typeof ONYXKEYS.FORMS.POLICY_DISTANCE_RATE_EDIT_FORM>) => { | ||
| if (!customUnit || !rate) { | ||
| return; | ||
| } | ||
|
|
||
| const newValue = (isStartDate ? values.startDate : values.endDate) || null; | ||
| const currentValue = rate[fieldName] ?? null; | ||
| if (currentValue === newValue) { | ||
| Navigation.goBack(); | ||
| return; | ||
| } | ||
|
|
||
| updatePolicyDistanceRate( | ||
| policyID, | ||
| customUnit, | ||
| { | ||
| ...rate, | ||
| [fieldName]: newValue, | ||
| }, | ||
| fieldName, | ||
| ); | ||
| Navigation.goBack(); | ||
| }; | ||
|
|
||
| if (!rate || !isDateBoundMileageRateEnabled) { | ||
| return <NotFoundPage />; | ||
| } | ||
|
|
||
| return ( | ||
| <AccessOrNotFoundWrapper | ||
| accessVariants={[CONST.POLICY.ACCESS_VARIANTS.ADMIN, CONST.POLICY.ACCESS_VARIANTS.PAID]} | ||
| policyID={policyID} | ||
| featureName={CONST.POLICY.MORE_FEATURES.ARE_DISTANCE_RATES_ENABLED} | ||
| > | ||
| <ScreenWrapper | ||
| enableEdgeToEdgeBottomSafeAreaPadding | ||
| style={[styles.defaultModalContainer]} | ||
| testID={isStartDate ? 'PolicyDistanceRateStartDateEditPage' : 'PolicyDistanceRateEndDateEditPage'} | ||
| shouldEnableMaxHeight | ||
| > | ||
| <HeaderWithBackButton title={translate(titleKey)} /> | ||
| <FormProvider | ||
| formID={ONYXKEYS.FORMS.POLICY_DISTANCE_RATE_EDIT_FORM} | ||
| submitButtonText={translate('common.save')} | ||
| onSubmit={submit} | ||
| validate={validate} | ||
| enabledWhenOffline | ||
| style={[styles.flexGrow1]} | ||
| submitButtonStyles={[styles.mh5, styles.mt0]} | ||
| addBottomSafeAreaPadding | ||
| > | ||
| <View style={styles.mh5}> | ||
| <InputWrapper | ||
| autoFocus | ||
| InputComponent={DatePicker} | ||
| inputID={inputID} | ||
| label={translate(titleKey)} | ||
| defaultValue={rate[fieldName] ?? ''} | ||
| /> | ||
| </View> | ||
| </FormProvider> | ||
| </ScreenWrapper> | ||
| </AccessOrNotFoundWrapper> | ||
| ); | ||
| } | ||
|
|
||
| export default PolicyDistanceRateDateEditPage; | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -14,9 +14,11 @@ import useConfirmModal from '@hooks/useConfirmModal'; | |
| import {useMemoizedLazyExpensifyIcons} from '@hooks/useLazyAsset'; | ||
| import useLocalize from '@hooks/useLocalize'; | ||
| import useOnyx from '@hooks/useOnyx'; | ||
| import usePermissions from '@hooks/usePermissions'; | ||
| import useThemeStyles from '@hooks/useThemeStyles'; | ||
| import useTransactionViolation from '@hooks/useTransactionViolation'; | ||
| import {convertAmountToDisplayString} from '@libs/CurrencyUtils'; | ||
| import DateUtils from '@libs/DateUtils'; | ||
| import {getLatestErrorField} from '@libs/ErrorUtils'; | ||
| import Navigation from '@libs/Navigation/Navigation'; | ||
| import type {PlatformStackScreenProps} from '@libs/Navigation/PlatformStackNavigation/types'; | ||
|
|
@@ -38,6 +40,8 @@ function PolicyDistanceRateDetailsPage({route}: PolicyDistanceRateDetailsPagePro | |
| const styles = useThemeStyles(); | ||
| const {translate} = useLocalize(); | ||
| const {showConfirmModal} = useConfirmModal(); | ||
| const {isBetaEnabled} = usePermissions(); | ||
| const isDateBoundMileageRateEnabled = isBetaEnabled(CONST.BETAS.DATE_BOUND_MILEAGE_RATE); | ||
| const policyID = route.params.policyID; | ||
| const [policy] = useOnyx(`${ONYXKEYS.COLLECTION.POLICY}${route.params.policyID}`); | ||
| const rateID = route.params.rateID; | ||
|
|
@@ -115,6 +119,12 @@ function PolicyDistanceRateDetailsPage({route}: PolicyDistanceRateDetailsPagePro | |
| const editTaxRateValue = () => { | ||
| Navigation.navigate(ROUTES.WORKSPACE_DISTANCE_RATE_TAX_RATE_EDIT.getRoute(policyID, rateID)); | ||
| }; | ||
| const editStartDate = () => { | ||
| Navigation.navigate(ROUTES.WORKSPACE_DISTANCE_RATE_START_DATE_EDIT.getRoute(policyID, rateID)); | ||
| }; | ||
| const editEndDate = () => { | ||
| Navigation.navigate(ROUTES.WORKSPACE_DISTANCE_RATE_END_DATE_EDIT.getRoute(policyID, rateID)); | ||
| }; | ||
|
|
||
| const showWarningModal = () => { | ||
| showConfirmModal({ | ||
|
|
@@ -218,6 +228,38 @@ function PolicyDistanceRateDetailsPage({route}: PolicyDistanceRateDetailsPagePro | |
| onPress={editRateValue} | ||
| /> | ||
| </OfflineWithFeedback> | ||
| {isDateBoundMileageRateEnabled && ( | ||
| <OfflineWithFeedback | ||
| errors={getLatestErrorField(rate ?? {}, 'startDate')} | ||
| pendingAction={rate?.pendingFields?.startDate} | ||
| errorRowStyles={styles.mh5} | ||
| onClose={() => clearErrorFields('startDate')} | ||
| > | ||
| <MenuItemWithTopDescription | ||
| shouldShowRightIcon | ||
| title={rate.startDate ? DateUtils.formatToReadableString(rate.startDate) : ''} | ||
| description={translate('workspace.distanceRates.startDate')} | ||
| descriptionTextStyle={styles.textNormal} | ||
| onPress={editStartDate} | ||
| /> | ||
| </OfflineWithFeedback> | ||
| )} | ||
| {isDateBoundMileageRateEnabled && ( | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why is this in another conditional block? Couldn't it just go in the first one? |
||
| <OfflineWithFeedback | ||
| errors={getLatestErrorField(rate ?? {}, 'endDate')} | ||
| pendingAction={rate?.pendingFields?.endDate} | ||
| errorRowStyles={styles.mh5} | ||
| onClose={() => clearErrorFields('endDate')} | ||
| > | ||
| <MenuItemWithTopDescription | ||
| shouldShowRightIcon | ||
| title={rate.endDate ? DateUtils.formatToReadableString(rate.endDate) : ''} | ||
| description={translate('workspace.distanceRates.endDate')} | ||
| descriptionTextStyle={styles.textNormal} | ||
| onPress={editEndDate} | ||
| /> | ||
| </OfflineWithFeedback> | ||
| )} | ||
| {isDistanceTrackTaxEnabled && isPolicyTrackTaxEnabled && ( | ||
| <OfflineWithFeedback | ||
| errors={getLatestErrorField(rate, 'taxRateExternalID')} | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.