Skip to content

Commit 2bef11c

Browse files
authored
fix: [Assets --> Applications] Changes to attachment types affect both current and new version (Issue #2128) (#2135)
1 parent a2a5cf9 commit 2bef11c

File tree

3 files changed

+45
-32
lines changed

3 files changed

+45
-32
lines changed

apps/ai-dial-admin/src/components/Common/AttachmentInput/AttachmentInput.spec.tsx

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { fireEvent, render, screen, waitFor, within } from '@testing-library/react';
22
import userEvent from '@testing-library/user-event';
33
import { afterEach, beforeAll, describe, expect, test, vi } from 'vitest';
4+
import { useState } from 'react';
45

56
import { AttachmentsI18nKey } from '@/src/constants/i18n';
67

@@ -15,6 +16,26 @@ const placeHolder = 'Attachment Input';
1516

1617
const onChange = vi.fn();
1718

19+
// Wrapper component to simulate controlled behavior
20+
const ControlledAttachmentInput = (props: Partial<Props> & { initialValues?: string[] }) => {
21+
const [values, setValues] = useState<string[] | undefined>(props.initialValues);
22+
23+
const handleChange = (newValues?: string[]) => {
24+
setValues(newValues);
25+
onChange(newValues);
26+
};
27+
28+
return (
29+
<AttachmentInput
30+
{...props}
31+
availableItems={options}
32+
onChange={handleChange}
33+
placeholder={placeHolder}
34+
initialValues={values}
35+
/>
36+
);
37+
};
38+
1839
describe('Common components - AttachmentInput', () => {
1940
beforeAll(() => {
2041
const resizeObserverMock = vi.fn().mockImplementation(() => ({
@@ -123,5 +144,5 @@ describe('Common components - AttachmentInput', () => {
123144
});
124145

125146
function renderComponent(extra: Partial<Props> = {}) {
126-
return render(<AttachmentInput availableItems={options} onChange={onChange} placeholder={placeHolder} {...extra} />);
147+
return render(<ControlledAttachmentInput {...extra} />);
127148
}

apps/ai-dial-admin/src/components/Common/AttachmentInput/AttachmentInput.tsx

Lines changed: 21 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ import Field from '@/src/components/Common/Field/Field';
1010
import { ALL_ATTACHMENTS } from '@/src/constants/dial-base-entity';
1111
import { AttachmentsI18nKey } from '@/src/constants/i18n';
1212
import { CONTROL_WITH_BUTTON_WIDTH, STANDARD_CONTROL_WIDTH } from '@/src/constants/main-layout';
13-
import { useSaveValidationContext } from '@/src/context/SaveValidationContext';
1413
import { useI18n } from '@/src/locales/client';
1514
import Suggestions from './Suggestions';
1615

@@ -32,44 +31,42 @@ export interface Props {
3231
fieldTitle?: string;
3332
elementId?: string;
3433
optional?: boolean;
35-
onChange?: (values: string[]) => void;
34+
onChange?: (values?: string[]) => void;
3635
}
3736

3837
const ALL_ATTACHMENTS_VALUE = [{ label: ALL_ATTACHMENTS, value: ALL_ATTACHMENTS }];
3938

4039
const AttachmentInput: FC<Props> = ({
4140
availableItems,
42-
initialValues = [],
41+
initialValues,
4342
fieldTitle,
4443
placeholder,
4544
elementId,
4645
optional,
4746
onChange,
4847
}) => {
4948
const t = useI18n();
50-
const { resetCounter } = useSaveValidationContext();
5149
const containerRef = useRef<HTMLDivElement>(null);
5250

53-
const initialSelected = useMemo(() => {
51+
const selected = useMemo(() => {
52+
if (!initialValues) return [];
5453
return initialValues
5554
.map((val) => availableItems.find((o) => o.value === val) || { label: val, value: val })
5655
.filter(Boolean) as AttachmentOption[];
5756
}, [availableItems, initialValues]);
5857

59-
const initialRadio = useMemo(() => {
60-
return initialValues.length === 0
61-
? AttachmentType.NONE
62-
: isEqual(initialSelected, ALL_ATTACHMENTS_VALUE)
63-
? AttachmentType.ALL
64-
: AttachmentType.SPECIFIC;
65-
}, [initialSelected, initialValues.length]);
58+
const selectedRadio = useMemo(() => {
59+
if (!initialValues) {
60+
return AttachmentType.NONE;
61+
} else {
62+
return isEqual(selected, ALL_ATTACHMENTS_VALUE) ? AttachmentType.ALL : AttachmentType.SPECIFIC;
63+
}
64+
}, [initialValues, selected]);
6665

67-
const [selected, setSelected] = useState<AttachmentOption[]>(initialSelected);
6866
const [inputValue, setInputValue] = useState('');
6967
const [wraps, setWraps] = useState(false);
7068
const [showSuggestions, setShowSuggestions] = useState(false);
7169
const [highlight, setHighlight] = useState(0);
72-
const [selectedRadio, setSelectedRadio] = useState<AttachmentType>(initialRadio);
7370

7471
const filteredSuggestions = useMemo(() => {
7572
return availableItems
@@ -86,14 +83,6 @@ const AttachmentInput: FC<Props> = ({
8683
return selectedRadio === AttachmentType.SPECIFIC && showSuggestions && filteredSuggestions.length > 0;
8784
}, [filteredSuggestions.length, selectedRadio, showSuggestions]);
8885

89-
useEffect(() => {
90-
if (resetCounter) {
91-
setSelected(initialSelected);
92-
setSelectedRadio(initialRadio);
93-
}
94-
// eslint-disable-next-line react-hooks/exhaustive-deps
95-
}, [resetCounter]);
96-
9786
useEffect(() => {
9887
const observer = new ResizeObserver(() => {
9988
if (containerRef.current) {
@@ -110,15 +99,18 @@ const AttachmentInput: FC<Props> = ({
11099
}, []);
111100

112101
const fireChange = useCallback(
113-
(items: AttachmentOption[]) => {
114-
onChange?.(items.map((i) => i.value));
102+
(items?: AttachmentOption[]) => {
103+
if (!items) {
104+
onChange?.(void 0);
105+
return;
106+
}
107+
onChange?.(items?.map((i) => i.value));
115108
},
116109
[onChange],
117110
);
118111

119112
const setValues = useCallback(
120-
(value: AttachmentOption[]) => {
121-
setSelected(value);
113+
(value?: AttachmentOption[]) => {
122114
fireChange(value);
123115
setInputValue('');
124116
setShowSuggestions(false);
@@ -155,9 +147,9 @@ const AttachmentInput: FC<Props> = ({
155147

156148
const handleRadioChange = useCallback(
157149
(option: AttachmentType) => {
158-
setSelectedRadio(option);
159-
160-
if (option === AttachmentType.NONE || option === AttachmentType.SPECIFIC) {
150+
if (option === AttachmentType.NONE) {
151+
setValues(void 0);
152+
} else if (option === AttachmentType.SPECIFIC) {
161153
setValues([]);
162154
} else if (option === AttachmentType.ALL) {
163155
setValues(ALL_ATTACHMENTS_VALUE);

apps/ai-dial-admin/src/components/EntityMainProperties/EntityAttachments/EntityAttachments.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,8 @@ const EntityAttachments: FC<Props> = ({ entity, onChangeEntity }) => {
3636
);
3737

3838
const onChangeAttachmentTypes = useCallback(
39-
(types: string[]) => {
40-
if (types.length) {
39+
(types?: string[]) => {
40+
if (types) {
4141
onChangeEntity({ ...entity, inputAttachmentTypes: types });
4242
} else {
4343
onChangeEntity({

0 commit comments

Comments
 (0)