Skip to content

Commit

Permalink
extract actions to their own module
Browse files Browse the repository at this point in the history
  • Loading branch information
pgayvallet committed Nov 25, 2020
1 parent 60ed0b5 commit 8be87b9
Show file tree
Hide file tree
Showing 6 changed files with 320 additions and 176 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License;
* you may not use this file except in compliance with the Elastic License.
*/

import { Action as EuiTableAction } from '@elastic/eui/src/components/basic_table/action_types';
import { i18n } from '@kbn/i18n';
import { NotificationsStart, OverlayStart } from 'kibana/public';
import { TagWithRelations } from '../../../common';
import { ITagsCache } from '../../services/tags';
import { getAssignFlyoutOpener } from '../../components/assign_flyout';
import { ITagAssignmentService } from '../../services/assignments';

interface GetAssignActionOptions {
overlays: OverlayStart;
notifications: NotificationsStart;
tagCache: ITagsCache;
assignmentService: ITagAssignmentService;
assignableTypes: string[];
fetchTags: () => Promise<void>;
}

export const getAssignAction = ({
notifications,
overlays,
assignableTypes,
assignmentService,
tagCache,
fetchTags,
}: GetAssignActionOptions): EuiTableAction<TagWithRelations> => {
const openFlyout = getAssignFlyoutOpener({
overlays,
notifications,
tagCache,
assignmentService,
assignableTypes,
});

return {
name: ({ name }) =>
i18n.translate('xpack.savedObjectsTagging.management.table.actions.assign.title', {
defaultMessage: 'Manage {name} assignments',
values: { name },
}),
description: i18n.translate(
'xpack.savedObjectsTagging.management.table.actions.assign.description',
{
defaultMessage: 'Manage assignments',
}
),
type: 'icon',
icon: 'tag',
onClick: async (tag: TagWithRelations) => {
const flyout = await openFlyout({
tagIds: [tag.id],
});

// TODO
/*
canceled$.pipe(takeUntil(from(flyout.onClose))).subscribe(() => {
flyout.close();
});
*/

await flyout.onClose;
await fetchTags();
},
'data-test-subj': 'tagsTableAction-assign',
};
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License;
* you may not use this file except in compliance with the Elastic License.
*/

import { Action as EuiTableAction } from '@elastic/eui/src/components/basic_table/action_types';
import { i18n } from '@kbn/i18n';
import { NotificationsStart, OverlayStart } from 'kibana/public';
import { TagWithRelations } from '../../../common';
import { ITagInternalClient } from '../../services/tags';

interface GetDeleteActionOptions {
overlays: OverlayStart;
notifications: NotificationsStart;
tagClient: ITagInternalClient;
fetchTags: () => Promise<void>;
}

export const getDeleteAction = ({
notifications,
overlays,
tagClient,
fetchTags,
}: GetDeleteActionOptions): EuiTableAction<TagWithRelations> => {
return {
name: ({ name }) =>
i18n.translate('xpack.savedObjectsTagging.management.table.actions.delete.title', {
defaultMessage: 'Delete {name} tag',
values: { name },
}),
description: i18n.translate(
'xpack.savedObjectsTagging.management.table.actions.delete.description',
{
defaultMessage: 'Delete this tag',
}
),
type: 'icon',
icon: 'trash',
onClick: async (tag: TagWithRelations) => {
const confirmed = await overlays.openConfirm(
i18n.translate('xpack.savedObjectsTagging.modals.confirmDelete.text', {
defaultMessage:
'By deleting this tag, you will no longer be able to assign it to saved objects. ' +
'This tag will be removed from any saved objects that currently use it. ' +
'Are you sure you wish to proceed?',
}),
{
title: i18n.translate('xpack.savedObjectsTagging.modals.confirmDelete.title', {
defaultMessage: 'Delete "{name}" tag',
values: {
name: tag.name,
},
}),
confirmButtonText: i18n.translate(
'xpack.savedObjectsTagging.modals.confirmDelete.confirmButtonText',
{
defaultMessage: 'Delete tag',
}
),
buttonColor: 'danger',
maxWidth: 560,
}
);
if (confirmed) {
await tagClient.delete(tag.id);

notifications.toasts.addSuccess({
title: i18n.translate('xpack.savedObjectsTagging.notifications.deleteTagSuccessTitle', {
defaultMessage: 'Deleted "{name}" tag',
values: {
name: tag.name,
},
}),
});

await fetchTags();
}
},
'data-test-subj': 'tagsTableAction-delete',
};
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License;
* you may not use this file except in compliance with the Elastic License.
*/

import { Action as EuiTableAction } from '@elastic/eui/src/components/basic_table/action_types';
import { i18n } from '@kbn/i18n';
import { NotificationsStart, OverlayStart } from 'kibana/public';
import { TagWithRelations } from '../../../common';
import { ITagInternalClient } from '../../services/tags';
import { getEditModalOpener } from '../../components/edition_modal';

interface GetEditActionOptions {
overlays: OverlayStart;
notifications: NotificationsStart;
tagClient: ITagInternalClient;
fetchTags: () => Promise<void>;
}

export const getEditAction = ({
notifications,
overlays,
tagClient,
fetchTags,
}: GetEditActionOptions): EuiTableAction<TagWithRelations> => {
const editModalOpener = getEditModalOpener({ overlays, tagClient });
return {
name: ({ name }) =>
i18n.translate('xpack.savedObjectsTagging.management.table.actions.edit.title', {
defaultMessage: 'Edit {name} tag',
values: { name },
}),
isPrimary: true,
description: i18n.translate(
'xpack.savedObjectsTagging.management.table.actions.edit.description',
{
defaultMessage: 'Edit this tag',
}
),
type: 'icon',
icon: 'pencil',
onClick: (tag: TagWithRelations) => {
editModalOpener({
tagId: tag.id,
onUpdate: (updatedTag) => {
fetchTags();
notifications.toasts.addSuccess({
title: i18n.translate('xpack.savedObjectsTagging.notifications.editTagSuccessTitle', {
defaultMessage: 'Saved changes to "{name}" tag',
values: {
name: updatedTag.name,
},
}),
});
},
});
},
'data-test-subj': 'tagsTableAction-edit',
};
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License;
* you may not use this file except in compliance with the Elastic License.
*/

import { Action as EuiTableAction } from '@elastic/eui/src/components/basic_table/action_types';
import { CoreStart } from 'kibana/public';
import { TagsCapabilities, TagWithRelations } from '../../../common';
import { ITagInternalClient, ITagAssignmentService, ITagsCache } from '../../services';
import { getDeleteAction } from './delete';
import { getEditAction } from './edit';
import { getAssignAction } from './assign';

interface GetActionsOptions {
core: CoreStart;
capabilities: TagsCapabilities;
tagClient: ITagInternalClient;
tagCache: ITagsCache;
assignmentService: ITagAssignmentService;
setLoading: (loading: boolean) => void;
assignableTypes: string[];
fetchTags: () => Promise<void>;
}

export const getTableActions = ({
core: { notifications, overlays },
capabilities,
tagClient,
tagCache,
assignmentService,
setLoading,
assignableTypes,
fetchTags,
}: GetActionsOptions): Array<EuiTableAction<TagWithRelations>> => {
const actions: Array<EuiTableAction<TagWithRelations>> = [];

if (capabilities.edit) {
actions.push(getEditAction({ notifications, overlays, tagClient, fetchTags }));
}

if (capabilities.assign && assignableTypes.length > 0) {
actions.push(
getAssignAction({
tagCache,
assignmentService,
assignableTypes,
fetchTags,
notifications,
overlays,
})
);
}

if (capabilities.delete) {
actions.push(getDeleteAction({ overlays, notifications, tagClient, fetchTags }));
}

return actions;
};
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,9 @@ interface TagTableProps {
onQueryChange: (query?: Query) => void;
selectedTags: TagWithRelations[];
onSelectionChange: (selection: TagWithRelations[]) => void;
onEdit: (tag: TagWithRelations) => void;
onDelete: (tag: TagWithRelations) => void;
onAssign: (tag: TagWithRelations) => void;
getTagRelationUrl: (tag: TagWithRelations) => string;
onShowRelations: (tag: TagWithRelations) => void;
actions: Array<EuiTableAction<TagWithRelations>>;
actionBar: ReactNode;
}

Expand Down Expand Up @@ -53,12 +51,10 @@ export const TagTable: FC<TagTableProps> = ({
onQueryChange,
selectedTags,
onSelectionChange,
onEdit,
onDelete,
onAssign,
onShowRelations,
getTagRelationUrl,
actionBar,
actions,
}) => {
const tableRef = useRef<EuiInMemoryTable<TagWithRelations>>(null);

Expand All @@ -68,66 +64,6 @@ export const TagTable: FC<TagTableProps> = ({
}
}, [selectedTags]);

const actions: Array<EuiTableAction<TagWithRelations>> = [];
if (capabilities.edit) {
actions.push({
name: ({ name }) =>
i18n.translate('xpack.savedObjectsTagging.management.table.actions.edit.title', {
defaultMessage: 'Edit {name} tag',
values: { name },
}),
isPrimary: true,
description: i18n.translate(
'xpack.savedObjectsTagging.management.table.actions.edit.description',
{
defaultMessage: 'Edit this tag',
}
),
type: 'icon',
icon: 'pencil',
onClick: (object: TagWithRelations) => onEdit(object),
'data-test-subj': 'tagsTableAction-edit',
});
}
if (capabilities.assign) {
actions.push({
name: ({ name }) =>
i18n.translate('xpack.savedObjectsTagging.management.table.actions.assign.title', {
defaultMessage: 'Manage {name} assignments',
values: { name },
}),
description: i18n.translate(
'xpack.savedObjectsTagging.management.table.actions.assign.description',
{
defaultMessage: 'Manage assignments',
}
),
type: 'icon',
icon: 'tag',
onClick: (object: TagWithRelations) => onAssign(object),
'data-test-subj': 'tagsTableAction-assign',
});
}
if (capabilities.delete) {
actions.push({
name: ({ name }) =>
i18n.translate('xpack.savedObjectsTagging.management.table.actions.delete.title', {
defaultMessage: 'Delete {name} tag',
values: { name },
}),
description: i18n.translate(
'xpack.savedObjectsTagging.management.table.actions.delete.description',
{
defaultMessage: 'Delete this tag',
}
),
type: 'icon',
icon: 'trash',
onClick: (object: TagWithRelations) => onDelete(object),
'data-test-subj': 'tagsTableAction-delete',
});
}

const columns: Array<EuiBasicTableColumn<TagWithRelations>> = [
{
field: 'name',
Expand Down
Loading

0 comments on commit 8be87b9

Please sign in to comment.