Skip to content

Commit

Permalink
Merge pull request #49791 from etCoderDysto/report-fields-validation
Browse files Browse the repository at this point in the history
fix: Display error message when saving the same report list value
  • Loading branch information
Julesssss authored Oct 8, 2024
2 parents 867bbb4 + d23de9c commit 8b27c8d
Showing 1 changed file with 15 additions and 3 deletions.
18 changes: 15 additions & 3 deletions src/pages/workspace/reportFields/ReportFieldsAddListValuePage.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import type {StackScreenProps} from '@react-navigation/stack';
import React, {useCallback} from 'react';
import React, {useCallback, useMemo} from 'react';
import {Keyboard} from 'react-native';
import {useOnyx} from 'react-native-onyx';
import FormProvider from '@components/Form/FormProvider';
Expand All @@ -14,6 +14,7 @@ import useThemeStyles from '@hooks/useThemeStyles';
import * as ReportField from '@libs/actions/Policy/ReportField';
import Navigation from '@libs/Navigation/Navigation';
import * as PolicyUtils from '@libs/PolicyUtils';
import * as ReportUtils from '@libs/ReportUtils';
import * as WorkspaceReportFieldUtils from '@libs/WorkspaceReportFieldUtils';
import type {SettingsNavigatorParamList} from '@navigation/types';
import AccessOrNotFoundWrapper from '@pages/workspace/AccessOrNotFoundWrapper';
Expand All @@ -37,10 +38,21 @@ function ReportFieldsAddListValuePage({
const {inputCallbackRef} = useAutoFocusInput();
const [formDraft] = useOnyx(ONYXKEYS.FORMS.WORKSPACE_REPORT_FIELDS_FORM_DRAFT);

const listValues = useMemo(() => {
let reportFieldListValues: string[];
if (reportFieldID) {
const reportFieldKey = ReportUtils.getReportFieldKey(reportFieldID);
reportFieldListValues = Object.values(policy?.fieldList?.[reportFieldKey]?.values ?? {});
} else {
reportFieldListValues = formDraft?.[INPUT_IDS.LIST_VALUES] ?? [];
}
return reportFieldListValues;
}, [formDraft, policy?.fieldList, reportFieldID]);

const validate = useCallback(
(values: FormOnyxValues<typeof ONYXKEYS.FORMS.WORKSPACE_REPORT_FIELDS_FORM>) =>
WorkspaceReportFieldUtils.validateReportFieldListValueName(values[INPUT_IDS.VALUE_NAME].trim(), '', formDraft?.[INPUT_IDS.LIST_VALUES] ?? [], INPUT_IDS.VALUE_NAME),
[formDraft],
WorkspaceReportFieldUtils.validateReportFieldListValueName(values[INPUT_IDS.VALUE_NAME].trim(), '', listValues, INPUT_IDS.VALUE_NAME),
[listValues],
);

const createValue = useCallback(
Expand Down

0 comments on commit 8b27c8d

Please sign in to comment.