|
| 1 | +import type {StackScreenProps} from '@react-navigation/stack'; |
| 2 | +import React from 'react'; |
| 3 | +import {useOnyx} from 'react-native-onyx'; |
| 4 | +import FormProvider from '@components/Form/FormProvider'; |
| 5 | +import InputWrapper from '@components/Form/InputWrapper'; |
| 6 | +import type {FormInputErrors, FormOnyxValues} from '@components/Form/types'; |
| 7 | +import HeaderWithBackButton from '@components/HeaderWithBackButton'; |
| 8 | +import ScreenWrapper from '@components/ScreenWrapper'; |
| 9 | +import TextInput from '@components/TextInput'; |
| 10 | +import useAutoFocusInput from '@hooks/useAutoFocusInput'; |
| 11 | +import useLocalize from '@hooks/useLocalize'; |
| 12 | +import useThemeStyles from '@hooks/useThemeStyles'; |
| 13 | +import * as ValidationUtils from '@libs/ValidationUtils'; |
| 14 | +import Navigation from '@navigation/Navigation'; |
| 15 | +import type {SettingsNavigatorParamList} from '@navigation/types'; |
| 16 | +import AccessOrNotFoundWrapper from '@pages/workspace/AccessOrNotFoundWrapper'; |
| 17 | +import CONST from '@src/CONST'; |
| 18 | +import ONYXKEYS from '@src/ONYXKEYS'; |
| 19 | +import type SCREENS from '@src/SCREENS'; |
| 20 | +import INPUT_IDS from '@src/types/form/EditExpensifyCardNameForm'; |
| 21 | + |
| 22 | +// TODO: remove when Onyx data is available |
| 23 | +const mockedCard = { |
| 24 | + accountID: 885646, |
| 25 | + availableSpend: 1000, |
| 26 | + nameValuePairs: { |
| 27 | + cardTitle: 'Test 1', |
| 28 | + isVirtual: true, |
| 29 | + limit: 2000, |
| 30 | + limitType: CONST.EXPENSIFY_CARD.LIMIT_TYPES.SMART, |
| 31 | + }, |
| 32 | + lastFourPAN: '1234', |
| 33 | +}; |
| 34 | + |
| 35 | +type WorkspaceEditCardNamePageProps = StackScreenProps<SettingsNavigatorParamList, typeof SCREENS.WORKSPACE.EXPENSIFY_CARD_NAME>; |
| 36 | + |
| 37 | +function WorkspaceEditCardNamePage({route}: WorkspaceEditCardNamePageProps) { |
| 38 | + const {policyID, cardID} = route.params; |
| 39 | + |
| 40 | + const {translate} = useLocalize(); |
| 41 | + const {inputCallbackRef} = useAutoFocusInput(); |
| 42 | + const styles = useThemeStyles(); |
| 43 | + |
| 44 | + const [cardsList] = useOnyx(`${ONYXKEYS.COLLECTION.WORKSPACE_CARDS_LIST}${policyID}_${CONST.EXPENSIFY_CARD.BANK}`); |
| 45 | + const card = cardsList?.[cardID] ?? mockedCard; |
| 46 | + |
| 47 | + // eslint-disable-next-line @typescript-eslint/no-unused-vars |
| 48 | + const submit = (values: FormOnyxValues<typeof ONYXKEYS.FORMS.EDIT_EXPENSIFY_CARD_NAME_FORM>) => { |
| 49 | + // TODO: add API call when it's supported https://github.com/Expensify/Expensify/issues/407832 |
| 50 | + Navigation.goBack(); |
| 51 | + }; |
| 52 | + |
| 53 | + const validate = (values: FormOnyxValues<typeof ONYXKEYS.FORMS.EDIT_EXPENSIFY_CARD_NAME_FORM>): FormInputErrors<typeof ONYXKEYS.FORMS.EDIT_EXPENSIFY_CARD_NAME_FORM> => |
| 54 | + ValidationUtils.getFieldRequiredErrors(values, [INPUT_IDS.NAME]); |
| 55 | + |
| 56 | + return ( |
| 57 | + <AccessOrNotFoundWrapper |
| 58 | + accessVariants={[CONST.POLICY.ACCESS_VARIANTS.ADMIN, CONST.POLICY.ACCESS_VARIANTS.PAID]} |
| 59 | + policyID={policyID} |
| 60 | + featureName={CONST.POLICY.MORE_FEATURES.ARE_EXPENSIFY_CARDS_ENABLED} |
| 61 | + > |
| 62 | + <ScreenWrapper |
| 63 | + testID={WorkspaceEditCardNamePage.displayName} |
| 64 | + shouldEnablePickerAvoiding={false} |
| 65 | + shouldEnableMaxHeight |
| 66 | + > |
| 67 | + <HeaderWithBackButton title={translate('workspace.card.issueNewCard.cardName')} /> |
| 68 | + <FormProvider |
| 69 | + formID={ONYXKEYS.FORMS.EDIT_EXPENSIFY_CARD_NAME_FORM} |
| 70 | + submitButtonText={translate('common.save')} |
| 71 | + onSubmit={submit} |
| 72 | + style={[styles.flex1, styles.mh5]} |
| 73 | + enabledWhenOffline |
| 74 | + validate={validate} |
| 75 | + > |
| 76 | + <InputWrapper |
| 77 | + InputComponent={TextInput} |
| 78 | + inputID={INPUT_IDS.NAME} |
| 79 | + label={translate('workspace.card.issueNewCard.cardName')} |
| 80 | + hint={translate('workspace.card.issueNewCard.giveItNameInstruction')} |
| 81 | + aria-label={translate('workspace.card.issueNewCard.cardName')} |
| 82 | + role={CONST.ROLE.PRESENTATION} |
| 83 | + defaultValue={card?.nameValuePairs?.cardTitle} |
| 84 | + ref={inputCallbackRef} |
| 85 | + /> |
| 86 | + </FormProvider> |
| 87 | + </ScreenWrapper> |
| 88 | + </AccessOrNotFoundWrapper> |
| 89 | + ); |
| 90 | +} |
| 91 | + |
| 92 | +WorkspaceEditCardNamePage.displayName = 'WorkspaceEditCardNamePage'; |
| 93 | + |
| 94 | +export default WorkspaceEditCardNamePage; |
0 commit comments