Skip to content

Commit ca62cb0

Browse files
Extract sanitizeCategoryName helper to deduplicate category name normalization
Co-authored-by: Monil Bhavsar <MonilBhavsar@users.noreply.github.com>
1 parent d7bd61a commit ca62cb0

1 file changed

Lines changed: 6 additions & 4 deletions

File tree

src/pages/workspace/categories/EditCategoryPage.tsx

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,10 +30,12 @@ function EditCategoryPage({route}: EditCategoryPageProps) {
3030
const isQuickSettingsFlow = route.name === SCREENS.SETTINGS_CATEGORIES.DYNAMIC_SETTINGS_CATEGORY_EDIT;
3131
const backPath = useDynamicBackPath(DYNAMIC_ROUTES.SETTINGS_CATEGORY_EDIT.path);
3232

33+
const sanitizeCategoryName = useCallback((name: string) => name.replaceAll(CONST.REGEX.NON_BREAKING_SPACE, ' ').trim(), []);
34+
3335
const validate = useCallback(
3436
(values: FormOnyxValues<typeof ONYXKEYS.FORMS.WORKSPACE_CATEGORY_FORM>) => {
3537
const errors: FormInputErrors<typeof ONYXKEYS.FORMS.WORKSPACE_CATEGORY_FORM> = {};
36-
const newCategoryName = values.categoryName.replaceAll(CONST.REGEX.NON_BREAKING_SPACE, ' ').trim();
38+
const newCategoryName = sanitizeCategoryName(values.categoryName);
3739

3840
if (!newCategoryName) {
3941
errors.categoryName = translate('workspace.categories.categoryRequiredError');
@@ -45,12 +47,12 @@ function EditCategoryPage({route}: EditCategoryPageProps) {
4547
}
4648
return errors;
4749
},
48-
[policyCategories, currentCategoryName, translate],
50+
[policyCategories, currentCategoryName, translate, sanitizeCategoryName],
4951
);
5052

5153
const editCategory = useCallback(
5254
(values: FormOnyxValues<typeof ONYXKEYS.FORMS.WORKSPACE_CATEGORY_FORM>) => {
53-
const newCategoryName = values.categoryName.replaceAll(CONST.REGEX.NON_BREAKING_SPACE, ' ').trim();
55+
const newCategoryName = sanitizeCategoryName(values.categoryName);
5456
// Do not call the API if the edited category name is the same as the current category name
5557
if (currentCategoryName !== newCategoryName) {
5658
renamePolicyCategory(policyData, {oldName: currentCategoryName, newName: newCategoryName});
@@ -61,7 +63,7 @@ function EditCategoryPage({route}: EditCategoryPageProps) {
6163
Navigation.goBack(isQuickSettingsFlow ? backPath : ROUTES.WORKSPACE_CATEGORY_SETTINGS.getRoute(policyID, currentCategoryName), {compareParams: false});
6264
});
6365
},
64-
[currentCategoryName, policyData, isQuickSettingsFlow, backPath, policyID],
66+
[currentCategoryName, policyData, isQuickSettingsFlow, backPath, policyID, sanitizeCategoryName],
6567
);
6668

6769
return (

0 commit comments

Comments
 (0)