Browse Source

Create collection dialog

ruiyi.jiang 2 years ago
parent
commit
488bcaa6e7

+ 3 - 31
client/src/pages/collections/Collections.tsx

@@ -22,7 +22,7 @@ import { makeStyles, Theme } from '@material-ui/core';
 import StatusIcon from '../../components/status/StatusIcon';
 import CustomToolTip from '../../components/customToolTip/CustomToolTip';
 import { rootContext } from '../../context/Root';
-import CreateCollection from './Create';
+import CreateCollectionDialog from '../dialogs/CreateCollectionDialog';
 import DeleteTemplate from '../../components/customDialog/DeleteDialogTemplate';
 import { CollectionHttp } from '../../http/Collection';
 import { useInsertDialogHook } from '../../hooks/Dialog';
@@ -210,33 +210,7 @@ const Collections = () => {
     }
   };
 
-  const handleCreateCollection = async (param: CollectionCreateParam) => {
-    const data: CollectionCreateParam = JSON.parse(JSON.stringify(param));
-    const vectorType = [DataTypeEnum.BinaryVector, DataTypeEnum.FloatVector];
-
-    data.fields = data.fields.map(v =>
-      vectorType.includes(v.data_type)
-        ? {
-            ...v,
-            type_params: {
-              // if data type is vector, dimension must exist.
-              dim: v.dimension!,
-            },
-          }
-        : v.data_type === DataTypeEnum.VarChar
-        ? {
-            ...v,
-            type_params: {
-              max_length: v.max_length!,
-            },
-          }
-        : v
-    );
-
-    await CollectionHttp.createCollection({
-      ...data,
-      consistency_level: data.consistency_level,
-    });
+  const onCreate = () => {
     handleCloseDialog();
     openSnackBar(
       successTrans('create', { name: collectionTrans('collection') })
@@ -280,9 +254,7 @@ const Collections = () => {
           open: true,
           type: 'custom',
           params: {
-            component: (
-              <CreateCollection handleCreate={handleCreateCollection} />
-            ),
+            component: <CreateCollectionDialog onCreate={onCreate} />,
           },
         });
       },

+ 1 - 1
client/src/pages/collections/Types.ts

@@ -39,7 +39,7 @@ export interface CollectionView extends CollectionData {
 }
 
 export interface CollectionCreateProps {
-  handleCreate: (param: CollectionCreateParam) => void;
+  onCreate?: () => void;
 }
 
 export interface CollectionCreateParam {

+ 29 - 8
client/src/pages/collections/Create.tsx → client/src/pages/dialogs/CreateCollectionDialog.tsx

@@ -9,15 +9,16 @@ import { rootContext } from '../../context/Root';
 import { useFormValidation } from '../../hooks/Form';
 import { formatForm } from '../../utils/Form';
 import { TypeEnum } from '../../utils/Validation';
-import CreateFields from './CreateFields';
+import CreateFields from '../collections/CreateFields';
+import { CollectionHttp } from '../../http/Collection';
 import {
   CollectionCreateParam,
   CollectionCreateProps,
   DataTypeEnum,
   ConsistencyLevelEnum,
   Field,
-} from './Types';
-import { CONSISTENCY_LEVEL_OPTIONS } from './Constants';
+} from '../collections/Types';
+import { CONSISTENCY_LEVEL_OPTIONS } from '../collections/Constants';
 
 const useStyles = makeStyles((theme: Theme) => ({
   fieldset: {
@@ -53,7 +54,7 @@ const useStyles = makeStyles((theme: Theme) => ({
   },
 }));
 
-const CreateCollection: FC<CollectionCreateProps> = ({ handleCreate }) => {
+const CreateCollectionDialog: FC<CollectionCreateProps> = ({ onCreate }) => {
   const classes = useStyles();
   const { handleCloseDialog } = useContext(rootContext);
   const { t: collectionTrans } = useTranslation('collection');
@@ -178,7 +179,7 @@ const CreateCollection: FC<CollectionCreateProps> = ({ handleCreate }) => {
     },
   ];
 
-  const handleCreateCollection = () => {
+  const handleCreateCollection = async () => {
     const vectorType = [DataTypeEnum.BinaryVector, DataTypeEnum.FloatVector];
     const param: CollectionCreateParam = {
       ...form,
@@ -194,11 +195,31 @@ const CreateCollection: FC<CollectionCreateProps> = ({ handleCreate }) => {
 
         v.is_primary_key && (data.autoID = form.autoID);
 
-        return data;
+        return vectorType.includes(v.data_type)
+          ? {
+              ...data,
+              type_params: {
+                // if data type is vector, dimension must exist.
+                dim: data.dimension!,
+              },
+            }
+          : v.data_type === DataTypeEnum.VarChar
+          ? {
+              ...v,
+              type_params: {
+                max_length: v.max_length!,
+              },
+            }
+          : v;
       }),
       consistency_level: consistencyLevel,
     };
-    handleCreate(param);
+
+    await CollectionHttp.createCollection({
+      ...param,
+    });
+
+    onCreate && onCreate();
   };
 
   return (
@@ -256,4 +277,4 @@ const CreateCollection: FC<CollectionCreateProps> = ({ handleCreate }) => {
   );
 };
 
-export default CreateCollection;
+export default CreateCollectionDialog;