|
| 1 | +import React, {useCallback} from 'react'; |
| 2 | +import type {ValueOf} from 'type-fest'; |
| 3 | +import RadioListItem from '@components/SelectionList/RadioListItem'; |
| 4 | +import type {ListItem} from '@components/SelectionList/types'; |
| 5 | +import SelectionScreen from '@components/SelectionScreen'; |
| 6 | +import type {SelectorType} from '@components/SelectionScreen'; |
| 7 | +import useLocalize from '@hooks/useLocalize'; |
| 8 | +import useThemeStyles from '@hooks/useThemeStyles'; |
| 9 | +import * as ErrorUtils from '@libs/ErrorUtils'; |
| 10 | +import {settingsPendingAction} from '@libs/PolicyUtils'; |
| 11 | +import Navigation from '@navigation/Navigation'; |
| 12 | +import type {WithPolicyConnectionsProps} from '@pages/workspace/withPolicyConnections'; |
| 13 | +import withPolicyConnections from '@pages/workspace/withPolicyConnections'; |
| 14 | +import {updateSageIntacctNonreimbursableExpensesExportDestination} from '@userActions/connections/SageIntacct'; |
| 15 | +import * as Policy from '@userActions/Policy/Policy'; |
| 16 | +import CONST from '@src/CONST'; |
| 17 | +import ROUTES from '@src/ROUTES'; |
| 18 | + |
| 19 | +type MenuListItem = ListItem & { |
| 20 | + value: ValueOf<typeof CONST.SAGE_INTACCT_NON_REIMBURSABLE_EXPENSE_TYPE>; |
| 21 | +}; |
| 22 | + |
| 23 | +function SageIntacctNonReimbursableExpensesDestinationPage({policy}: WithPolicyConnectionsProps) { |
| 24 | + const {translate} = useLocalize(); |
| 25 | + const styles = useThemeStyles(); |
| 26 | + const policyID = policy?.id ?? '-1'; |
| 27 | + const {config} = policy?.connections?.intacct ?? {}; |
| 28 | + |
| 29 | + const data: MenuListItem[] = Object.values(CONST.SAGE_INTACCT_NON_REIMBURSABLE_EXPENSE_TYPE).map((expenseType) => ({ |
| 30 | + value: expenseType, |
| 31 | + text: translate(`workspace.sageIntacct.nonReimbursableExpenses.values.${expenseType}`), |
| 32 | + keyForList: expenseType, |
| 33 | + isSelected: config?.export.nonReimbursable === expenseType, |
| 34 | + })); |
| 35 | + |
| 36 | + const selectDestination = useCallback( |
| 37 | + (row: MenuListItem) => { |
| 38 | + if (row.value !== config?.export.nonReimbursable) { |
| 39 | + updateSageIntacctNonreimbursableExpensesExportDestination(policyID, row.value, config?.export.nonReimbursable); |
| 40 | + } |
| 41 | + Navigation.goBack(ROUTES.POLICY_ACCOUNTING_SAGE_INTACCT_NON_REIMBURSABLE_EXPENSES.getRoute(policyID)); |
| 42 | + }, |
| 43 | + [config?.export.nonReimbursable, policyID], |
| 44 | + ); |
| 45 | + |
| 46 | + return ( |
| 47 | + <SelectionScreen |
| 48 | + displayName={SageIntacctNonReimbursableExpensesDestinationPage.displayName} |
| 49 | + title="workspace.accounting.exportAs" |
| 50 | + sections={[{data}]} |
| 51 | + listItem={RadioListItem} |
| 52 | + onSelectRow={(selection: SelectorType) => selectDestination(selection as MenuListItem)} |
| 53 | + initiallyFocusedOptionKey={data.find((mode) => mode.isSelected)?.keyForList} |
| 54 | + policyID={policyID} |
| 55 | + accessVariants={[CONST.POLICY.ACCESS_VARIANTS.ADMIN, CONST.POLICY.ACCESS_VARIANTS.PAID]} |
| 56 | + featureName={CONST.POLICY.MORE_FEATURES.ARE_CONNECTIONS_ENABLED} |
| 57 | + onBackButtonPress={() => Navigation.goBack(ROUTES.POLICY_ACCOUNTING_SAGE_INTACCT_NON_REIMBURSABLE_EXPENSES.getRoute(policyID))} |
| 58 | + connectionName={CONST.POLICY.CONNECTIONS.NAME.SAGE_INTACCT} |
| 59 | + pendingAction={settingsPendingAction([CONST.SAGE_INTACCT_CONFIG.NON_REIMBURSABLE], config?.pendingFields)} |
| 60 | + errors={ErrorUtils.getLatestErrorField(config, CONST.SAGE_INTACCT_CONFIG.NON_REIMBURSABLE)} |
| 61 | + errorRowStyles={[styles.ph5, styles.pv3]} |
| 62 | + onClose={() => Policy.clearSageIntacctErrorField(policyID, CONST.SAGE_INTACCT_CONFIG.NON_REIMBURSABLE)} |
| 63 | + /> |
| 64 | + ); |
| 65 | +} |
| 66 | + |
| 67 | +SageIntacctNonReimbursableExpensesDestinationPage.displayName = 'SageIntacctNonReimbursableExpensesDestinationPage'; |
| 68 | + |
| 69 | +export default withPolicyConnections(SageIntacctNonReimbursableExpensesDestinationPage); |
0 commit comments