Skip to content
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

[FIX]: Inconsistent behavior when saving existing name without edit and unnecessary API calls #38757

Merged
merged 7 commits into from
Mar 26, 2024
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions src/pages/workspace/categories/CategoryForm.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,11 @@ type CategoryFormProps = {

/** Function to call when the form is submitted */
onSubmit: (values: FormOnyxValues<typeof ONYXKEYS.FORMS.WORKSPACE_CATEGORY_FORM>) => void;

validateEdit?: (values: FormOnyxValues<typeof ONYXKEYS.FORMS.WORKSPACE_CATEGORY_FORM>) => FormInputErrors<typeof ONYXKEYS.FORMS.WORKSPACE_CATEGORY_FORM>;
};

function CategoryForm({onSubmit, policyCategories, categoryName}: CategoryFormProps) {
function CategoryForm({onSubmit, policyCategories, categoryName, validateEdit}: CategoryFormProps) {
const styles = useThemeStyles();
const {translate} = useLocalize();
const {inputCallbackRef} = useAutoFocusInput();
Expand Down Expand Up @@ -67,7 +69,8 @@ function CategoryForm({onSubmit, policyCategories, categoryName}: CategoryFormPr
formID={ONYXKEYS.FORMS.WORKSPACE_CATEGORY_FORM}
onSubmit={submit}
submitButtonText={translate('common.save')}
validate={validate}
// eslint-disable-next-line @typescript-eslint/prefer-nullish-coalescing
validate={validateEdit || validate}
style={[styles.mh5, styles.flex1]}
enabledWhenOffline
>
Expand Down
24 changes: 22 additions & 2 deletions src/pages/workspace/categories/EditCategoryPage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import type {StackScreenProps} from '@react-navigation/stack';
import React, {useCallback} from 'react';
import {withOnyx} from 'react-native-onyx';
import type {OnyxEntry} from 'react-native-onyx';
import type {FormOnyxValues} from '@components/Form/types';
import type {FormInputErrors, FormOnyxValues} from '@components/Form/types';
import HeaderWithBackButton from '@components/HeaderWithBackButton';
import ScreenWrapper from '@components/ScreenWrapper';
import useLocalize from '@hooks/useLocalize';
Expand Down Expand Up @@ -32,9 +32,28 @@ function EditCategoryPage({route, policyCategories}: EditCategoryPageProps) {
const {translate} = useLocalize();
const currentCategoryName = route.params.categoryName;

const validate = useCallback(
(values: FormOnyxValues<typeof ONYXKEYS.FORMS.WORKSPACE_CATEGORY_FORM>) => {
const errors: FormInputErrors<typeof ONYXKEYS.FORMS.WORKSPACE_CATEGORY_FORM> = {};
const newCategoryName = values.categoryName.trim();

if (!newCategoryName) {
errors.categoryName = 'workspace.categories.categoryRequiredError';
} else if (policyCategories?.[newCategoryName] && currentCategoryName !== newCategoryName) {
errors.categoryName = 'workspace.categories.existingCategoryError';
}

return errors;
},
[policyCategories, currentCategoryName],
);

const editCategory = useCallback(
(values: FormOnyxValues<typeof ONYXKEYS.FORMS.WORKSPACE_CATEGORY_FORM>) => {
Policy.renamePolicyCategory(route.params.policyID, {oldName: currentCategoryName, newName: values.categoryName});
const newCategoryName = values.categoryName.trim();
if (currentCategoryName !== newCategoryName) {
Policy.renamePolicyCategory(route.params.policyID, {oldName: currentCategoryName, newName: values.categoryName});
}
},
[currentCategoryName, route.params.policyID],
);
Expand All @@ -58,6 +77,7 @@ function EditCategoryPage({route, policyCategories}: EditCategoryPageProps) {
/>
<CategoryForm
onSubmit={editCategory}
validateEdit={validate}
categoryName={currentCategoryName}
policyCategories={policyCategories}
/>
Expand Down
5 changes: 4 additions & 1 deletion src/pages/workspace/tags/EditTagPage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,10 @@ function EditTagPage({route, policyTags}: EditTagPageProps) {

const editTag = useCallback(
(values: FormOnyxValues<typeof ONYXKEYS.FORMS.WORKSPACE_TAG_FORM>) => {
Policy.renamePolicyTag(route.params.policyID, {oldName: currentTagName, newName: values.tagName.trim()});
const tagName = values.tagName.trim();
if (currentTagName !== tagName) {
Policy.renamePolicyTag(route.params.policyID, {oldName: currentTagName, newName: values.tagName.trim()});
}
Keyboard.dismiss();
Navigation.dismissModal();
},
Expand Down
Loading