Skip to content

Commit

Permalink
fix: Fix an issue where add/edit workspace is not working properly (#99)
Browse files Browse the repository at this point in the history
  • Loading branch information
jessieweiyi authored Mar 13, 2024
1 parent 168e32e commit 42e35b1
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import { InputProps } from '@cloudscape-design/components/input';
import Modal from '@cloudscape-design/components/modal';
import SpaceBetween from '@cloudscape-design/components/space-between';
import React, { FC, RefObject, useCallback, useEffect, useMemo, useRef, useState } from 'react';
import { WorkspaceSchema } from '../../../customTypes';
import { WorkspaceSchema, Workspace } from '../../../customTypes';
import Input from '../../generic/Input';

export interface EditWorkspaceProps {
Expand All @@ -30,22 +30,36 @@ export interface EditWorkspaceProps {
onConfirm: (workspace: string) => Promise<void>;
value?: string;
editMode?: boolean;
currentWorkspace?: Workspace;
workspaceList: Workspace[];
}

const EditWorkspace: FC<EditWorkspaceProps> = ({
visible,
setVisible,
onConfirm,
editMode = false,
workspaceList,
currentWorkspace,
...props
}) => {
const inputRef= useRef<InputProps.Ref>();
const inputRef = useRef<InputProps.Ref>();
const [value, setValue] = useState(props.value || '');
const [errorText, setErrorText] = useState('');

useEffect(() => {
setErrorText('');
}, [value]);

const handleConfirm = useCallback(async () => {
await onConfirm(value);
setVisible(false);
}, [onConfirm, value]);
setErrorText('');
if (workspaceList.find(x => x.name === value && (!currentWorkspace || currentWorkspace.id !== x.id))) {
setErrorText('A workspace already exists with that name');
} else {
await onConfirm(value);
setVisible(false);
}
}, [onConfirm, value, workspaceList, currentWorkspace]);

useEffect(() => {
inputRef.current?.focus();
Expand All @@ -71,6 +85,7 @@ const EditWorkspace: FC<EditWorkspaceProps> = ({
<SpaceBetween direction="vertical" size="m">
<FormField
label="Workspace name"
errorText={errorText}
>
<Input ref={inputRef as RefObject<InputProps.Ref>}
value={value}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ import {
import useImportExport from '../../../hooks/useExportImport';
import useRemoveData from '../../../hooks/useRemoveData';
import isWorkspaceExample from '../../../utils/isWorkspaceExample';
import AddWorkspace from '../../workspaces/EditWorkspace';
import EditWorkspace from '../../workspaces/EditWorkspace';
import FileImport from '../../workspaces/FileImport';

export interface WorkspaceSelectorProps {
Expand Down Expand Up @@ -367,23 +367,26 @@ const WorkspaceSelector: FC<PropsWithChildren<WorkspaceSelectorProps>> = ({
/>
)}
{addWorkspaceModalVisible && (
<AddWorkspace
<EditWorkspace
visible={addWorkspaceModalVisible}
setVisible={setAddWorkspaceModalVisible}
onConfirm={async (workspaceName: string) => {
await addWorkspace(workspaceName);
}}
workspaceList={workspaceList}
/>
)}
{editWorkspaceModalVisible && currentWorkspace && (
<AddWorkspace
<EditWorkspace
visible={editWorkspaceModalVisible}
setVisible={setEditWorkspaceModalVisible}
editMode
value={currentWorkspace.name}
onConfirm={(newWorkspaceName) =>
renameWorkspace(currentWorkspace.id, newWorkspaceName)
}
currentWorkspace={currentWorkspace}
workspaceList={workspaceList}
/>
)}
{removeDataModalVisible && (
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ const WorkspacesContextProvider: FC<WorkspacesContextProviderProps> = ({
storageType,
metadata,
};
setWorkspaceList(prev => prev.find(p => p.name === workspaceName) ? [...prev] : [...prev, newWorkspace]);
setWorkspaceList(prev => [...prev, newWorkspace]);
setCurrentWorkspace(newWorkspace);
onWorkspaceChanged?.(newWorkspace.id);
return newWorkspace;
Expand All @@ -105,7 +105,7 @@ const WorkspacesContextProvider: FC<WorkspacesContextProviderProps> = ({
const handleRenameWorkspace = useCallback(async (id: string, newWorkspaceName: string) => {
setWorkspaceList(prev => {
const index = prev.findIndex(w => w.id === id);
const newList = [... index <= 1 ? [] : prev.slice(0, index - 1), {
const newList = [... index < 1 ? [] : prev.slice(0, index), {
id,
name: newWorkspaceName,
}, ...prev.slice(index + 1)];
Expand Down

0 comments on commit 42e35b1

Please sign in to comment.