-
Notifications
You must be signed in to change notification settings - Fork 3.1k
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
feat: policy add distance rate #38035
Merged
luacmartins
merged 22 commits into
Expensify:main
from
MrMuzyk:feat/policy-add-distance-rate
Mar 15, 2024
Merged
Changes from all commits
Commits
Show all changes
22 commits
Select commit
Hold shift + click to select a range
c14fbf8
feat: PolicyNewDistanceRatePage
MrMuzyk b20f519
Merge branch 'feat/policy-distance-rates-page' of github.com:MrMuzyk/…
MrMuzyk 729c746
Merge branch 'feat/policy-distance-rates-page' of github.com:MrMuzyk/…
MrMuzyk 84115b4
Merge branch 'feat/policy-distance-rates-page' of github.com:MrMuzyk/…
MrMuzyk f7567c3
feat: adding delete
MrMuzyk c74c9fb
Merge branch 'main' of https://github.com/Expensify/App into feat/pol…
MrMuzyk d0d8d2c
feat: add
MrMuzyk d27b9fe
fix: ts error
MrMuzyk cfa9145
fix: scrap delete from this PR
MrMuzyk 2b1baf7
fix: one line
MrMuzyk 1b53b39
feat: add validation
MrMuzyk f24b378
Merge branch 'main' of https://github.com/Expensify/App into feat/pol…
MrMuzyk bf5ba1d
Merge branch 'main' of https://github.com/Expensify/App into feat/pol…
MrMuzyk f6df548
fix: cr fixes
MrMuzyk ba3f3b3
fix: error handling
MrMuzyk ddeabce
Merge branch 'main' of https://github.com/Expensify/App into feat/pol…
MrMuzyk 94638fc
Merge branch 'main' of https://github.com/Expensify/App into feat/pol…
MrMuzyk 0fd5258
fix: cr fixes
MrMuzyk d04d2a3
fix: last cr fixes
MrMuzyk 9522399
Merge branch 'main' of https://github.com/Expensify/App into feat/pol…
MrMuzyk 78bd350
fix: move validate to util
MrMuzyk e096ce3
fix: remove extra const
MrMuzyk File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -10,6 +10,7 @@ import type {ValueOf} from 'type-fest'; | |
import * as API from '@libs/API'; | ||
import type { | ||
AddMembersToWorkspaceParams, | ||
CreatePolicyDistanceRateParams, | ||
CreateWorkspaceFromIOUPaymentParams, | ||
CreateWorkspaceParams, | ||
DeleteMembersFromWorkspaceParams, | ||
|
@@ -3621,6 +3622,83 @@ function openPolicyMoreFeaturesPage(policyID: string) { | |
API.read(READ_COMMANDS.OPEN_POLICY_MORE_FEATURES_PAGE, params); | ||
} | ||
|
||
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. Case missed when updating workspace currency distance rates are reset based on old logic which had one distance rate: #30156 |
||
function createPolicyDistanceRate(policyID: string, customUnitID: string, customUnitRate: Rate) { | ||
const optimisticData: OnyxUpdate[] = [ | ||
{ | ||
onyxMethod: Onyx.METHOD.MERGE, | ||
key: `${ONYXKEYS.COLLECTION.POLICY}${policyID}`, | ||
value: { | ||
customUnits: { | ||
[customUnitID]: { | ||
rates: { | ||
[customUnitRate.customUnitRateID ?? '']: { | ||
...customUnitRate, | ||
pendingAction: CONST.RED_BRICK_ROAD_PENDING_ACTION.ADD, | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
]; | ||
|
||
const successData: OnyxUpdate[] = [ | ||
{ | ||
onyxMethod: Onyx.METHOD.MERGE, | ||
key: `${ONYXKEYS.COLLECTION.POLICY}${policyID}`, | ||
value: { | ||
customUnits: { | ||
[customUnitID]: { | ||
rates: { | ||
[customUnitRate.customUnitRateID ?? '']: { | ||
pendingAction: null, | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
]; | ||
|
||
const failureData: OnyxUpdate[] = [ | ||
{ | ||
onyxMethod: Onyx.METHOD.MERGE, | ||
key: `${ONYXKEYS.COLLECTION.POLICY}${policyID}`, | ||
value: { | ||
customUnits: { | ||
[customUnitID]: { | ||
rates: { | ||
[customUnitRate.customUnitRateID ?? '']: { | ||
errors: ErrorUtils.getMicroSecondOnyxError('common.genericErrorMessage'), | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
]; | ||
|
||
const params: CreatePolicyDistanceRateParams = { | ||
policyID, | ||
customUnitID, | ||
customUnitRate: JSON.stringify(customUnitRate), | ||
}; | ||
|
||
API.write(WRITE_COMMANDS.CREATE_POLICY_DISTANCE_RATE, params, {optimisticData, successData, failureData}); | ||
} | ||
|
||
function clearCreateDistanceRateItemAndError(policyID: string, customUnitID: string, customUnitRateIDToClear: string) { | ||
Onyx.merge(`${ONYXKEYS.COLLECTION.POLICY}${policyID}`, { | ||
customUnits: { | ||
[customUnitID]: { | ||
rates: { | ||
[customUnitRateIDToClear]: null, | ||
}, | ||
}, | ||
}, | ||
}); | ||
} | ||
|
||
export { | ||
removeMembers, | ||
updateWorkspaceMembersRole, | ||
|
@@ -3693,6 +3771,9 @@ export { | |
enablePolicyWorkflows, | ||
openPolicyDistanceRatesPage, | ||
openPolicyMoreFeaturesPage, | ||
generateCustomUnitID, | ||
createPolicyDistanceRate, | ||
clearCreateDistanceRateItemAndError, | ||
createPolicyTag, | ||
clearPolicyTagErrors, | ||
clearWorkspaceReimbursementErrors, | ||
|
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.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We miss to check the duplicated value and it caused this problem #51769