From 5105abc730d67744edc4f00f7f57a2231c129f30 Mon Sep 17 00:00:00 2001 From: Lin Wang Date: Thu, 15 Jun 2023 16:06:06 +0800 Subject: [PATCH] feat: integrate with workspace create API (#13) * feat: integrate with workspace create API Signed-off-by: Lin Wang * feat: update to i18n text for toast Signed-off-by: Lin Wang --------- Signed-off-by: Lin Wang --- .../workspace_creator/workspace_creator.tsx | 37 +++++++++++++++++-- .../workspace_creator/workspace_form.tsx | 2 +- 2 files changed, 35 insertions(+), 4 deletions(-) diff --git a/src/plugins/workspace/public/components/workspace_creator/workspace_creator.tsx b/src/plugins/workspace/public/components/workspace_creator/workspace_creator.tsx index e7006464ba7..59c4ce0c444 100644 --- a/src/plugins/workspace/public/components/workspace_creator/workspace_creator.tsx +++ b/src/plugins/workspace/public/components/workspace_creator/workspace_creator.tsx @@ -5,17 +5,48 @@ import React, { useCallback } from 'react'; import { EuiPage, EuiPageBody, EuiPageHeader, EuiPageContent } from '@elastic/eui'; +import { i18n } from '@osd/i18n'; import { useOpenSearchDashboards } from '../../../../../plugins/opensearch_dashboards_react/public'; -import { WorkspaceForm } from './workspace_form'; +import { WorkspaceForm, WorkspaceFormData } from './workspace_form'; export const WorkspaceCreator = () => { const { - services: { application }, + services: { application, workspaces, notifications }, } = useOpenSearchDashboards(); - const handleWorkspaceFormSubmit = useCallback(() => {}, []); + const handleWorkspaceFormSubmit = useCallback( + async (data: WorkspaceFormData) => { + let result; + try { + result = await workspaces?.client.create(data); + } catch (error) { + notifications?.toasts.addDanger({ + title: i18n.translate('workspace.create.failed', { + defaultMessage: 'Failed to create workspace', + }), + text: error instanceof Error ? error.message : JSON.stringify(error), + }); + return; + } + if (result?.success) { + notifications?.toasts.addSuccess({ + title: i18n.translate('workspace.create.success', { + defaultMessage: 'Create workspace successfully', + }), + }); + return; + } + notifications?.toasts.addDanger({ + title: i18n.translate('workspace.create.failed', { + defaultMessage: 'Failed to create workspace', + }), + text: result?.error, + }); + }, + [notifications?.toasts, workspaces?.client] + ); return ( diff --git a/src/plugins/workspace/public/components/workspace_creator/workspace_form.tsx b/src/plugins/workspace/public/components/workspace_creator/workspace_form.tsx index 8cb3a1e3c39..41639701c43 100644 --- a/src/plugins/workspace/public/components/workspace_creator/workspace_form.tsx +++ b/src/plugins/workspace/public/components/workspace_creator/workspace_form.tsx @@ -44,7 +44,7 @@ interface WorkspaceFeatureGroup { features: WorkspaceFeature[]; } -interface WorkspaceFormData { +export interface WorkspaceFormData { name: string; description?: string; features: string[];