Skip to content

Commit

Permalink
PR feedback 2
Browse files Browse the repository at this point in the history
  • Loading branch information
js-jankisalvi committed Jul 11, 2024
1 parent 5059e9c commit 77f756a
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 35 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -752,7 +752,7 @@ describe('ConfigureCases', () => {

appMockRender.render(<ConfigureCases />);

const list = screen.getByTestId('custom-fields-list');
const list = await screen.findByTestId('custom-fields-list');

userEvent.click(
within(list).getByTestId(`${customFieldsConfigurationMock[0].key}-custom-field-delete`)
Expand Down
67 changes: 36 additions & 31 deletions x-pack/plugins/cases/public/components/configure_cases/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ import { Templates } from '../templates';
import type { TemplateFormProps } from '../templates/types';
import { CustomFieldsForm } from '../custom_fields/form';
import { TemplateForm } from '../templates/form';
import type { CaseUI } from '../../containers/types';
import type { CasesConfigurationUI, CaseUI } from '../../containers/types';
import { builderMap as customFieldsBuilderMap } from '../custom_fields/builder';

const sectionWrapperCss = css`
Expand All @@ -74,6 +74,40 @@ interface Flyout {
visible: boolean;
}

const addNewCustomFieldToTemplates = ({templates, customFields}: Pick<CasesConfigurationUI, 'templates' | 'customFields'>) => {
return templates.map((template) => {
const templateCustomFields = template.caseFields?.customFields ?? [];

customFields.forEach((field) => {
if (
!templateCustomFields.length ||
!templateCustomFields.find(
(templateCustomField) => templateCustomField.key === field.key
)
) {
const customFieldFactory = customFieldsBuilderMap[field.type];
const { getDefaultValue } = customFieldFactory();
const value = getDefaultValue?.() ?? null;

templateCustomFields.push({
key: field.key,
type: field.type as CustomFieldTypes,
value: field.defaultValue ?? value,
} as CaseUI['customFields'][number]);
}
});

return {
...template,
caseFields: {
...template.caseFields,
customFields: [...templateCustomFields],
},
};
});
}


export const ConfigureCases: React.FC = React.memo(() => {
const { permissions } = useCasesContext();
const { triggersActionsUi } = useKibana().services;
Expand Down Expand Up @@ -341,36 +375,7 @@ export const ConfigureCases: React.FC = React.memo(() => {
const updatedCustomFields = addOrReplaceField(customFields, data);

// add the new custom field to each template as well
const updatedTemplates = templates.map((template) => {
const templateCustomFields = template.caseFields?.customFields ?? [];

updatedCustomFields.forEach((field) => {
if (
!templateCustomFields.length ||
!templateCustomFields.find(
(templateCustomField) => templateCustomField.key === field.key
)
) {
const customFieldFactory = customFieldsBuilderMap[field.type];
const { getDefaultValue } = customFieldFactory();
const value = getDefaultValue ? getDefaultValue() : null;

templateCustomFields.push({
key: field.key,
type: field.type as CustomFieldTypes,
value: field.defaultValue ?? value,
} as CaseUI['customFields'][number]);
}
});

return {
...template,
caseFields: {
...template.caseFields,
customFields: [...templateCustomFields],
},
};
});
const updatedTemplates = addNewCustomFieldToTemplates({templates, customFields: updatedCustomFields});

persistCaseConfigure({
connector,
Expand Down
5 changes: 2 additions & 3 deletions x-pack/plugins/cases/server/client/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -635,16 +635,15 @@ export const transformTemplateCustomFields = ({
);

// add new custom fields to template
if (customFields.length > transformedTemplateCustomFields.length) {
if (customFields.length >= transformedTemplateCustomFields.length) {
customFields.forEach((field) => {
if (
!transformedTemplateCustomFields.length ||
!transformedTemplateCustomFields.find(
(templateCustomField) => templateCustomField.key === field.key
)
) {
const { getDefaultValue } = casesCustomFields.get(field.type) ?? {};
const value = getDefaultValue ? getDefaultValue() : null;
const value = getDefaultValue?.() ?? null;

transformedTemplateCustomFields.push({
key: field.key,
Expand Down

0 comments on commit 77f756a

Please sign in to comment.