fix: resolve error when environment above selected env gets deleted

This commit is contained in:
Andrew Bastin
2022-03-09 12:13:11 +05:30
parent dee62c2a25
commit ace527e21b

View File

@@ -94,10 +94,19 @@ const dispatchers = defineDispatchers({
{ environments, currentEnvironmentIndex }: EnvironmentStore, { environments, currentEnvironmentIndex }: EnvironmentStore,
{ envIndex }: { envIndex: number } { envIndex }: { envIndex: number }
) { ) {
let newCurrEnvIndex = currentEnvironmentIndex
// Scenario 1: Currently Selected Env is removed -> Set currently selected to none
if (envIndex === currentEnvironmentIndex) newCurrEnvIndex = -1
// Scenario 2: Currently Selected Env Index > Deletion Index -> Current Selection Index Shifts One Position to the left -> Correct Env Index by moving back 1 index
if (envIndex < currentEnvironmentIndex)
newCurrEnvIndex = currentEnvironmentIndex - 1
// Scenario 3: Currently Selected Env Index < Deletion Index -> No change happens at selection position -> Noop
return { return {
environments: environments.filter((_, index) => index !== envIndex), environments: environments.filter((_, index) => index !== envIndex),
currentEnvironmentIndex: currentEnvironmentIndex: newCurrEnvIndex,
envIndex === currentEnvironmentIndex ? -1 : currentEnvironmentIndex,
} }
}, },
renameEnvironment( renameEnvironment(
@@ -260,12 +269,9 @@ export const selectedEnvIndex$ = environmentsStore.subject$.pipe(
distinctUntilChanged() distinctUntilChanged()
) )
export const currentEnvironment$ = combineLatest([ export const currentEnvironment$ = environmentsStore.subject$.pipe(
environments$, map(({ currentEnvironmentIndex, environments }) => {
selectedEnvIndex$, if (currentEnvironmentIndex === -1) {
]).pipe(
map(([envs, selectedIndex]) => {
if (selectedIndex === -1) {
const env: Environment = { const env: Environment = {
name: "No environment", name: "No environment",
variables: [], variables: [],
@@ -273,7 +279,7 @@ export const currentEnvironment$ = combineLatest([
return env return env
} else { } else {
return envs[selectedIndex] return environments[currentEnvironmentIndex]
} }
}) })
) )