Skip to content

Commit

Permalink
optimize code
Browse files Browse the repository at this point in the history
Signed-off-by: yubonluo <[email protected]>
  • Loading branch information
yubonluo committed Apr 7, 2024
1 parent 1cd26a3 commit 334129a
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,20 @@ export class SavedObjectsDuplicateModal extends React.Component<Props, State> {
return savedObjectTypeInfo && savedObjectTypeInfo[1];
};

getIncludeAndNotDuplicateObjects = (targetWorkspaceId: string | undefined) => {
let selectedObjects = this.state.allSelectedObjects;
if (this.props.duplicateMode === DuplicateMode.All) {
selectedObjects = selectedObjects.filter((item) => this.isSavedObjectTypeIncluded(item.type));
}
// If the target workspace is not selected, all saved objects will be retained.
// If the target workspace has been selected, filter out the saved objects that belongs to the workspace.
const includedSelectedObjects = selectedObjects.filter((item) =>
!!targetWorkspaceId && !!item.workspaces ? !item.workspaces.includes(targetWorkspaceId) : true
);
const ignoredSelectedObjectsLength = selectedObjects.length - includedSelectedObjects.length;
return { includedSelectedObjects, ignoredSelectedObjectsLength };
};

render() {
const {
workspaceOptions,
Expand All @@ -156,17 +170,11 @@ export class SavedObjectsDuplicateModal extends React.Component<Props, State> {
} = this.state;
const { duplicateMode, onClose } = this.props;
const targetWorkspaceId = targetWorkspaceOption?.at(0)?.key;
let selectedObjects = allSelectedObjects;
if (duplicateMode === DuplicateMode.All) {
selectedObjects = selectedObjects.filter((item) => this.isSavedObjectTypeIncluded(item.type));
}
// If the target workspace is not selected, all saved objects will be retained.
// If the target workspace has been selected, filter out the saved objects that belongs to the workspace.
const includedSelectedObjects = selectedObjects.filter((item) =>
!!targetWorkspaceId && !!item.workspaces ? !item.workspaces.includes(targetWorkspaceId) : true
);

const ignoredSelectedObjectsLength = selectedObjects.length - includedSelectedObjects.length;
const {
includedSelectedObjects,
ignoredSelectedObjectsLength,
} = this.getIncludeAndNotDuplicateObjects(targetWorkspaceId);

let confirmDuplicateButtonEnabled = false;
if (!!targetWorkspaceId && includedSelectedObjects.length > 0) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@ describe('Header', () => {
onImport: () => {},
onRefresh: () => {},
onDuplicate: () => {},
title: 'Saved Objects',
objectCount: 4,
filteredCount: 2,
showDuplicateAll: false,
Expand All @@ -56,7 +55,6 @@ describe('Header', () => {
onImport: () => {},
onRefresh: () => {},
onDuplicate: () => {},
title: 'Saved Objects',
objectCount: 4,
filteredCount: 2,
showDuplicateAll: undefined,
Expand All @@ -75,7 +73,6 @@ describe('Header - workspace enabled', () => {
onImport: () => {},
onRefresh: () => {},
onDuplicate: () => {},
title: 'Saved Objects',
objectCount: 4,
filteredCount: 2,
showDuplicateAll: true,
Expand Down

0 comments on commit 334129a

Please sign in to comment.