fix: environment variables save without pressing 'save' button (#2454)
Co-authored-by: Andrew Bastin <andrewbastin.k@gmail.com>
This commit is contained in:
@@ -50,18 +50,18 @@
|
|||||||
</div>
|
</div>
|
||||||
<div class="border rounded divide-y divide-dividerLight border-divider">
|
<div class="border rounded divide-y divide-dividerLight border-divider">
|
||||||
<div
|
<div
|
||||||
v-for="(variable, index) in vars"
|
v-for="({ id, env }, index) in vars"
|
||||||
:key="`variable-${index}`"
|
:key="`variable-${id}-${index}`"
|
||||||
class="flex divide-x divide-dividerLight"
|
class="flex divide-x divide-dividerLight"
|
||||||
>
|
>
|
||||||
<input
|
<input
|
||||||
v-model="variable.key"
|
v-model="env.key"
|
||||||
class="flex flex-1 px-4 py-2 bg-transparent"
|
class="flex flex-1 px-4 py-2 bg-transparent"
|
||||||
:placeholder="`${t('count.variable', { count: index + 1 })}`"
|
:placeholder="`${t('count.variable', { count: index + 1 })}`"
|
||||||
:name="'param' + index"
|
:name="'param' + index"
|
||||||
/>
|
/>
|
||||||
<SmartEnvInput
|
<SmartEnvInput
|
||||||
v-model="variable.value"
|
v-model="env.value"
|
||||||
:placeholder="`${t('count.value', { count: index + 1 })}`"
|
:placeholder="`${t('count.value', { count: index + 1 })}`"
|
||||||
:envs="liveEnvs"
|
:envs="liveEnvs"
|
||||||
:name="'value' + index"
|
:name="'value' + index"
|
||||||
@@ -119,6 +119,9 @@
|
|||||||
import clone from "lodash/clone"
|
import clone from "lodash/clone"
|
||||||
import { computed, ref, watch } from "@nuxtjs/composition-api"
|
import { computed, ref, watch } from "@nuxtjs/composition-api"
|
||||||
import * as E from "fp-ts/Either"
|
import * as E from "fp-ts/Either"
|
||||||
|
import * as A from "fp-ts/Array"
|
||||||
|
import * as O from "fp-ts/Option"
|
||||||
|
import { pipe, flow } from "fp-ts/function"
|
||||||
import { Environment, parseTemplateStringE } from "@hoppscotch/data"
|
import { Environment, parseTemplateStringE } from "@hoppscotch/data"
|
||||||
import { refAutoReset } from "@vueuse/core"
|
import { refAutoReset } from "@vueuse/core"
|
||||||
import {
|
import {
|
||||||
@@ -137,6 +140,14 @@ import {
|
|||||||
useToast,
|
useToast,
|
||||||
} from "~/helpers/utils/composables"
|
} from "~/helpers/utils/composables"
|
||||||
|
|
||||||
|
type EnvironmentVariable = {
|
||||||
|
id: number
|
||||||
|
env: {
|
||||||
|
key: string
|
||||||
|
value: string
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
const t = useI18n()
|
const t = useI18n()
|
||||||
const toast = useToast()
|
const toast = useToast()
|
||||||
|
|
||||||
@@ -159,8 +170,12 @@ const emit = defineEmits<{
|
|||||||
(e: "hide-modal"): void
|
(e: "hide-modal"): void
|
||||||
}>()
|
}>()
|
||||||
|
|
||||||
|
const idTicker = ref(0)
|
||||||
|
|
||||||
const name = ref<string | null>(null)
|
const name = ref<string | null>(null)
|
||||||
const vars = ref([{ key: "", value: "" }])
|
const vars = ref<EnvironmentVariable[]>([
|
||||||
|
{ id: idTicker.value++, env: { key: "", value: "" } },
|
||||||
|
])
|
||||||
|
|
||||||
const clearIcon = refAutoReset<"trash-2" | "check">("trash-2", 1000)
|
const clearIcon = refAutoReset<"trash-2" | "check">("trash-2", 1000)
|
||||||
|
|
||||||
@@ -187,15 +202,15 @@ const workingEnv = computed(() => {
|
|||||||
const envList = useReadonlyStream(environments$, []) || props.envVars()
|
const envList = useReadonlyStream(environments$, []) || props.envVars()
|
||||||
|
|
||||||
const evnExpandError = computed(() => {
|
const evnExpandError = computed(() => {
|
||||||
for (const variable of vars.value) {
|
const variables = pipe(
|
||||||
const result = parseTemplateStringE(variable.value.toString(), vars.value)
|
vars.value,
|
||||||
|
A.map((e) => e.env)
|
||||||
|
)
|
||||||
|
|
||||||
if (E.isLeft(result)) {
|
return pipe(
|
||||||
console.error("error", result.left)
|
variables,
|
||||||
return true
|
A.exists(({ value }) => E.isLeft(parseTemplateStringE(value, variables)))
|
||||||
}
|
)
|
||||||
}
|
|
||||||
return false
|
|
||||||
})
|
})
|
||||||
|
|
||||||
const liveEnvs = computed(() => {
|
const liveEnvs = computed(() => {
|
||||||
@@ -218,21 +233,38 @@ watch(
|
|||||||
(show) => {
|
(show) => {
|
||||||
if (show) {
|
if (show) {
|
||||||
name.value = workingEnv.value?.name ?? null
|
name.value = workingEnv.value?.name ?? null
|
||||||
vars.value = clone(workingEnv.value?.variables ?? [])
|
vars.value = pipe(
|
||||||
|
workingEnv.value?.variables ?? [],
|
||||||
|
A.map((e) => ({
|
||||||
|
id: idTicker.value++,
|
||||||
|
env: clone(e),
|
||||||
|
}))
|
||||||
|
)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
|
|
||||||
const clearContent = () => {
|
const clearContent = () => {
|
||||||
vars.value = []
|
vars.value = [
|
||||||
|
{
|
||||||
|
id: idTicker.value++,
|
||||||
|
env: {
|
||||||
|
key: "",
|
||||||
|
value: "",
|
||||||
|
},
|
||||||
|
},
|
||||||
|
]
|
||||||
clearIcon.value = "check"
|
clearIcon.value = "check"
|
||||||
toast.success(`${t("state.cleared")}`)
|
toast.success(`${t("state.cleared")}`)
|
||||||
}
|
}
|
||||||
|
|
||||||
const addEnvironmentVariable = () => {
|
const addEnvironmentVariable = () => {
|
||||||
vars.value.push({
|
vars.value.push({
|
||||||
key: "",
|
id: idTicker.value++,
|
||||||
value: "",
|
env: {
|
||||||
|
key: "",
|
||||||
|
value: "",
|
||||||
|
},
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -246,9 +278,19 @@ const saveEnvironment = () => {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const filterdVariables = pipe(
|
||||||
|
vars.value,
|
||||||
|
A.filterMap(
|
||||||
|
flow(
|
||||||
|
O.fromPredicate((e) => e.env.key !== ""),
|
||||||
|
O.map((e) => e.env)
|
||||||
|
)
|
||||||
|
)
|
||||||
|
)
|
||||||
|
|
||||||
const environmentUpdated: Environment = {
|
const environmentUpdated: Environment = {
|
||||||
name: name.value,
|
name: name.value,
|
||||||
variables: vars.value,
|
variables: filterdVariables,
|
||||||
}
|
}
|
||||||
|
|
||||||
if (props.action === "new") {
|
if (props.action === "new") {
|
||||||
|
|||||||
Reference in New Issue
Block a user