-
Notifications
You must be signed in to change notification settings - Fork 3.9k
[TS migration] Migrate WorkspaceReimburse Page #35399
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
Merged
tylerkaraszewski
merged 17 commits into
Expensify:main
from
ruben-rebelo:ts-migration/workspacereimburse-page
Mar 22, 2024
Merged
Changes from all commits
Commits
Show all changes
17 commits
Select commit
Hold shift + click to select a range
4feb092
[TS Migration] Migrate WorkspaceCard to Typescript
ruben-rebelo b974629
[TS migration] Migrate WorkspaceReimburse page
ruben-rebelo f51dfad
Revert "[TS Migration] Migrate WorkspaceCard to Typescript"
ruben-rebelo 3a7ad1e
[TS migration][WorkspaceReimburse] Missing imports
ruben-rebelo d11c98a
[TS migration][WorkspaceReimburse] Code improvements
ruben-rebelo 794d552
Merge branch 'main' into ts-migration/workspacereimburse-page
ruben-rebelo e66017b
Merge branch 'main' into ts-migration/workspacereimburse-page
ruben-rebelo 34135a2
[TS migration][WorkspaceReimburse] Code improvements
ruben-rebelo 7cd655d
Merge branch 'main' into ts-migration/workspacereimburse-page
ruben-rebelo 458be5e
[TS migration][WorkspaceReimburse] Ts issues fix
ruben-rebelo aafd0e5
[TS migration][WorkspaceReimburse] Lint
ruben-rebelo a04d571
Merge branch 'main' into ts-migration/workspacereimburse-page
ruben-rebelo c403cfa
Merge branch 'main' into ts-migration/workspacereimburse-page
ruben-rebelo 9cda701
Merge branch 'main' into ts-migration/workspacereimburse-page
ruben-rebelo 026d8e7
Merge branch 'main' into ts-migration/workspacereimburse-page
ruben-rebelo 64cfc7e
[TS migration][WorkspaceReimburse] TS issue fix
ruben-rebelo 1f240fd
Merge branch 'main' into ts-migration/workspacereimburse-page
ruben-rebelo 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 |
|---|---|---|
|
|
@@ -4623,3 +4623,5 @@ export { | |
| setPolicyDistanceRatesEnabled, | ||
| deletePolicyDistanceRates, | ||
| }; | ||
|
|
||
| export type {NewCustomUnit}; | ||
158 changes: 158 additions & 0 deletions
158
src/pages/workspace/reimburse/WorkspaceRateAndUnitPage.tsx
This file contains hidden or 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,158 @@ | ||
| import type {StackScreenProps} from '@react-navigation/stack'; | ||
| import React, {useEffect} from 'react'; | ||
| import {Keyboard, View} from 'react-native'; | ||
| import FormProvider from '@components/Form/FormProvider'; | ||
| import InputWrapper from '@components/Form/InputWrapper'; | ||
| import type {FormOnyxValues} from '@components/Form/types'; | ||
| import OfflineWithFeedback from '@components/OfflineWithFeedback'; | ||
| import Picker from '@components/Picker'; | ||
| import TextInput from '@components/TextInput'; | ||
| import useLocalize from '@hooks/useLocalize'; | ||
| import useThemeStyles from '@hooks/useThemeStyles'; | ||
| import * as CurrencyUtils from '@libs/CurrencyUtils'; | ||
| import getPermittedDecimalSeparator from '@libs/getPermittedDecimalSeparator'; | ||
| import Navigation from '@libs/Navigation/Navigation'; | ||
| import type {SettingsNavigatorParamList} from '@libs/Navigation/types'; | ||
| import * as NumberUtils from '@libs/NumberUtils'; | ||
| import * as PolicyUtils from '@libs/PolicyUtils'; | ||
| import withPolicy from '@pages/workspace/withPolicy'; | ||
| import type {WithPolicyProps} from '@pages/workspace/withPolicy'; | ||
| import WorkspacePageWithSections from '@pages/workspace/WorkspacePageWithSections'; | ||
| import * as BankAccounts from '@userActions/BankAccounts'; | ||
| import * as Policy from '@userActions/Policy'; | ||
| import CONST from '@src/CONST'; | ||
| import type {TranslationPaths} from '@src/languages/types'; | ||
| import ONYXKEYS from '@src/ONYXKEYS'; | ||
| import ROUTES from '@src/ROUTES'; | ||
| import type SCREENS from '@src/SCREENS'; | ||
| import type {Unit} from '@src/types/onyx/Policy'; | ||
|
|
||
| type WorkspaceRateAndUnitPageProps = WithPolicyProps & StackScreenProps<SettingsNavigatorParamList, typeof SCREENS.WORKSPACE.RATE_AND_UNIT>; | ||
|
|
||
| type ValidationError = {rate?: TranslationPaths | undefined}; | ||
|
|
||
| function WorkspaceRateAndUnitPage({policy, route}: WorkspaceRateAndUnitPageProps) { | ||
| const {translate, toLocaleDigit} = useLocalize(); | ||
| const styles = useThemeStyles(); | ||
|
|
||
| useEffect(() => { | ||
| if ((policy?.customUnits ?? []).length !== 0) { | ||
| return; | ||
| } | ||
|
|
||
| BankAccounts.setReimbursementAccountLoading(true); | ||
| Policy.openWorkspaceReimburseView(policy?.id ?? ''); | ||
| }, [policy?.customUnits, policy?.id]); | ||
|
|
||
| const unitItems = [ | ||
| {label: translate('common.kilometers'), value: CONST.CUSTOM_UNITS.DISTANCE_UNIT_KILOMETERS}, | ||
| {label: translate('common.miles'), value: CONST.CUSTOM_UNITS.DISTANCE_UNIT_MILES}, | ||
| ]; | ||
|
|
||
| const saveUnitAndRate = (unit: Unit, rate: string) => { | ||
| const distanceCustomUnit = Object.values(policy?.customUnits ?? {}).find((customUnit) => customUnit.name === CONST.CUSTOM_UNITS.NAME_DISTANCE); | ||
| if (!distanceCustomUnit) { | ||
| return; | ||
| } | ||
| const currentCustomUnitRate = Object.values(distanceCustomUnit?.rates ?? {}).find((r) => r.name === CONST.CUSTOM_UNITS.DEFAULT_RATE); | ||
| const unitID = distanceCustomUnit.customUnitID ?? ''; | ||
| const unitName = distanceCustomUnit.name ?? ''; | ||
| const rateNumValue = PolicyUtils.getNumericValue(rate, toLocaleDigit); | ||
|
|
||
| const newCustomUnit: Policy.NewCustomUnit = { | ||
| customUnitID: unitID, | ||
| name: unitName, | ||
| attributes: {unit}, | ||
| rates: { | ||
| ...currentCustomUnitRate, | ||
| rate: Number(rateNumValue) * CONST.POLICY.CUSTOM_UNIT_RATE_BASE_OFFSET, | ||
| }, | ||
| }; | ||
|
|
||
| Policy.updateWorkspaceCustomUnitAndRate(policy?.id ?? '', distanceCustomUnit, newCustomUnit, policy?.lastModified); | ||
| }; | ||
|
|
||
| const submit = (values: FormOnyxValues<typeof ONYXKEYS.FORMS.WORKSPACE_RATE_AND_UNIT_FORM>) => { | ||
| saveUnitAndRate(values.unit as Unit, values.rate); | ||
| Keyboard.dismiss(); | ||
| Navigation.goBack(ROUTES.WORKSPACE_REIMBURSE.getRoute(policy?.id ?? '')); | ||
| }; | ||
|
|
||
| const validate = (values: FormOnyxValues<typeof ONYXKEYS.FORMS.WORKSPACE_RATE_AND_UNIT_FORM>): ValidationError => { | ||
| const errors: ValidationError = {}; | ||
| const decimalSeparator = toLocaleDigit('.'); | ||
| const outputCurrency = policy?.outputCurrency ?? CONST.CURRENCY.USD; | ||
| // Allow one more decimal place for accuracy | ||
| const rateValueRegex = RegExp(String.raw`^-?\d{0,8}([${getPermittedDecimalSeparator(decimalSeparator)}]\d{1,${CurrencyUtils.getCurrencyDecimals(outputCurrency) + 1}})?$`, 'i'); | ||
| if (!rateValueRegex.test(values.rate) || values.rate === '') { | ||
| errors.rate = 'workspace.reimburse.invalidRateError'; | ||
| } else if (NumberUtils.parseFloatAnyLocale(values.rate) <= 0) { | ||
| errors.rate = 'workspace.reimburse.lowRateError'; | ||
| } | ||
| return errors; | ||
| }; | ||
|
|
||
| const distanceCustomUnit = Object.values(policy?.customUnits ?? {}).find((unit) => unit.name === CONST.CUSTOM_UNITS.NAME_DISTANCE); | ||
| const distanceCustomRate = Object.values(distanceCustomUnit?.rates ?? {}).find((rate) => rate.name === CONST.CUSTOM_UNITS.DEFAULT_RATE); | ||
|
|
||
| return ( | ||
| <WorkspacePageWithSections | ||
| headerText={translate('workspace.reimburse.trackDistance')} | ||
| route={route} | ||
| guidesCallTaskID={CONST.GUIDES_CALL_TASK_IDS.WORKSPACE_REIMBURSE} | ||
| shouldSkipVBBACall | ||
| backButtonRoute={ROUTES.WORKSPACE_REIMBURSE.getRoute(policy?.id ?? '')} | ||
| shouldShowLoading={false} | ||
| > | ||
| {() => ( | ||
| <FormProvider | ||
| formID={ONYXKEYS.FORMS.WORKSPACE_RATE_AND_UNIT_FORM} | ||
| submitButtonText={translate('common.save')} | ||
| style={[styles.mh5, styles.flexGrow1]} | ||
| scrollContextEnabled | ||
| validate={validate} | ||
| onSubmit={submit} | ||
| enabledWhenOffline | ||
| > | ||
| <OfflineWithFeedback | ||
| errors={{ | ||
| ...(distanceCustomUnit?.errors ?? {}), | ||
| ...(distanceCustomRate?.errors ?? {}), | ||
| }} | ||
| pendingAction={distanceCustomUnit?.pendingAction ?? distanceCustomRate?.pendingAction} | ||
| onClose={() => Policy.clearCustomUnitErrors(policy?.id ?? '', distanceCustomUnit?.customUnitID ?? '', distanceCustomRate?.customUnitRateID ?? '')} | ||
| > | ||
| <InputWrapper | ||
| InputComponent={TextInput} | ||
| role={CONST.ROLE.PRESENTATION} | ||
| inputID="rate" | ||
| containerStyles={styles.mt4} | ||
| defaultValue={PolicyUtils.getUnitRateValue(toLocaleDigit, distanceCustomRate)} | ||
| label={translate('workspace.reimburse.trackDistanceRate')} | ||
| aria-label={translate('workspace.reimburse.trackDistanceRate')} | ||
| placeholder={policy?.outputCurrency ?? CONST.CURRENCY.USD} | ||
| autoCompleteType="off" | ||
| autoCorrect={false} | ||
| inputMode={CONST.INPUT_MODE.DECIMAL} | ||
| maxLength={12} | ||
| /> | ||
|
|
||
| <View style={styles.mt4}> | ||
| <InputWrapper | ||
| InputComponent={Picker} | ||
| inputID="unit" | ||
| label={translate('workspace.reimburse.trackDistanceUnit')} | ||
| items={unitItems} | ||
| defaultValue={distanceCustomUnit?.attributes.unit ?? CONST.CUSTOM_UNITS.DISTANCE_UNIT_MILES} | ||
| /> | ||
| </View> | ||
| </OfflineWithFeedback> | ||
| </FormProvider> | ||
| )} | ||
| </WorkspacePageWithSections> | ||
| ); | ||
| } | ||
|
|
||
| WorkspaceRateAndUnitPage.displayName = 'WorkspaceRateAndUnitPage'; | ||
|
|
||
| export default withPolicy(WorkspaceRateAndUnitPage); |
This file was deleted.
Oops, something went wrong.
This file contains hidden or 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,33 @@ | ||
| import type {StackScreenProps} from '@react-navigation/stack'; | ||
| import React from 'react'; | ||
| import useLocalize from '@hooks/useLocalize'; | ||
| import type {WorkspacesCentralPaneNavigatorParamList} from '@libs/Navigation/types'; | ||
| import type {WithPolicyProps} from '@pages/workspace/withPolicy'; | ||
| import withPolicy from '@pages/workspace/withPolicy'; | ||
| import WorkspacePageWithSections from '@pages/workspace/WorkspacePageWithSections'; | ||
| import CONST from '@src/CONST'; | ||
| import type SCREENS from '@src/SCREENS'; | ||
| import WorkspaceReimburseView from './WorkspaceReimburseView'; | ||
|
|
||
| type WorkspaceReimbursePageProps = WithPolicyProps & StackScreenProps<WorkspacesCentralPaneNavigatorParamList, typeof SCREENS.WORKSPACE.REIMBURSE>; | ||
|
|
||
| function WorkspaceReimbursePage({route, policy}: WorkspaceReimbursePageProps) { | ||
| const {translate} = useLocalize(); | ||
|
|
||
| return ( | ||
| <WorkspacePageWithSections | ||
| shouldUseScrollView | ||
| headerText={translate('workspace.common.reimburse')} | ||
| route={route} | ||
| guidesCallTaskID={CONST.GUIDES_CALL_TASK_IDS.WORKSPACE_REIMBURSE} | ||
| shouldSkipVBBACall | ||
| shouldShowLoading={false} | ||
| > | ||
| {() => <WorkspaceReimburseView policy={policy} />} | ||
| </WorkspacePageWithSections> | ||
| ); | ||
| } | ||
|
|
||
| WorkspaceReimbursePage.displayName = 'WorkspaceReimbursePage'; | ||
|
|
||
| export default withPolicy(WorkspaceReimbursePage); | ||
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.
Uh oh!
There was an error while loading. Please reload this page.