Skip to content

Commit

Permalink
Merge pull request #38555 from software-mansion-labs/wave8/WorkspaceT…
Browse files Browse the repository at this point in the history
…axesSettings-customNameValidation

[Simplified Collect][Taxes] Add validation for custom tax name field
  • Loading branch information
luacmartins authored Mar 21, 2024
2 parents 1a8b66a + fa90c4f commit 69630f6
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 6 deletions.
1 change: 1 addition & 0 deletions src/CONST.ts
Original file line number Diff line number Diff line change
Expand Up @@ -359,6 +359,7 @@ const CONST = {
NOT_INSTALLED: 'not-installed',
},
TAX_RATES: {
CUSTOM_NAME_MAX_LENGTH: 8,
NAME_MAX_LENGTH: 50,
},
PLATFORM: {
Expand Down
8 changes: 4 additions & 4 deletions src/components/TaxPicker.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -53,10 +53,10 @@ function TaxPicker({selectedTaxRate = '', taxRates, insets, onSubmit}: TaxPicker
];
}, [selectedTaxRate, getTaxName]);

const sections = useMemo(() => {
const taxRatesOptions = OptionsListUtils.getTaxRatesSection(taxRates, selectedOptions as OptionsListUtils.Category[], searchValue, selectedTaxRate);
return taxRatesOptions;
}, [taxRates, searchValue, selectedOptions, selectedTaxRate]);
const sections = useMemo(
() => OptionsListUtils.getTaxRatesSection(taxRates, selectedOptions as OptionsListUtils.Category[], searchValue, selectedTaxRate),
[taxRates, searchValue, selectedOptions, selectedTaxRate],
);

const headerMessage = OptionsListUtils.getHeaderMessageForNonUserList(sections[0].data.length > 0, searchValue);

Expand Down
1 change: 1 addition & 0 deletions src/languages/en.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1894,6 +1894,7 @@ export default {
errors: {
taxRateAlreadyExists: 'This tax name is already in use.',
valuePercentageRange: 'Please enter a valid percentage between 0 and 100.',
customNameRequired: 'Custom tax name is required.',
deleteFailureMessage: 'An error occurred while deleting the tax rate. Please try again or ask Concierge for help.',
updateFailureMessage: 'An error occurred while updating the tax rate. Please try again or ask Concierge for help.',
createFailureMessage: 'An error occurred while creating the tax rate. Please try again or ask Concierge for help.',
Expand Down
1 change: 1 addition & 0 deletions src/languages/es.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1920,6 +1920,7 @@ export default {
value: 'Valor',
errors: {
taxRateAlreadyExists: 'Ya existe un impuesto con este nombre',
customNameRequired: 'El nombre del impuesto es obligatorio.',
valuePercentageRange: 'Por favor, introduce un porcentaje entre 0 y 100',
deleteFailureMessage: 'Se ha producido un error al intentar eliminar la tasa de impuesto. Por favor, inténtalo más tarde.',
updateFailureMessage: 'Se ha producido un error al intentar modificar la tasa de impuesto. Por favor, inténtalo más tarde.',
Expand Down
18 changes: 16 additions & 2 deletions src/pages/workspace/taxes/WorkspaceTaxesSettingsCustomTaxName.tsx
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import type {StackScreenProps} from '@react-navigation/stack';
import React from 'react';
import React, {useCallback} from 'react';
import {View} from 'react-native';
import FormProvider from '@components/Form/FormProvider';
import InputWrapper from '@components/Form/InputWrapper';
import type {FormInputErrors, FormOnyxValues} from '@components/Form/types';
import HeaderWithBackButton from '@components/HeaderWithBackButton';
import ScreenWrapper from '@components/ScreenWrapper';
import TextInput from '@components/TextInput';
Expand All @@ -12,6 +13,7 @@ import useThemeStyles from '@hooks/useThemeStyles';
import {setPolicyCustomTaxName} from '@libs/actions/Policy';
import Navigation from '@libs/Navigation/Navigation';
import type {SettingsNavigatorParamList} from '@libs/Navigation/types';
import * as ValidationUtils from '@libs/ValidationUtils';
import AdminPolicyAccessOrNotFoundWrapper from '@pages/workspace/AdminPolicyAccessOrNotFoundWrapper';
import FeatureEnabledAccessOrNotFoundWrapper from '@pages/workspace/FeatureEnabledAccessOrNotFoundWrapper';
import PaidPolicyAccessOrNotFoundWrapper from '@pages/workspace/PaidPolicyAccessOrNotFoundWrapper';
Expand All @@ -36,6 +38,17 @@ function WorkspaceTaxesSettingsCustomTaxName({
const {translate} = useLocalize();
const {inputCallbackRef} = useAutoFocusInput();

const validate = useCallback((values: FormOnyxValues<typeof ONYXKEYS.FORMS.WORKSPACE_TAX_CUSTOM_NAME>) => {
const errors: FormInputErrors<typeof ONYXKEYS.FORMS.WORKSPACE_TAX_CUSTOM_NAME> = {};
const customTaxName = values[INPUT_IDS.NAME];

if (!ValidationUtils.isRequiredFulfilled(customTaxName)) {
errors.name = 'workspace.taxes.errors.customNameRequired';
}

return errors;
}, []);

const submit = ({name}: WorkspaceTaxCustomName) => {
setPolicyCustomTaxName(policyID, name);
Navigation.goBack(ROUTES.WORKSPACE_TAXES_SETTINGS.getRoute(policyID));
Expand All @@ -62,6 +75,7 @@ function WorkspaceTaxesSettingsCustomTaxName({
style={[styles.flexGrow1, styles.ph5]}
scrollContextEnabled
enabledWhenOffline
validate={validate}
onSubmit={submit}
>
<View style={styles.mb4}>
Expand All @@ -72,7 +86,7 @@ function WorkspaceTaxesSettingsCustomTaxName({
label={translate('workspace.editor.nameInputLabel')}
accessibilityLabel={translate('workspace.editor.nameInputLabel')}
defaultValue={policy?.taxRates?.name}
maxLength={CONST.TAX_RATES.NAME_MAX_LENGTH}
maxLength={CONST.TAX_RATES.CUSTOM_NAME_MAX_LENGTH}
multiline={false}
ref={inputCallbackRef}
/>
Expand Down

0 comments on commit 69630f6

Please sign in to comment.