fix: environments being duplicated (#77)

This commit is contained in:
Akash K
2023-04-09 14:50:12 +05:30
committed by GitHub
parent 4a4ee19ba9
commit 45c84beb81
2 changed files with 42 additions and 50 deletions

View File

@@ -2,7 +2,10 @@ import { authEvents$, def as platformAuth } from "@platform/auth"
import { import {
createEnvironment, createEnvironment,
deleteEnvironment, deleteEnvironment,
environmentsStore,
getLocalIndexByEnvironmentID,
replaceEnvironments, replaceEnvironments,
setGlobalEnvID,
setGlobalEnvVariables, setGlobalEnvVariables,
updateEnvironment, updateEnvironment,
} from "@hoppscotch/common/newstore/environments" } from "@hoppscotch/common/newstore/environments"
@@ -10,11 +13,7 @@ import {
import { EnvironmentsPlatformDef } from "@hoppscotch/common/src/platform/environments" import { EnvironmentsPlatformDef } from "@hoppscotch/common/src/platform/environments"
import { runGQLSubscription } from "@hoppscotch/common/helpers/backend/GQLClient" import { runGQLSubscription } from "@hoppscotch/common/helpers/backend/GQLClient"
import { import { environnmentsSyncer } from "@platform/environments/environments.sync"
environmentsMapper,
globalEnvironmentMapper,
environnmentsSyncer,
} from "@platform/environments/environments.sync"
import * as E from "fp-ts/Either" import * as E from "fp-ts/Either"
import { runDispatchWithOutSyncing } from "@lib/sync" import { runDispatchWithOutSyncing } from "@lib/sync"
@@ -79,10 +78,6 @@ async function loadUserEnvironments() {
const environments = res.right.me.environments const environments = res.right.me.environments
if (environments.length > 0) { if (environments.length > 0) {
environments.forEach((env, index) => {
environmentsMapper.addEntry(index, env.id)
})
runDispatchWithOutSyncing(() => { runDispatchWithOutSyncing(() => {
replaceEnvironments( replaceEnvironments(
environments.map(({ id, variables, name }) => ({ environments.map(({ id, variables, name }) => ({
@@ -105,15 +100,15 @@ async function loadGlobalEnvironments() {
if (globalEnv) { if (globalEnv) {
runDispatchWithOutSyncing(() => { runDispatchWithOutSyncing(() => {
setGlobalEnvVariables(JSON.parse(globalEnv.variables)) setGlobalEnvVariables(JSON.parse(globalEnv.variables))
setGlobalEnvID(globalEnv.id)
}) })
globalEnvironmentMapper.addEntry(0, globalEnv.id)
} }
} else if (res.left.error == "user_environment/user_env_does_not_exists") { } else if (res.left.error == "user_environment/user_env_does_not_exists") {
const res = await createUserGlobalEnvironment(JSON.stringify([])) const res = await createUserGlobalEnvironment(JSON.stringify([]))
if (E.isRight(res)) { if (E.isRight(res)) {
const backendId = res.right.createUserGlobalEnvironment.id const backendId = res.right.createUserGlobalEnvironment.id
globalEnvironmentMapper.addEntry(0, backendId) setGlobalEnvID(backendId)
} }
} }
} }
@@ -128,16 +123,16 @@ function setupUserEnvironmentCreatedSubscription() {
runUserEnvironmentCreatedSubscription() runUserEnvironmentCreatedSubscription()
userEnvironmentCreated$.subscribe((res) => { userEnvironmentCreated$.subscribe((res) => {
console.group("Subscription: User Environment Created")
console.log(res)
console.groupEnd()
if (E.isRight(res)) { if (E.isRight(res)) {
const { name, variables } = res.right.userEnvironmentCreated const { name, variables, id } = res.right.userEnvironmentCreated
if (name) { const isAlreadyExisting = environmentsStore.value.environments.some(
(env) => env.id == id
)
if (name && !isAlreadyExisting) {
runDispatchWithOutSyncing(() => { runDispatchWithOutSyncing(() => {
createEnvironment(name, JSON.parse(variables)) createEnvironment(name, JSON.parse(variables), id)
}) })
} }
} }
@@ -151,10 +146,6 @@ function setupUserEnvironmentUpdatedSubscription() {
runUserEnvironmentUpdatedSubscription() runUserEnvironmentUpdatedSubscription()
userEnvironmentUpdated$.subscribe((res) => { userEnvironmentUpdated$.subscribe((res) => {
console.group("Subscription: User Environment Updated")
console.log(res)
console.groupEnd()
if (E.isRight(res)) { if (E.isRight(res)) {
const { name, variables, id, isGlobal } = res.right.userEnvironmentUpdated const { name, variables, id, isGlobal } = res.right.userEnvironmentUpdated
@@ -166,11 +157,14 @@ function setupUserEnvironmentUpdatedSubscription() {
} else { } else {
// handle the case for normal environments // handle the case for normal environments
const localIndex = environmentsMapper.getLocalIDByBackendID(id) const localIndex = environmentsStore.value.environments.findIndex(
(env) => env.id == id
)
if (localIndex && name) { if ((localIndex || localIndex == 0) && name) {
runDispatchWithOutSyncing(() => { runDispatchWithOutSyncing(() => {
updateEnvironment(localIndex, { updateEnvironment(localIndex, {
id,
name, name,
variables: JSON.parse(variables), variables: JSON.parse(variables),
}) })
@@ -184,27 +178,20 @@ function setupUserEnvironmentUpdatedSubscription() {
} }
function setupUserEnvironmentDeletedSubscription() { function setupUserEnvironmentDeletedSubscription() {
console.log("setting up user environments for user deleted")
const [userEnvironmentDeleted$, userEnvironmentDeletedSub] = const [userEnvironmentDeleted$, userEnvironmentDeletedSub] =
runUserEnvironmentDeletedSubscription() runUserEnvironmentDeletedSubscription()
userEnvironmentDeleted$.subscribe((res) => { userEnvironmentDeleted$.subscribe((res) => {
console.group("Subscription: User Environment Deleted")
console.log(res)
console.groupEnd()
if (E.isRight(res)) { if (E.isRight(res)) {
const { id } = res.right.userEnvironmentDeleted const { id } = res.right.userEnvironmentDeleted
const localIndex = environmentsMapper.getLocalIDByBackendID(id) // TODO: move getLocalIndexByID to a getter in the environmentsStore
const localIndex = getLocalIndexByEnvironmentID(id)
if (localIndex) { if (localIndex || localIndex === 0) {
runDispatchWithOutSyncing(() => { runDispatchWithOutSyncing(() => {
deleteEnvironment(localIndex) deleteEnvironment(localIndex)
}) })
environmentsMapper.removeEntry(id)
} else { } else {
console.log("could not find the localIndex") console.log("could not find the localIndex")
// TODO: // TODO:

View File

@@ -1,4 +1,8 @@
import { environmentsStore } from "@hoppscotch/common/newstore/environments" import {
environmentsStore,
getGlobalVariableID,
removeDuplicateEntry,
} from "@hoppscotch/common/newstore/environments"
import { import {
getSettingSubject, getSettingSubject,
settingsStore, settingsStore,
@@ -25,11 +29,13 @@ export const storeSyncDefinition: StoreSyncDefinitionOf<
> = { > = {
async createEnvironment({ name, variables }) { async createEnvironment({ name, variables }) {
const lastCreatedEnvIndex = environmentsStore.value.environments.length - 1 const lastCreatedEnvIndex = environmentsStore.value.environments.length - 1
const res = await createUserEnvironment(name, JSON.stringify(variables)) const res = await createUserEnvironment(name, JSON.stringify(variables))
if (E.isRight(res)) { if (E.isRight(res)) {
const id = res.right.createUserEnvironment.id const id = res.right.createUserEnvironment.id
environmentsMapper.addEntry(lastCreatedEnvIndex, id) environmentsStore.value.environments[lastCreatedEnvIndex].id = id
removeDuplicateEntry(id)
} }
}, },
async appendEnvironments({ envs }) { async appendEnvironments({ envs }) {
@@ -48,7 +54,9 @@ export const storeSyncDefinition: StoreSyncDefinitionOf<
if (E.isRight(res)) { if (E.isRight(res)) {
const id = res.right.createUserEnvironment.id const id = res.right.createUserEnvironment.id
environmentsMapper.addEntry(envId, id) environmentsStore.value.environments[envId].id = id
removeDuplicateEntry(id)
} }
})() })()
}) })
@@ -62,41 +70,38 @@ export const storeSyncDefinition: StoreSyncDefinitionOf<
if (environmentToDuplicate) { if (environmentToDuplicate) {
const res = await createUserEnvironment( const res = await createUserEnvironment(
environmentToDuplicate?.name, `${environmentToDuplicate?.name} - Duplicate`,
JSON.stringify(environmentToDuplicate?.variables) JSON.stringify(environmentToDuplicate?.variables)
) )
if (E.isRight(res)) { if (E.isRight(res)) {
const id = res.right.createUserEnvironment.id const id = res.right.createUserEnvironment.id
environmentsMapper.addEntry(lastCreatedEnvIndex, id) environmentsStore.value.environments[lastCreatedEnvIndex].id = id
removeDuplicateEntry(id)
} }
} }
}, },
updateEnvironment({ envIndex, updatedEnv }) { updateEnvironment({ envIndex, updatedEnv }) {
const backendId = environmentsMapper.getBackendIDByLocalID(envIndex) const backendId = environmentsStore.value.environments[envIndex].id
console.log(environmentsMapper)
if (backendId) { if (backendId) {
updateUserEnvironment(backendId, updatedEnv)() updateUserEnvironment(backendId, updatedEnv)()
} }
}, },
async deleteEnvironment({ envIndex }) { async deleteEnvironment({ _, envID }) {
const backendId = environmentsMapper.getBackendIDByLocalID(envIndex) if (envID) {
await deleteUserEnvironment(envID)()
if (backendId) {
await deleteUserEnvironment(backendId)()
environmentsMapper.removeEntry(backendId)
} }
}, },
setGlobalVariables({ entries }) { setGlobalVariables({ entries }) {
const backendId = globalEnvironmentMapper.getBackendIDByLocalID(0) const backendId = getGlobalVariableID()
if (backendId) { if (backendId) {
updateUserEnvironment(backendId, { name: "", variables: entries })() updateUserEnvironment(backendId, { name: "", variables: entries })()
} }
}, },
clearGlobalVariables() { clearGlobalVariables() {
const backendId = globalEnvironmentMapper.getBackendIDByLocalID(0) const backendId = getGlobalVariableID()
if (backendId) { if (backendId) {
clearGlobalEnvironmentVariables(backendId) clearGlobalEnvironmentVariables(backendId)