Skip to content

Commit

Permalink
fix: add validation for create directory modal
Browse files Browse the repository at this point in the history
  • Loading branch information
walborn committed Jul 1, 2024
1 parent c9579aa commit f7856aa
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 26 deletions.
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
@import '../../../../styles/mixins.scss';

.ydb-schema-create-directory-dialog {
&__modal {
max-height: 100%;
@include flex-container();
}
&__label {
margin-bottom: 10px;
}
&__description {
color: var(--g-color-text-secondary);
}
&__error {
margin-top: 10px;

color: var(--g-color-private-red-450);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,23 +11,43 @@ const b = cn('ydb-schema-create-directory-dialog');
interface SchemaTreeProps {
open: boolean;
parent: string;
isLoading: boolean;
onClose: () => void;
onSubmit: (value: string) => void;
onUpdate: (value: string) => void;
error: string;
}

export function CreateDirectoryDialog(props: SchemaTreeProps) {
const {open, parent, onClose, onSubmit} = props;
const {open, parent, isLoading, onClose, onSubmit, onUpdate, error} = props;
const [child, setChild] = React.useState('');

const handleClose = () => {
setChild('');
onClose();
};

const handleSubmit = () => {
setChild('');
onSubmit(child);
};

const handleUpdate = (value: string) => {
setChild(value);
};

React.useEffect(() => {
if (!open) {
setChild('');
}
}, [open]);

React.useEffect(() => {
onUpdate(child);
}, [onUpdate, child]);

const invalid = React.useMemo(() => {
return /\s/.test(child);
}, [child]);

return (
<Dialog open={open} onClose={handleClose} onEnterKeyDown={handleSubmit}>
<Dialog.Header caption={i18n('schema.tree.dialog.header')} />
Expand All @@ -39,12 +59,16 @@ export function CreateDirectoryDialog(props: SchemaTreeProps) {
<TextInput
placeholder={i18n('schema.tree.dialog.placeholder')}
value={child}
onUpdate={setChild}
onUpdate={handleUpdate}
autoFocus
hasClear
disabled={isLoading}
validationState={error || invalid ? 'invalid' : undefined}
/>
{error && <div className={b('error')}>{error}</div>}
</Dialog.Body>
<Dialog.Footer
loading={isLoading}
textButtonApply={i18n('schema.tree.dialog.buttonApply')}
textButtonCancel={i18n('schema.tree.dialog.buttonCancel')}
onClickButtonCancel={handleClose}
Expand Down
47 changes: 31 additions & 16 deletions src/containers/Tenant/Schema/SchemaTree/SchemaTree.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -28,18 +28,19 @@ export function SchemaTree(props: SchemaTreeProps) {
const [_, setQueryMode] = useQueryModes();
const [createDirectoryOpen, setCreateDirectoryOpen] = React.useState(false);
const [parent, setParent] = React.useState<string>('');
const [child, setChild] = React.useState<string>('');
const [navigationTreeKey, setNavigationTreeKey] = React.useState('');
const [error, setError] = React.useState<string>('');
const [createDirectory, createDirectoryResult] = schemaApi.useCreateDirectoryMutation();

const [createDirectory, {isSuccess, requestId}] = schemaApi.useCreateDirectoryMutation();

const fetchPath = async (path: string) => {
const fetchPath = async (value: string) => {
const promise = dispatch(
schemaApi.endpoints.getSchema.initiate({path}, {forceRefetch: true}),
schemaApi.endpoints.getSchema.initiate({path: value}, {forceRefetch: true}),
);
const {data} = await promise;
promise.unsubscribe();
if (!data) {
throw new Error(`no describe data about path ${path}`);
throw new Error(`no describe data about path ${value}`);
}
const {PathDescription: {Children = []} = {}} = data;

Expand All @@ -59,10 +60,20 @@ export function SchemaTree(props: SchemaTreeProps) {
};

React.useEffect(() => {
if (isSuccess) {
setNavigationTreeKey(requestId);
if (createDirectoryResult.isSuccess) {
setNavigationTreeKey(createDirectoryResult.requestId);
onActivePathUpdate(`${parent}/${child}`);
setCreateDirectoryOpen(false);
}
}, [createDirectoryResult, onActivePathUpdate, parent, child]);

React.useEffect(() => {
if (createDirectoryResult.isError) {
const errorMessage =
(createDirectoryResult.error as {data: string})?.data || ' Unknown error';
setError(errorMessage);
}
}, [isSuccess, requestId]);
}, [createDirectoryResult]);

React.useEffect(() => {
// if the cached path is not in the current tree, show root
Expand All @@ -75,26 +86,30 @@ export function SchemaTree(props: SchemaTreeProps) {
setCreateDirectoryOpen(false);
};

const handleCreateDirectorySubmit = (child: string) => {
createDirectory({database: parent, path: `${parent}/${child}`});

onActivePathUpdate(`${parent}/${child}`);

setCreateDirectoryOpen(false);
const handleCreateDirectorySubmit = (value: string) => {
setChild(value);
createDirectory({database: parent, path: `${parent}/${value}`});
};

const handleOpenCreateDirectoryDialog = (path: string) => {
setParent(path);
const handleOpenCreateDirectoryDialog = (value: string) => {
setParent(value);
setCreateDirectoryOpen(true);
};

const handleCreateDirectoryUpdate = React.useCallback(() => {
setError('');
}, []);

return (
<React.Fragment>
<CreateDirectoryDialog
open={createDirectoryOpen}
parent={parent}
onClose={handleCreateDirectoryClose}
onSubmit={handleCreateDirectorySubmit}
onUpdate={handleCreateDirectoryUpdate}
isLoading={createDirectoryResult.isLoading}
error={error}
/>
<NavigationTree
key={navigationTreeKey}
Expand Down

0 comments on commit f7856aa

Please sign in to comment.