Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React, { useCallback, useLayoutEffect, useMemo, useState } from 'react';
import React, { useMemo } from 'react';
import { connect } from 'react-redux';
import toNS from 'mongodb-ns';
import type { Relationship } from '../../services/data-model-storage';
Expand Down Expand Up @@ -116,8 +116,21 @@ const CollectionDrawerContent: React.FunctionComponent<
onNoteChange,
onRenameCollection,
}) => {
const [collectionName, setCollectionName] = useState(
() => toNS(namespace).collection
const { value: collectionName, ...nameInputProps } = useChangeOnBlur(
toNS(namespace).collection,
(collectionName) => {
const trimmedName = collectionName.trim();
if (trimmedName === toNS(namespace).collection) {
return;
}
if (!isCollectionNameValid) {
return;
}
onRenameCollection(
namespace,
`${toNS(namespace).database}.${trimmedName}`
);
}
);

const {
Expand All @@ -128,23 +141,6 @@ const CollectionDrawerContent: React.FunctionComponent<
[collectionName, namespaces, namespace]
);

useLayoutEffect(() => {
setCollectionName(toNS(namespace).collection);
}, [namespace]);

const onBlurCollectionName = useCallback(() => {
const trimmedName = collectionName.trim();
if (trimmedName === toNS(namespace).collection) {
return;
}

if (!isCollectionNameValid) {
return;
}

onRenameCollection(namespace, `${toNS(namespace).database}.${trimmedName}`);
}, [collectionName, namespace, onRenameCollection, isCollectionNameValid]);

const noteInputProps = useChangeOnBlur(note, (newNote) => {
onNoteChange(namespace, newNote);
});
Expand All @@ -157,12 +153,9 @@ const CollectionDrawerContent: React.FunctionComponent<
label="Name"
sizeVariant="small"
value={collectionName}
{...nameInputProps}
state={isCollectionNameValid ? undefined : 'error'}
errorMessage={collectionNameEditErrorMessage}
onChange={(e) => {
setCollectionName(e.target.value);
}}
onBlur={onBlurCollectionName}
/>
</DMFormFieldContainer>
</DMDrawerSection>
Expand Down
Loading