-
Notifications
You must be signed in to change notification settings - Fork 3.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #38035 from MrMuzyk/feat/policy-add-distance-rate
feat: policy add distance rate
- Loading branch information
Showing
17 changed files
with
285 additions
and
38 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
type CreatePolicyDistanceRateParams = { | ||
policyID: string; | ||
customUnitID: string; | ||
customUnitRate: string; | ||
}; | ||
|
||
export default CreatePolicyDistanceRateParams; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
import type {FormInputErrors, FormOnyxValues} from '@components/Form/types'; | ||
import type ONYXKEYS from '@src/ONYXKEYS'; | ||
import * as CurrencyUtils from './CurrencyUtils'; | ||
import getPermittedDecimalSeparator from './getPermittedDecimalSeparator'; | ||
import * as MoneyRequestUtils from './MoneyRequestUtils'; | ||
import * as NumberUtils from './NumberUtils'; | ||
|
||
type RateValueForm = typeof ONYXKEYS.FORMS.WORKSPACE_RATE_AND_UNIT_FORM | typeof ONYXKEYS.FORMS.POLICY_CREATE_DISTANCE_RATE_FORM; | ||
|
||
function validateRateValue(values: FormOnyxValues<RateValueForm>, currency: string, toLocaleDigit: (arg: string) => string): FormInputErrors<RateValueForm> { | ||
const errors: FormInputErrors<RateValueForm> = {}; | ||
const parsedRate = MoneyRequestUtils.replaceAllDigits(values.rate, toLocaleDigit); | ||
const decimalSeparator = toLocaleDigit('.'); | ||
|
||
// Allow one more decimal place for accuracy | ||
const rateValueRegex = RegExp(String.raw`^-?\d{0,8}([${getPermittedDecimalSeparator(decimalSeparator)}]\d{1,${CurrencyUtils.getCurrencyDecimals(currency) + 1}})?$`, 'i'); | ||
if (!rateValueRegex.test(parsedRate) || parsedRate === '') { | ||
errors.rate = 'workspace.reimburse.invalidRateError'; | ||
} else if (NumberUtils.parseFloatAnyLocale(parsedRate) <= 0) { | ||
errors.rate = 'workspace.reimburse.lowRateError'; | ||
} | ||
return errors; | ||
} | ||
|
||
export default validateRateValue; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
101 changes: 101 additions & 0 deletions
101
src/pages/workspace/distanceRates/CreateDistanceRatePage.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,101 @@ | ||
import type {StackScreenProps} from '@react-navigation/stack'; | ||
import React, {useCallback} from 'react'; | ||
import type {OnyxEntry} from 'react-native-onyx'; | ||
import {withOnyx} from 'react-native-onyx'; | ||
import AmountForm from '@components/AmountForm'; | ||
import FormProvider from '@components/Form/FormProvider'; | ||
import InputWrapperWithRef from '@components/Form/InputWrapper'; | ||
import type {FormOnyxValues} from '@components/Form/types'; | ||
import HeaderWithBackButton from '@components/HeaderWithBackButton'; | ||
import ScreenWrapper from '@components/ScreenWrapper'; | ||
import useAutoFocusInput from '@hooks/useAutoFocusInput'; | ||
import useLocalize from '@hooks/useLocalize'; | ||
import useThemeStyles from '@hooks/useThemeStyles'; | ||
import validateRateValue from '@libs/PolicyDistanceRatesUtils'; | ||
import Navigation from '@navigation/Navigation'; | ||
import type {SettingsNavigatorParamList} from '@navigation/types'; | ||
import AdminPolicyAccessOrNotFoundWrapper from '@pages/workspace/AdminPolicyAccessOrNotFoundWrapper'; | ||
import PaidPolicyAccessOrNotFoundWrapper from '@pages/workspace/PaidPolicyAccessOrNotFoundWrapper'; | ||
import {createPolicyDistanceRate, generateCustomUnitID} from '@userActions/Policy'; | ||
import CONST from '@src/CONST'; | ||
import ONYXKEYS from '@src/ONYXKEYS'; | ||
import type SCREENS from '@src/SCREENS'; | ||
import INPUT_IDS from '@src/types/form/PolicyCreateDistanceRateForm'; | ||
import type {Rate} from '@src/types/onyx/Policy'; | ||
import type Policy from '@src/types/onyx/Policy'; | ||
|
||
type CreateDistanceRatePageOnyxProps = { | ||
policy: OnyxEntry<Policy>; | ||
}; | ||
|
||
type CreateDistanceRatePageProps = CreateDistanceRatePageOnyxProps & StackScreenProps<SettingsNavigatorParamList, typeof SCREENS.WORKSPACE.CREATE_DISTANCE_RATE>; | ||
|
||
function CreateDistanceRatePage({policy, route}: CreateDistanceRatePageProps) { | ||
const styles = useThemeStyles(); | ||
const {translate, toLocaleDigit} = useLocalize(); | ||
const currency = policy?.outputCurrency ?? CONST.CURRENCY.USD; | ||
const customUnits = policy?.customUnits ?? {}; | ||
const customUnitID = customUnits[Object.keys(customUnits)[0]]?.customUnitID ?? ''; | ||
const customUnitRateID = generateCustomUnitID(); | ||
const {inputCallbackRef} = useAutoFocusInput(); | ||
|
||
const validate = useCallback( | ||
(values: FormOnyxValues<typeof ONYXKEYS.FORMS.POLICY_CREATE_DISTANCE_RATE_FORM>) => validateRateValue(values, currency, toLocaleDigit), | ||
[currency, toLocaleDigit], | ||
); | ||
|
||
const submit = (values: FormOnyxValues<typeof ONYXKEYS.FORMS.POLICY_CREATE_DISTANCE_RATE_FORM>) => { | ||
const newRate: Rate = { | ||
currency, | ||
name: CONST.CUSTOM_UNITS.DEFAULT_RATE, | ||
rate: parseFloat(values.rate) * CONST.POLICY.CUSTOM_UNIT_RATE_BASE_OFFSET, | ||
customUnitRateID, | ||
enabled: true, | ||
}; | ||
|
||
createPolicyDistanceRate(route.params.policyID, customUnitID, newRate); | ||
Navigation.goBack(); | ||
}; | ||
|
||
return ( | ||
<AdminPolicyAccessOrNotFoundWrapper policyID={route.params.policyID}> | ||
<PaidPolicyAccessOrNotFoundWrapper policyID={route.params.policyID}> | ||
<ScreenWrapper | ||
includeSafeAreaPaddingBottom={false} | ||
style={[styles.defaultModalContainer]} | ||
testID={CreateDistanceRatePage.displayName} | ||
> | ||
<HeaderWithBackButton title={translate('workspace.distanceRates.addRate')} /> | ||
<FormProvider | ||
formID={ONYXKEYS.FORMS.POLICY_CREATE_DISTANCE_RATE_FORM} | ||
submitButtonText={translate('common.save')} | ||
onSubmit={submit} | ||
validate={validate} | ||
enabledWhenOffline | ||
style={[styles.flexGrow1]} | ||
shouldHideFixErrorsAlert | ||
submitFlexEnabled={false} | ||
submitButtonStyles={[styles.mh5, styles.mt0]} | ||
> | ||
<InputWrapperWithRef | ||
InputComponent={AmountForm} | ||
inputID={INPUT_IDS.RATE} | ||
extraDecimals={1} | ||
isCurrencyPressable={false} | ||
currency={currency} | ||
ref={inputCallbackRef} | ||
/> | ||
</FormProvider> | ||
</ScreenWrapper> | ||
</PaidPolicyAccessOrNotFoundWrapper> | ||
</AdminPolicyAccessOrNotFoundWrapper> | ||
); | ||
} | ||
|
||
CreateDistanceRatePage.displayName = 'CreateDistanceRatePage'; | ||
|
||
export default withOnyx<CreateDistanceRatePageProps, CreateDistanceRatePageOnyxProps>({ | ||
policy: { | ||
key: ({route}) => `${ONYXKEYS.COLLECTION.POLICY}${route.params.policyID}`, | ||
}, | ||
})(CreateDistanceRatePage); |
Oops, something went wrong.