From c34185dc4badc9fb048676706ecd7825613b5e3f Mon Sep 17 00:00:00 2001 From: Nivedin <53208152+nivedin@users.noreply.github.com> Date: Thu, 30 Jun 2022 14:42:43 +0530 Subject: [PATCH] fix: environment variables save without pressing 'save' button (#2454) Co-authored-by: Andrew Bastin --- .../components/environments/Details.vue | 78 ++++++++++++++----- 1 file changed, 60 insertions(+), 18 deletions(-) diff --git a/packages/hoppscotch-app/components/environments/Details.vue b/packages/hoppscotch-app/components/environments/Details.vue index e28e5ab13..33c7a6c10 100644 --- a/packages/hoppscotch-app/components/environments/Details.vue +++ b/packages/hoppscotch-app/components/environments/Details.vue @@ -50,18 +50,18 @@
() +const idTicker = ref(0) + const name = ref(null) -const vars = ref([{ key: "", value: "" }]) +const vars = ref([ + { id: idTicker.value++, env: { key: "", value: "" } }, +]) const clearIcon = refAutoReset<"trash-2" | "check">("trash-2", 1000) @@ -187,15 +202,15 @@ const workingEnv = computed(() => { const envList = useReadonlyStream(environments$, []) || props.envVars() const evnExpandError = computed(() => { - for (const variable of vars.value) { - const result = parseTemplateStringE(variable.value.toString(), vars.value) + const variables = pipe( + vars.value, + A.map((e) => e.env) + ) - if (E.isLeft(result)) { - console.error("error", result.left) - return true - } - } - return false + return pipe( + variables, + A.exists(({ value }) => E.isLeft(parseTemplateStringE(value, variables))) + ) }) const liveEnvs = computed(() => { @@ -218,21 +233,38 @@ watch( (show) => { if (show) { 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 = () => { - vars.value = [] + vars.value = [ + { + id: idTicker.value++, + env: { + key: "", + value: "", + }, + }, + ] clearIcon.value = "check" toast.success(`${t("state.cleared")}`) } const addEnvironmentVariable = () => { vars.value.push({ - key: "", - value: "", + id: idTicker.value++, + env: { + key: "", + value: "", + }, }) } @@ -246,9 +278,19 @@ const saveEnvironment = () => { return } + const filterdVariables = pipe( + vars.value, + A.filterMap( + flow( + O.fromPredicate((e) => e.env.key !== ""), + O.map((e) => e.env) + ) + ) + ) + const environmentUpdated: Environment = { name: name.value, - variables: vars.value, + variables: filterdVariables, } if (props.action === "new") {