Browse Source

refactor: update coll properties when syncing

nivedin 1 year ago
parent
commit
c18e801420

+ 15 - 0
packages/hoppscotch-selfhost-web/src/api/mutations/UpdateUserCollection.graphql

@@ -0,0 +1,15 @@
+mutation UpdateUserCollection(
+  $userCollectionID: ID!
+  $newTitle: String
+  $data: String
+) {
+  updateUserCollection(
+    userCollectionID: $userCollectionID
+    newTitle: $newTitle
+    data: $data
+  ) {
+    id
+    title
+    data
+  }
+}

+ 2 - 0
packages/hoppscotch-selfhost-web/src/api/queries/GetUserRootCollections.graphql

@@ -4,10 +4,12 @@ query GetUserRootCollections {
     id
     title
     type
+    data
     childrenREST {
       id
       title
       type
+      data
     }
   }
 }

+ 1 - 0
packages/hoppscotch-selfhost-web/src/api/subscriptions/UserCollectionCreated.graphql

@@ -6,5 +6,6 @@ subscription UserCollectionCreated {
     id
     title
     type
+    data
   }
 }

+ 1 - 0
packages/hoppscotch-selfhost-web/src/api/subscriptions/UserCollectionUpdated.graphql

@@ -3,6 +3,7 @@ subscription userCollectionUpdated {
     id
     title
     type
+    data
     parent {
       id
     }

+ 14 - 0
packages/hoppscotch-selfhost-web/src/platform/collections/collections.api.ts

@@ -65,6 +65,9 @@ import {
   GetGqlRootUserCollectionsQueryVariables,
   GetGqlRootUserCollectionsDocument,
   ReqType,
+  UpdateUserCollectionMutation,
+  UpdateUserCollectionMutationVariables,
+  UpdateUserCollectionDocument,
 } from "../../api/generated/graphql"
 
 export const createRESTRootUserCollection = (title: string) =>
@@ -160,6 +163,17 @@ export const renameUserCollection = (
     ""
   >(RenameUserCollectionDocument, { userCollectionID, newTitle })()
 
+export const updateUserCollection = (
+  userCollectionID: string,
+  newTitle?: string,
+  data?: string
+) =>
+  runMutation<
+    UpdateUserCollectionMutation,
+    UpdateUserCollectionMutationVariables,
+    ""
+  >(UpdateUserCollectionDocument, { userCollectionID, newTitle, data })()
+
 export const moveUserCollection = (
   sourceCollectionID: string,
   destinationCollectionID?: string

+ 17 - 16
packages/hoppscotch-selfhost-web/src/platform/collections/collections.platform.ts

@@ -89,8 +89,7 @@ type ExportedUserCollectionREST = {
   folders: ExportedUserCollectionREST[]
   requests: Array<HoppRESTRequest & { id: string }>
   name: string
-  auth: HoppRESTRequest["auth"]
-  headers: HoppRESTRequest["headers"]
+  data: string
 }
 
 type ExportedUserCollectionGQL = {
@@ -98,8 +97,7 @@ type ExportedUserCollectionGQL = {
   folders: ExportedUserCollectionGQL[]
   requests: Array<HoppGQLRequest & { id: string }>
   name: string
-  auth: HoppGQLRequest["auth"]
-  headers: HoppGQLRequest["headers"]
+  data: string
 }
 
 function exportedCollectionToHoppCollection(
@@ -108,6 +106,8 @@ function exportedCollectionToHoppCollection(
 ): HoppCollection<HoppRESTRequest | HoppGQLRequest> {
   if (collectionType == "REST") {
     const restCollection = collection as ExportedUserCollectionREST
+    const data = restCollection.data ? JSON.parse(restCollection.data) : null
+
     return {
       id: restCollection.id,
       v: 1,
@@ -142,11 +142,12 @@ function exportedCollectionToHoppCollection(
           testScript,
         })
       ),
-      auth: restCollection.auth,
-      headers: restCollection.headers,
+      auth: data.auth ?? { authType: "inherit", authActive: false },
+      headers: data.headers ?? [],
     }
   } else {
     const gqlCollection = collection as ExportedUserCollectionGQL
+    const data = gqlCollection.data ? JSON.parse(gqlCollection.data) : null
 
     return {
       id: gqlCollection.id,
@@ -164,8 +165,8 @@ function exportedCollectionToHoppCollection(
           name,
         })
       ) as HoppGQLRequest[],
-      auth: gqlCollection.auth,
-      headers: gqlCollection.headers,
+      auth: data.auth,
+      headers: data.headers,
     }
   }
 }
@@ -297,6 +298,9 @@ function setupUserCollectionCreatedSubscription() {
         })
       } else {
         // root collections won't have parentCollectionID
+        const data = res.right.userCollectionCreated.data
+          ? JSON.parse(res.right.userCollectionCreated.data)
+          : null
         runDispatchWithOutSyncing(() => {
           collectionType == "GQL"
             ? addGraphqlCollection({
@@ -304,22 +308,19 @@ function setupUserCollectionCreatedSubscription() {
                 folders: [],
                 requests: [],
                 v: 1,
-                auth: {
-                  authType: "none",
-                  authActive: false,
-                },
-                headers: [],
+                auth: data?.auth ?? { authType: "inherit", authActive: false },
+                headers: data?.headers ?? [],
               })
             : addRESTCollection({
                 name: res.right.userCollectionCreated.title,
                 folders: [],
                 requests: [],
                 v: 1,
-                auth: {
-                  authType: "none",
+                auth: data?.auth ?? {
+                  authType: "inherit",
                   authActive: false,
                 },
-                headers: [],
+                headers: data?.headers ?? [],
               })
 
           const localIndex = collectionStore.value.state.length - 1

+ 14 - 6
packages/hoppscotch-selfhost-web/src/platform/collections/collections.sync.ts

@@ -24,7 +24,7 @@ import {
   editUserRequest,
   moveUserCollection,
   moveUserRequest,
-  renameUserCollection,
+  updateUserCollection,
   updateUserCollectionOrder,
 } from "./collections.api"
 
@@ -155,8 +155,13 @@ export const storeSyncDefinition: StoreSyncDefinitionOf<
       [collectionIndex]
     )?.id
 
-    if (collectionID && collection.name) {
-      renameUserCollection(collectionID, collection.name)
+    const data = {
+      auth: collection.auth,
+      headers: collection.headers,
+    }
+
+    if (collectionID) {
+      updateUserCollection(collectionID, collection.name, JSON.stringify(data))
     }
   },
   async addFolder({ name, path }) {
@@ -195,9 +200,12 @@ export const storeSyncDefinition: StoreSyncDefinitionOf<
     )?.id
 
     const folderName = folder.name
-
-    if (folderID && folderName) {
-      renameUserCollection(folderID, folderName)
+    const data = {
+      auth: folder.auth,
+      headers: folder.headers,
+    }
+    if (folderID) {
+      updateUserCollection(folderID, folderName, JSON.stringify(data))
     }
   },
   async removeFolder({ folderID }) {