Selaa lähdekoodia

fix: drop collection always success even it was failed (#799)

Signed-off-by: ryjiang <jiangruiyi@gmail.com>
ryjiang 4 kuukautta sitten
vanhempi
commit
2294a00a62

+ 9 - 1
client/src/context/Data.tsx

@@ -27,6 +27,7 @@ import type {
   CollectionObject,
   CollectionFullObject,
   DatabaseObject,
+  ResStatus,
 } from '@server/types';
 
 export const dataContext = createContext<DataContextType>({
@@ -58,7 +59,12 @@ export const dataContext = createContext<DataContextType>({
   duplicateCollection: async () => {
     return {} as CollectionFullObject;
   },
-  dropCollection: async () => {},
+  dropCollection: async () => {
+    return {
+      error_code: -1,
+      reason: '',
+    };
+  },
   createIndex: async () => {
     return {} as CollectionFullObject;
   },
@@ -319,6 +325,8 @@ export const DataProvider = (props: { children: React.ReactNode }) => {
       // remove collection from state
       setCollections(prev => prev.filter(v => v.collection_name !== name));
     }
+
+    return dropped.data;
   };
 
   // API: create index

+ 2 - 1
client/src/context/Types.ts

@@ -10,6 +10,7 @@ import type {
   DatabaseObject,
   AuthReq,
   AuthObject,
+  ResStatus
 } from '@server/types';
 
 export type RootContextType = {
@@ -130,7 +131,7 @@ export type DataContextType = {
     name: string,
     newName: string
   ) => Promise<CollectionFullObject>;
-  dropCollection: (name: string) => Promise<void>;
+  dropCollection: (name: string) => Promise<ResStatus>;
   createIndex: (param: IndexCreateParam) => Promise<CollectionFullObject>;
   dropIndex: (params: IndexManageParam) => Promise<CollectionFullObject>;
   createAlias: (

+ 20 - 7
client/src/pages/dialogs/DropCollectionDialog.tsx

@@ -14,16 +14,29 @@ const DropCollectionDialog: FC<DropCollectionProps> = props => {
   const { t: dialogTrans } = useTranslation('dialog');
 
   const handleDelete = async () => {
+    const res = [];
     for (const item of collections) {
-      await dropCollection(item.collection_name);
+      res.push(await dropCollection(item.collection_name));
     }
 
-    // show success message
-    openSnackBar(
-      successTrans('delete', {
-        name: collectionTrans('collection'),
-      })
-    );
+    if (res.some(item => item.error_code !== 'Success')) {
+      // show error message
+      openSnackBar(
+        res
+          .filter(item => item.error_code !== 'Success')
+          .map(item => item.reason)
+          .join(', '),
+        'error'
+      );
+      return;
+    } else {
+      // show success message
+      openSnackBar(
+        successTrans('delete', {
+          name: collectionTrans('collection'),
+        })
+      );
+    }
 
     handleCloseDialog();
     onDelete && (await onDelete());