|
| 1 | +/* |
| 2 | + * Copyright OpenSearch Contributors |
| 3 | + * SPDX-License-Identifier: Apache-2.0 |
| 4 | + */ |
| 5 | + |
| 6 | +import React, { useCallback, useEffect, useState } from 'react'; |
| 7 | +import { EuiPage, EuiPageBody, EuiPageHeader, EuiPageContent } from '@elastic/eui'; |
| 8 | +import { useObservable } from 'react-use'; |
| 9 | +import { i18n } from '@osd/i18n'; |
| 10 | +import { of } from 'rxjs'; |
| 11 | + |
| 12 | +import { WorkspaceAttribute } from 'opensearch-dashboards/public'; |
| 13 | +import { useOpenSearchDashboards } from '../../../../../../src/plugins/opensearch_dashboards_react/public'; |
| 14 | + |
| 15 | +import { PATHS } from '../../../common/constants'; |
| 16 | +import { WorkspaceForm, WorkspaceFormData } from '../workspace_creator/workspace_form'; |
| 17 | +import { |
| 18 | + WORKSPACE_APP_ID, |
| 19 | + WORKSPACE_ID_IN_SESSION_STORAGE, |
| 20 | + WORKSPACE_OP_TYPE_UPDATE, |
| 21 | +} from '../../../common/constants'; |
| 22 | +import { ApplicationStart } from '../../../../../core/public'; |
| 23 | + |
| 24 | +export const WorkspaceUpdater = () => { |
| 25 | + const { |
| 26 | + services: { application, workspaces, notifications }, |
| 27 | + } = useOpenSearchDashboards<{ application: ApplicationStart }>(); |
| 28 | + |
| 29 | + const currentWorkspace = useObservable( |
| 30 | + workspaces ? workspaces.client.currentWorkspace$ : of(null) |
| 31 | + ); |
| 32 | + |
| 33 | + const excludedAttribute = 'id'; |
| 34 | + const { [excludedAttribute]: removedProperty, ...otherAttributes } = |
| 35 | + currentWorkspace || ({} as WorkspaceAttribute); |
| 36 | + |
| 37 | + const [currentWorkspaceFormData, setCurrentWorkspaceFormData] = useState< |
| 38 | + Omit<WorkspaceAttribute, 'id'> |
| 39 | + >(otherAttributes); |
| 40 | + |
| 41 | + useEffect(() => { |
| 42 | + const { id, ...others } = currentWorkspace || ({} as WorkspaceAttribute); |
| 43 | + setCurrentWorkspaceFormData(others); |
| 44 | + }, [workspaces, currentWorkspace, excludedAttribute]); |
| 45 | + |
| 46 | + const handleWorkspaceFormSubmit = useCallback( |
| 47 | + async (data: WorkspaceFormData) => { |
| 48 | + let result; |
| 49 | + if (!currentWorkspace) { |
| 50 | + notifications?.toasts.addDanger({ |
| 51 | + title: i18n.translate('Cannot find current workspace', { |
| 52 | + defaultMessage: 'Cannot update workspace', |
| 53 | + }), |
| 54 | + }); |
| 55 | + return; |
| 56 | + } |
| 57 | + try { |
| 58 | + result = await workspaces?.client.update(currentWorkspace?.id, data); |
| 59 | + } catch (error) { |
| 60 | + notifications?.toasts.addDanger({ |
| 61 | + title: i18n.translate('workspace.update.failed', { |
| 62 | + defaultMessage: 'Failed to update workspace', |
| 63 | + }), |
| 64 | + text: error instanceof Error ? error.message : JSON.stringify(error), |
| 65 | + }); |
| 66 | + return; |
| 67 | + } |
| 68 | + if (result?.success) { |
| 69 | + notifications?.toasts.addSuccess({ |
| 70 | + title: i18n.translate('workspace.update.success', { |
| 71 | + defaultMessage: 'Update workspace successfully', |
| 72 | + }), |
| 73 | + }); |
| 74 | + application.navigateToApp(WORKSPACE_APP_ID, { |
| 75 | + path: PATHS.overview + '?' + WORKSPACE_ID_IN_SESSION_STORAGE + '=' + currentWorkspace.id, |
| 76 | + }); |
| 77 | + return; |
| 78 | + } |
| 79 | + notifications?.toasts.addDanger({ |
| 80 | + title: i18n.translate('workspace.update.failed', { |
| 81 | + defaultMessage: 'Failed to update workspace', |
| 82 | + }), |
| 83 | + text: result?.error, |
| 84 | + }); |
| 85 | + }, |
| 86 | + [notifications?.toasts, workspaces?.client, currentWorkspace, application] |
| 87 | + ); |
| 88 | + |
| 89 | + if (!currentWorkspaceFormData.name) { |
| 90 | + return null; |
| 91 | + } |
| 92 | + |
| 93 | + return ( |
| 94 | + <EuiPage paddingSize="none"> |
| 95 | + <EuiPageBody panelled> |
| 96 | + <EuiPageHeader restrictWidth pageTitle="Update Workspace" /> |
| 97 | + <EuiPageContent |
| 98 | + verticalPosition="center" |
| 99 | + horizontalPosition="center" |
| 100 | + paddingSize="none" |
| 101 | + color="subdued" |
| 102 | + hasShadow={false} |
| 103 | + style={{ width: '100%', maxWidth: 1000 }} |
| 104 | + > |
| 105 | + {application && ( |
| 106 | + <WorkspaceForm |
| 107 | + application={application} |
| 108 | + onSubmit={handleWorkspaceFormSubmit} |
| 109 | + defaultValues={currentWorkspaceFormData} |
| 110 | + opType={WORKSPACE_OP_TYPE_UPDATE} |
| 111 | + /> |
| 112 | + )} |
| 113 | + </EuiPageContent> |
| 114 | + </EuiPageBody> |
| 115 | + </EuiPage> |
| 116 | + ); |
| 117 | +}; |
0 commit comments