From 9e8a10f2fcdb97a442b58d1407c52236fbe971c0 Mon Sep 17 00:00:00 2001 From: Lin Wang Date: Wed, 28 Aug 2024 14:54:02 +0800 Subject: [PATCH] Clarify workspace form types Signed-off-by: Lin Wang --- .../public/components/forms/index.ts | 2 +- .../public/components/forms/types.ts | 27 +++++++++---------- .../forms/use_workspace_form.test.ts | 4 +-- .../components/forms/use_workspace_form.ts | 11 +++++--- .../public/components/forms/utils.ts | 16 +++-------- .../forms/workspace_detail_form.tsx | 7 ++++- .../workspace_creator/workspace_creator.tsx | 3 +-- 7 files changed, 35 insertions(+), 35 deletions(-) diff --git a/src/plugins/workspace/public/components/forms/index.ts b/src/plugins/workspace/public/components/forms/index.ts index e77561789759..3c493bf4aa3c 100644 --- a/src/plugins/workspace/public/components/forms/index.ts +++ b/src/plugins/workspace/public/components/forms/index.ts @@ -11,7 +11,7 @@ export { WorkspacePermissionSettingPanel } from './workspace_permission_setting_ export { WorkspaceCancelModal } from './workspace_cancel_modal'; export { WorkspaceNameField, WorkspaceDescriptionField } from './fields'; -export { WorkspaceFormSubmitData, WorkspaceFormData, WorkspaceFormProps } from './types'; +export { WorkspaceFormSubmitData, WorkspaceFormProps, WorkspaceFormDataState } from './types'; export { WorkspaceOperationType, DetailTab, diff --git a/src/plugins/workspace/public/components/forms/types.ts b/src/plugins/workspace/public/components/forms/types.ts index 21fac88ff488..9e6fbd9fe48c 100644 --- a/src/plugins/workspace/public/components/forms/types.ts +++ b/src/plugins/workspace/public/components/forms/types.ts @@ -31,17 +31,12 @@ export type WorkspacePermissionSetting = export interface WorkspaceFormSubmitData { name: string; description?: string; - features?: string[]; + features: string[]; color?: string; permissionSettings?: WorkspacePermissionSetting[]; selectedDataSources?: DataSource[]; } -export interface WorkspaceFormData extends WorkspaceFormSubmitData { - id: string; - reserved?: boolean; -} - export enum WorkspaceFormErrorCode { InvalidWorkspaceName, WorkspaceNameMissing, @@ -65,7 +60,7 @@ export interface WorkspaceFormError { export type WorkspaceFormErrors = { [key in keyof Omit< - WorkspaceFormData, + WorkspaceFormSubmitData, 'permissionSettings' | 'description' | 'selectedDataSources' >]?: WorkspaceFormError; } & { @@ -80,20 +75,24 @@ export interface WorkspaceFormProps { application: ApplicationStart; savedObjects: SavedObjectsStart; onSubmit?: (formData: WorkspaceFormSubmitData) => void; - defaultValues?: Partial; + defaultValues?: Partial; operationType: WorkspaceOperationType; permissionEnabled?: boolean; - detailTab?: DetailTab; dataSourceManagement?: DataSourceManagementPluginSetup; availableUseCases: WorkspaceUseCase[]; - detailTitle?: string; -} - -export interface WorkspaceDetailedFormProps extends WorkspaceFormProps { - defaultValues?: WorkspaceFormData; } export interface AvailableUseCaseItem extends Pick { disabled?: boolean; } + +export interface WorkspaceFormDataState + extends Omit { + name: string; + useCase: string | undefined; + selectedDataSources: DataSource[]; + permissionSettings: Array< + Pick & Partial + >; +} diff --git a/src/plugins/workspace/public/components/forms/use_workspace_form.test.ts b/src/plugins/workspace/public/components/forms/use_workspace_form.test.ts index 6e25c81d4440..078ce9af3e45 100644 --- a/src/plugins/workspace/public/components/forms/use_workspace_form.test.ts +++ b/src/plugins/workspace/public/components/forms/use_workspace_form.test.ts @@ -8,10 +8,10 @@ import { renderHook, act } from '@testing-library/react-hooks'; import { applicationServiceMock } from '../../../../../core/public/mocks'; import { WorkspacePermissionMode } from '../../../common/constants'; import { WorkspaceOperationType, WorkspacePermissionItemType } from './constants'; -import { WorkspaceFormData, WorkspaceFormErrorCode } from './types'; +import { WorkspaceFormSubmitData, WorkspaceFormErrorCode } from './types'; import { useWorkspaceForm } from './use_workspace_form'; -const setup = (defaultValues?: WorkspaceFormData, permissionEnabled = false) => { +const setup = (defaultValues?: WorkspaceFormSubmitData, permissionEnabled = false) => { const onSubmitMock = jest.fn(); const renderResult = renderHook(useWorkspaceForm, { initialProps: { diff --git a/src/plugins/workspace/public/components/forms/use_workspace_form.ts b/src/plugins/workspace/public/components/forms/use_workspace_form.ts index f627df1f3aad..55f5c5af1607 100644 --- a/src/plugins/workspace/public/components/forms/use_workspace_form.ts +++ b/src/plugins/workspace/public/components/forms/use_workspace_form.ts @@ -13,7 +13,12 @@ import { isUseCaseFeatureConfig, } from '../../utils'; import { DataSource } from '../../../common/types'; -import { WorkspaceFormProps, WorkspaceFormErrors, WorkspacePermissionSetting } from './types'; +import { + WorkspaceFormProps, + WorkspaceFormErrors, + WorkspacePermissionSetting, + WorkspaceFormDataState, +} from './types'; import { generatePermissionSettingsState, getNumberOfChanges, @@ -46,7 +51,7 @@ export const useWorkspaceForm = ({ featureConfigs, ]); const [permissionSettings, setPermissionSettings] = useState< - Array & Partial> + WorkspaceFormDataState['permissionSettings'] >(initialPermissionSettingsRef.current); const [selectedDataSources, setSelectedDataSources] = useState( @@ -58,7 +63,7 @@ export const useWorkspaceForm = ({ const [formErrors, setFormErrors] = useState({}); const numberOfErrors = useMemo(() => getNumberOfErrors(formErrors), [formErrors]); const formIdRef = useRef(); - const getFormData = () => ({ + const getFormData = (): WorkspaceFormDataState => ({ name, description, features: featureConfigs, diff --git a/src/plugins/workspace/public/components/forms/utils.ts b/src/plugins/workspace/public/components/forms/utils.ts index 04c1f3772600..bd45cf6a901f 100644 --- a/src/plugins/workspace/public/components/forms/utils.ts +++ b/src/plugins/workspace/public/components/forms/utils.ts @@ -16,7 +16,7 @@ import { } from './constants'; import { - WorkspaceFormData, + WorkspaceFormDataState, WorkspaceFormError, WorkspaceFormErrorCode, WorkspaceFormErrors, @@ -295,11 +295,7 @@ export const isSelectedDataSourcesDuplicated = ( ) => selectedDataSources.some((ds) => ds.id === row.id); export const validateWorkspaceForm = ( - formData: Omit, 'permissionSettings'> & { - permissionSettings?: Array< - Pick & Partial - >; - }, + formData: Partial, isPermissionEnabled: boolean ) => { const formErrors: WorkspaceFormErrors = {}; @@ -452,12 +448,8 @@ const isSamePermissionSetting = (a: PermissionSettingLike, b: PermissionSettingL }; export const getNumberOfChanges = ( - newFormData: Partial> & { - permissionSettings?: Array< - Pick & Partial - >; - }, - initialFormData: Partial> + newFormData: Partial, + initialFormData: Partial ) => { let count = 0; if (newFormData.name !== initialFormData.name) { diff --git a/src/plugins/workspace/public/components/forms/workspace_detail_form.tsx b/src/plugins/workspace/public/components/forms/workspace_detail_form.tsx index bc3ce92067ff..8b037a99ea63 100644 --- a/src/plugins/workspace/public/components/forms/workspace_detail_form.tsx +++ b/src/plugins/workspace/public/components/forms/workspace_detail_form.tsx @@ -50,7 +50,12 @@ const FormGroup = ({ title, children, describe }: FormGroupProps) => ( ); -export const WorkspaceDetailForm = (props: WorkspaceFormProps) => { +interface WorkspaceDetailedFormProps extends WorkspaceFormProps { + detailTab?: DetailTab; + detailTitle?: string; +} + +export const WorkspaceDetailForm = (props: WorkspaceDetailedFormProps) => { const { detailTab, detailTitle, defaultValues, availableUseCases } = props; const { formId, 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 fd98e5f9d9f8..8f99ba4731ee 100644 --- a/src/plugins/workspace/public/components/workspace_creator/workspace_creator.tsx +++ b/src/plugins/workspace/public/components/workspace_creator/workspace_creator.tsx @@ -17,7 +17,6 @@ import { convertPermissionSettingsToPermissions } from '../forms'; import { DataSource } from '../../../common/types'; import { DataSourceManagementPluginSetup } from '../../../../../plugins/data_source_management/public'; import { WorkspaceUseCase } from '../../types'; -import { WorkspaceFormData } from '../forms/types'; import { getUseCaseFeatureConfig } from '../../utils'; import { useFormAvailableUseCases } from '../forms/use_form_available_use_cases'; import { NavigationPublicPluginStart } from '../../../../../plugins/navigation/public'; @@ -53,7 +52,7 @@ export const WorkspaceCreator = (props: WorkspaceCreatorProps) => { }); const defaultSelectedUseCase = availableUseCases?.[0]; - const defaultWorkspaceFormValues: Partial = { + const defaultWorkspaceFormValues: Partial = { color: euiPaletteColorBlind()[0], ...(defaultSelectedUseCase ? {