Преглед на файлове

feat: update collections if restful api do something about collection (#832)

Signed-off-by: ryjiang <jiangruiyi@gmail.com>
ryjiang преди 3 седмици
родител
ревизия
2ceea589cf
променени са 4 файла, в които са добавени 44 реда и са изтрити 13 реда
  1. 16 1
      client/src/pages/play/Play.tsx
  2. 17 12
      client/src/pages/play/Types.ts
  3. 10 0
      client/src/pages/play/language/extensions/codelens.ts
  4. 1 0
      client/src/pages/play/utils/event.ts

+ 16 - 1
client/src/pages/play/Play.tsx

@@ -45,7 +45,7 @@ const Play: FC = () => {
     return savedResult || '{}';
   });
 
-  const { collections, databases, loading } = useContext(dataContext);
+  const { collections, databases, fetchCollections } = useContext(dataContext);
   const { isManaged, authReq } = useContext(authContext);
 
   // styles
@@ -144,6 +144,21 @@ const Play: FC = () => {
     };
   }, []);
 
+  useEffect(() => {
+    const handleCollectionUpdate = async (event: Event) => {
+      await fetchCollections();
+    };
+
+    const unsubscribe = DocumentEventManager.subscribe(
+      CustomEventNameEnum.PlaygroundCollectionUpdate,
+      handleCollectionUpdate
+    );
+
+    return () => {
+      unsubscribe();
+    };
+  }, []);
+
   return (
     <Box className={classes.root}>
       <Paper elevation={0} className={classes.leftPane}>

+ 17 - 12
client/src/pages/play/Types.ts

@@ -1,22 +1,27 @@
-import { type AxiosError, type AxiosResponse } from "axios"
+import { type AxiosError, type AxiosResponse } from 'axios';
 
 export enum CustomEventNameEnum {
-  PlaygroundResponseDetail = 'playgroundResponseDetail'
+  PlaygroundResponseDetail = 'playgroundResponseDetail',
+  PlaygroundCollectionUpdate = 'playgroundCollectionUpdate',
 }
 
 export interface PlaygroundCustomEventDetail {
-  loading?: boolean
-  response?: AxiosResponse['data']
-  error?: object
+  loading?: boolean;
+  response?: AxiosResponse['data'];
+  error?: object;
+}
+
+export interface PlaygroundCollectionUpdateEventDetail {
+  collectionName: string;
 }
 
 export interface PlaygroundExtensionParams {
-  baseUrl: string
-  isManaged?: boolean
-  token?: string
-  username?: string
-  password?: string
-  isDarkMode?: boolean
+  baseUrl: string;
+  isManaged?: boolean;
+  token?: string;
+  username?: string;
+  password?: string;
+  isDarkMode?: boolean;
 }
 
 export type IdentifierMap = {
@@ -31,4 +36,4 @@ export type CompletionMacro = {
   apply: string;
   type: 'text';
   detail: 'macro';
-}
+};

+ 10 - 0
client/src/pages/play/language/extensions/codelens.ts

@@ -137,11 +137,21 @@ export const codeLensDecoration = (options: PlaygroundExtensionParams) =>
                     CustomEventNameEnum.PlaygroundResponseDetail,
                     { loading: true, response: 'running' }
                   );
+
                   const res = await playgroundRequest(params);
+
                   DocumentEventManager.dispatch(
                     CustomEventNameEnum.PlaygroundResponseDetail,
                     { response: res.data, loading: false }
                   );
+
+                  // if param body contains collectionName, dispatch PlaygroundCollectionUpdate event
+                  if ('collectionName' in params.body) {
+                    DocumentEventManager.dispatch(
+                      CustomEventNameEnum.PlaygroundCollectionUpdate,
+                      { collectionName: params.body.collectionName as string }
+                    );
+                  }
                 } catch (err) {
                   DocumentEventManager.dispatch(
                     CustomEventNameEnum.PlaygroundResponseDetail,

+ 1 - 0
client/src/pages/play/utils/event.ts

@@ -2,6 +2,7 @@ import { type PlaygroundCustomEventDetail, CustomEventNameEnum } from '../Types'
 
 type EventMap = {
   [CustomEventNameEnum.PlaygroundResponseDetail]: PlaygroundCustomEventDetail;
+  [CustomEventNameEnum.PlaygroundCollectionUpdate]: { collectionName: string };
 };
 
 export class DocumentEventManager {