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 all 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
8 changes: 6 additions & 2 deletions src/pages/workspace/categories/CategoryForm.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,12 @@ type CategoryFormProps = {

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

/** Function to validate the edited values of the form */
validateEdit?: (values: FormOnyxValues<typeof ONYXKEYS.FORMS.WORKSPACE_CATEGORY_FORM>) => FormInputErrors<typeof ONYXKEYS.FORMS.WORKSPACE_CATEGORY_FORM>;
mountiny marked this conversation as resolved.
Show resolved Hide resolved
};

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 +70,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
25 changes: 23 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,29 @@ 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(
mountiny marked this conversation as resolved.
Show resolved Hide resolved
(values: FormOnyxValues<typeof ONYXKEYS.FORMS.WORKSPACE_CATEGORY_FORM>) => {
Policy.renamePolicyCategory(route.params.policyID, {oldName: currentCategoryName, newName: values.categoryName});
const newCategoryName = values.categoryName.trim();
// Do not call the API if the edited category name is the same as the current category name
if (currentCategoryName !== newCategoryName) {
Policy.renamePolicyCategory(route.params.policyID, {oldName: currentCategoryName, newName: values.categoryName});
}
},
[currentCategoryName, route.params.policyID],
);
Expand All @@ -58,6 +78,7 @@ function EditCategoryPage({route, policyCategories}: EditCategoryPageProps) {
/>
<CategoryForm
onSubmit={editCategory}
validateEdit={validate}
categoryName={currentCategoryName}
policyCategories={policyCategories}
/>
Expand Down
6 changes: 5 additions & 1 deletion src/pages/workspace/tags/EditTagPage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,11 @@ 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();
mountiny marked this conversation as resolved.
Show resolved Hide resolved
// Do not call the API if the edited tag name is the same as the current tag name
if (currentTagName !== tagName) {
Policy.renamePolicyTag(route.params.policyID, {oldName: currentTagName, newName: values.tagName.trim()});
}
Keyboard.dismiss();
Navigation.dismissModal();
},
Expand Down
Loading