Skip to content

Commit

Permalink
Clarify workspace form types
Browse files Browse the repository at this point in the history
Signed-off-by: Lin Wang <[email protected]>
  • Loading branch information
wanglam committed Aug 28, 2024
1 parent 3f65920 commit 9e8a10f
Show file tree
Hide file tree
Showing 7 changed files with 35 additions and 35 deletions.
2 changes: 1 addition & 1 deletion src/plugins/workspace/public/components/forms/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
27 changes: 13 additions & 14 deletions src/plugins/workspace/public/components/forms/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -65,7 +60,7 @@ export interface WorkspaceFormError {

export type WorkspaceFormErrors = {
[key in keyof Omit<
WorkspaceFormData,
WorkspaceFormSubmitData,
'permissionSettings' | 'description' | 'selectedDataSources'
>]?: WorkspaceFormError;
} & {
Expand All @@ -80,20 +75,24 @@ export interface WorkspaceFormProps {
application: ApplicationStart;
savedObjects: SavedObjectsStart;
onSubmit?: (formData: WorkspaceFormSubmitData) => void;
defaultValues?: Partial<WorkspaceFormData>;
defaultValues?: Partial<WorkspaceFormSubmitData>;
operationType: WorkspaceOperationType;
permissionEnabled?: boolean;
detailTab?: DetailTab;
dataSourceManagement?: DataSourceManagementPluginSetup;
availableUseCases: WorkspaceUseCase[];
detailTitle?: string;
}

export interface WorkspaceDetailedFormProps extends WorkspaceFormProps {
defaultValues?: WorkspaceFormData;
}

export interface AvailableUseCaseItem
extends Pick<WorkspaceUseCase, 'id' | 'title' | 'features' | 'description' | 'systematic'> {
disabled?: boolean;
}

export interface WorkspaceFormDataState
extends Omit<WorkspaceFormSubmitData, 'name' | 'permissionSettings'> {
name: string;
useCase: string | undefined;
selectedDataSources: DataSource[];
permissionSettings: Array<
Pick<WorkspacePermissionSetting, 'id'> & Partial<WorkspacePermissionSetting>
>;
}
Original file line number Diff line number Diff line change
Expand Up @@ -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: {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -46,7 +51,7 @@ export const useWorkspaceForm = ({
featureConfigs,
]);
const [permissionSettings, setPermissionSettings] = useState<
Array<Pick<WorkspacePermissionSetting, 'id'> & Partial<WorkspacePermissionSetting>>
WorkspaceFormDataState['permissionSettings']
>(initialPermissionSettingsRef.current);

const [selectedDataSources, setSelectedDataSources] = useState<DataSource[]>(
Expand All @@ -58,7 +63,7 @@ export const useWorkspaceForm = ({
const [formErrors, setFormErrors] = useState<WorkspaceFormErrors>({});
const numberOfErrors = useMemo(() => getNumberOfErrors(formErrors), [formErrors]);
const formIdRef = useRef<string>();
const getFormData = () => ({
const getFormData = (): WorkspaceFormDataState => ({
name,
description,
features: featureConfigs,
Expand Down
16 changes: 4 additions & 12 deletions src/plugins/workspace/public/components/forms/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import {
} from './constants';

import {
WorkspaceFormData,
WorkspaceFormDataState,
WorkspaceFormError,
WorkspaceFormErrorCode,
WorkspaceFormErrors,
Expand Down Expand Up @@ -295,11 +295,7 @@ export const isSelectedDataSourcesDuplicated = (
) => selectedDataSources.some((ds) => ds.id === row.id);

export const validateWorkspaceForm = (
formData: Omit<Partial<WorkspaceFormSubmitData>, 'permissionSettings'> & {
permissionSettings?: Array<
Pick<WorkspacePermissionSetting, 'id'> & Partial<WorkspacePermissionSetting>
>;
},
formData: Partial<WorkspaceFormDataState>,
isPermissionEnabled: boolean
) => {
const formErrors: WorkspaceFormErrors = {};
Expand Down Expand Up @@ -452,12 +448,8 @@ const isSamePermissionSetting = (a: PermissionSettingLike, b: PermissionSettingL
};

export const getNumberOfChanges = (
newFormData: Partial<Omit<WorkspaceFormSubmitData, 'permissionSettings'>> & {
permissionSettings?: Array<
Pick<WorkspacePermissionSetting, 'id'> & Partial<WorkspacePermissionSetting>
>;
},
initialFormData: Partial<Omit<WorkspaceFormData, 'id'>>
newFormData: Partial<WorkspaceFormDataState>,
initialFormData: Partial<WorkspaceFormSubmitData>
) => {
let count = 0;
if (newFormData.name !== initialFormData.name) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -53,7 +52,7 @@ export const WorkspaceCreator = (props: WorkspaceCreatorProps) => {
});

const defaultSelectedUseCase = availableUseCases?.[0];
const defaultWorkspaceFormValues: Partial<WorkspaceFormData> = {
const defaultWorkspaceFormValues: Partial<WorkspaceFormSubmitData> = {
color: euiPaletteColorBlind()[0],
...(defaultSelectedUseCase
? {
Expand Down

0 comments on commit 9e8a10f

Please sign in to comment.