refactor: combine add and edit environment modals (#2131)
This commit is contained in:
@@ -1,78 +0,0 @@
|
|||||||
<template>
|
|
||||||
<SmartModal
|
|
||||||
v-if="show"
|
|
||||||
:title="`${$t('environment.new')}`"
|
|
||||||
@close="hideModal"
|
|
||||||
>
|
|
||||||
<template #body>
|
|
||||||
<div class="flex flex-col px-2">
|
|
||||||
<input
|
|
||||||
id="selectLabelEnvAdd"
|
|
||||||
v-model="name"
|
|
||||||
v-focus
|
|
||||||
class="input floating-input"
|
|
||||||
placeholder=" "
|
|
||||||
type="text"
|
|
||||||
autocomplete="off"
|
|
||||||
@keyup.enter="addNewEnvironment"
|
|
||||||
/>
|
|
||||||
<label for="selectLabelEnvAdd">
|
|
||||||
{{ $t("action.label") }}
|
|
||||||
</label>
|
|
||||||
</div>
|
|
||||||
</template>
|
|
||||||
<template #footer>
|
|
||||||
<span>
|
|
||||||
<ButtonPrimary
|
|
||||||
:label="`${$t('action.save')}`"
|
|
||||||
@click.native="addNewEnvironment"
|
|
||||||
/>
|
|
||||||
<ButtonSecondary
|
|
||||||
:label="`${$t('action.cancel')}`"
|
|
||||||
@click.native="hideModal"
|
|
||||||
/>
|
|
||||||
</span>
|
|
||||||
</template>
|
|
||||||
</SmartModal>
|
|
||||||
</template>
|
|
||||||
|
|
||||||
<script lang="ts">
|
|
||||||
import { defineComponent } from "@nuxtjs/composition-api"
|
|
||||||
import { useReadonlyStream } from "~/helpers/utils/composables"
|
|
||||||
import { createEnvironment, environments$ } from "~/newstore/environments"
|
|
||||||
|
|
||||||
export default defineComponent({
|
|
||||||
props: {
|
|
||||||
show: Boolean,
|
|
||||||
},
|
|
||||||
setup() {
|
|
||||||
return {
|
|
||||||
envList: useReadonlyStream(environments$, []),
|
|
||||||
}
|
|
||||||
},
|
|
||||||
data() {
|
|
||||||
return {
|
|
||||||
name: null as string | null,
|
|
||||||
}
|
|
||||||
},
|
|
||||||
methods: {
|
|
||||||
addNewEnvironment() {
|
|
||||||
if (!this.name) {
|
|
||||||
this.$toast.error(`${this.$t("environment.invalid_name")}`)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
createEnvironment(this.name)
|
|
||||||
// TODO: find better way to get index of new environment
|
|
||||||
this.$emit("environment-added", {
|
|
||||||
name: this.name,
|
|
||||||
index: this.envList.length - 1,
|
|
||||||
})
|
|
||||||
this.hideModal()
|
|
||||||
},
|
|
||||||
hideModal() {
|
|
||||||
this.name = null
|
|
||||||
this.$emit("hide-modal")
|
|
||||||
},
|
|
||||||
},
|
|
||||||
})
|
|
||||||
</script>
|
|
||||||
@@ -1,7 +1,7 @@
|
|||||||
<template>
|
<template>
|
||||||
<SmartModal
|
<SmartModal
|
||||||
v-if="show"
|
v-if="show"
|
||||||
:title="`${$t('environment.edit')}`"
|
:title="$t(`environment.${action}`)"
|
||||||
@close="hideModal"
|
@close="hideModal"
|
||||||
>
|
>
|
||||||
<template #body>
|
<template #body>
|
||||||
@@ -120,9 +120,12 @@ import { computed, defineComponent, PropType } from "@nuxtjs/composition-api"
|
|||||||
import * as E from "fp-ts/Either"
|
import * as E from "fp-ts/Either"
|
||||||
import { Environment, parseTemplateStringE } from "@hoppscotch/data"
|
import { Environment, parseTemplateStringE } from "@hoppscotch/data"
|
||||||
import {
|
import {
|
||||||
|
createEnvironment,
|
||||||
|
environments$,
|
||||||
getEnviroment,
|
getEnviroment,
|
||||||
getGlobalVariables,
|
getGlobalVariables,
|
||||||
globalEnv$,
|
globalEnv$,
|
||||||
|
setCurrentEnvironment,
|
||||||
setGlobalEnvVariables,
|
setGlobalEnvVariables,
|
||||||
updateEnvironment,
|
updateEnvironment,
|
||||||
} from "~/newstore/environments"
|
} from "~/newstore/environments"
|
||||||
@@ -131,30 +134,44 @@ import { useReadonlyStream } from "~/helpers/utils/composables"
|
|||||||
export default defineComponent({
|
export default defineComponent({
|
||||||
props: {
|
props: {
|
||||||
show: Boolean,
|
show: Boolean,
|
||||||
|
action: {
|
||||||
|
type: String as PropType<"new" | "edit">,
|
||||||
|
default: "edit",
|
||||||
|
},
|
||||||
editingEnvironmentIndex: {
|
editingEnvironmentIndex: {
|
||||||
type: [Number, String] as PropType<number | "Global" | null>,
|
type: [Number, String] as PropType<number | "Global" | null>,
|
||||||
default: null,
|
default: null,
|
||||||
},
|
},
|
||||||
|
envVars: {
|
||||||
|
type: Function as PropType<() => Environment["variables"]>,
|
||||||
|
default: () => [],
|
||||||
|
},
|
||||||
},
|
},
|
||||||
setup(props) {
|
setup(props) {
|
||||||
const globalVars = useReadonlyStream(globalEnv$, [])
|
const globalVars = useReadonlyStream(globalEnv$, [])
|
||||||
|
|
||||||
const workingEnv = computed(() => {
|
const workingEnv = computed(() => {
|
||||||
if (props.editingEnvironmentIndex === null) return null
|
|
||||||
|
|
||||||
if (props.editingEnvironmentIndex === "Global") {
|
if (props.editingEnvironmentIndex === "Global") {
|
||||||
return {
|
return {
|
||||||
name: "Global",
|
name: "Global",
|
||||||
variables: getGlobalVariables(),
|
variables: getGlobalVariables(),
|
||||||
} as Environment
|
} as Environment
|
||||||
} else {
|
} else if (props.action === "new") {
|
||||||
|
return {
|
||||||
|
name: "",
|
||||||
|
variables: props.envVars(),
|
||||||
|
}
|
||||||
|
} else if (props.editingEnvironmentIndex !== null) {
|
||||||
return getEnviroment(props.editingEnvironmentIndex)
|
return getEnviroment(props.editingEnvironmentIndex)
|
||||||
|
} else {
|
||||||
|
return null
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
|
||||||
return {
|
return {
|
||||||
globalVars,
|
globalVars,
|
||||||
workingEnv,
|
workingEnv,
|
||||||
|
envList: useReadonlyStream(environments$, []) || props.envVars(),
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
data() {
|
data() {
|
||||||
@@ -219,6 +236,11 @@ export default defineComponent({
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (this.action === "new") {
|
||||||
|
createEnvironment(this.name)
|
||||||
|
setCurrentEnvironment(this.envList.length - 1)
|
||||||
|
}
|
||||||
|
|
||||||
const environmentUpdated: Environment = {
|
const environmentUpdated: Environment = {
|
||||||
name: this.name,
|
name: this.name,
|
||||||
variables: this.vars,
|
variables: this.vars,
|
||||||
@@ -226,7 +248,11 @@ export default defineComponent({
|
|||||||
|
|
||||||
if (this.editingEnvironmentIndex === "Global")
|
if (this.editingEnvironmentIndex === "Global")
|
||||||
setGlobalEnvVariables(environmentUpdated.variables)
|
setGlobalEnvVariables(environmentUpdated.variables)
|
||||||
else updateEnvironment(this.editingEnvironmentIndex!, environmentUpdated)
|
else if (this.action === "new") {
|
||||||
|
updateEnvironment(this.envList.length - 1, environmentUpdated)
|
||||||
|
} else {
|
||||||
|
updateEnvironment(this.editingEnvironmentIndex!, environmentUpdated)
|
||||||
|
}
|
||||||
this.hideModal()
|
this.hideModal()
|
||||||
},
|
},
|
||||||
hideModal() {
|
hideModal() {
|
||||||
@@ -107,12 +107,9 @@
|
|||||||
@click.native="displayModalAdd(true)"
|
@click.native="displayModalAdd(true)"
|
||||||
/>
|
/>
|
||||||
</div>
|
</div>
|
||||||
<EnvironmentsAdd
|
<EnvironmentsDetails
|
||||||
:show="showModalAdd"
|
:show="showModalDetails"
|
||||||
@hide-modal="displayModalAdd(false)"
|
:action="action"
|
||||||
/>
|
|
||||||
<EnvironmentsEdit
|
|
||||||
:show="showModalEdit"
|
|
||||||
:editing-environment-index="editingEnvironmentIndex"
|
:editing-environment-index="editingEnvironmentIndex"
|
||||||
@hide-modal="displayModalEdit(false)"
|
@hide-modal="displayModalEdit(false)"
|
||||||
/>
|
/>
|
||||||
@@ -155,17 +152,19 @@ export default defineComponent({
|
|||||||
data() {
|
data() {
|
||||||
return {
|
return {
|
||||||
showModalImportExport: false,
|
showModalImportExport: false,
|
||||||
showModalAdd: false,
|
showModalDetails: false,
|
||||||
showModalEdit: false,
|
action: "edit" as "new" | "edit",
|
||||||
editingEnvironmentIndex: undefined as number | "Global" | undefined,
|
editingEnvironmentIndex: undefined as number | "Global" | undefined,
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
methods: {
|
methods: {
|
||||||
displayModalAdd(shouldDisplay: boolean) {
|
displayModalAdd(shouldDisplay: boolean) {
|
||||||
this.showModalAdd = shouldDisplay
|
this.action = "new"
|
||||||
|
this.showModalDetails = shouldDisplay
|
||||||
},
|
},
|
||||||
displayModalEdit(shouldDisplay: boolean) {
|
displayModalEdit(shouldDisplay: boolean) {
|
||||||
this.showModalEdit = shouldDisplay
|
this.action = "edit"
|
||||||
|
this.showModalDetails = shouldDisplay
|
||||||
|
|
||||||
if (!shouldDisplay) this.resetSelectedData()
|
if (!shouldDisplay) this.resetSelectedData()
|
||||||
},
|
},
|
||||||
@@ -174,6 +173,7 @@ export default defineComponent({
|
|||||||
},
|
},
|
||||||
editEnvironment(environmentIndex: number | "Global") {
|
editEnvironment(environmentIndex: number | "Global") {
|
||||||
this.$data.editingEnvironmentIndex = environmentIndex
|
this.$data.editingEnvironmentIndex = environmentIndex
|
||||||
|
this.action = "edit"
|
||||||
this.displayModalEdit(true)
|
this.displayModalEdit(true)
|
||||||
},
|
},
|
||||||
resetSelectedData() {
|
resetSelectedData() {
|
||||||
|
|||||||
@@ -182,10 +182,11 @@
|
|||||||
class="my-4"
|
class="my-4"
|
||||||
/>
|
/>
|
||||||
</div>
|
</div>
|
||||||
<EnvironmentsAdd
|
<EnvironmentsDetails
|
||||||
:show="showModalAdd"
|
:show="showModalDetails"
|
||||||
|
action="new"
|
||||||
|
:env-vars="getAdditionVars"
|
||||||
@hide-modal="displayModalAdd(false)"
|
@hide-modal="displayModalAdd(false)"
|
||||||
@environment-added="createNewEnv($event)"
|
|
||||||
/>
|
/>
|
||||||
</div>
|
</div>
|
||||||
</template>
|
</template>
|
||||||
@@ -203,17 +204,16 @@ import {
|
|||||||
selectedEnvIndex$,
|
selectedEnvIndex$,
|
||||||
setCurrentEnvironment,
|
setCurrentEnvironment,
|
||||||
setGlobalEnvVariables,
|
setGlobalEnvVariables,
|
||||||
updateEnvironment,
|
|
||||||
} from "~/newstore/environments"
|
} from "~/newstore/environments"
|
||||||
import { restTestResults$, setRESTTestResults } from "~/newstore/RESTSession"
|
import { restTestResults$, setRESTTestResults } from "~/newstore/RESTSession"
|
||||||
import { HoppTestResult } from "~/helpers/types/HoppTestResult"
|
import { HoppTestResult } from "~/helpers/types/HoppTestResult"
|
||||||
|
|
||||||
const t = useI18n()
|
const t = useI18n()
|
||||||
|
|
||||||
const showModalAdd = ref(false)
|
const showModalDetails = ref(false)
|
||||||
|
|
||||||
const displayModalAdd = (shouldDisplay: boolean) => {
|
const displayModalAdd = (shouldDisplay: boolean) => {
|
||||||
showModalAdd.value = shouldDisplay
|
showModalDetails.value = shouldDisplay
|
||||||
}
|
}
|
||||||
|
|
||||||
const testResults = useReadonlyStream(
|
const testResults = useReadonlyStream(
|
||||||
@@ -221,6 +221,15 @@ const testResults = useReadonlyStream(
|
|||||||
null
|
null
|
||||||
) as Ref<HoppTestResult | null>
|
) as Ref<HoppTestResult | null>
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get the "addition" environment variables
|
||||||
|
* @returns Array of objects with key-value pairs of arguments
|
||||||
|
*/
|
||||||
|
const getAdditionVars = () =>
|
||||||
|
testResults?.value?.envDiff?.selected?.additions
|
||||||
|
? testResults.value.envDiff.selected.additions
|
||||||
|
: []
|
||||||
|
|
||||||
const clearContent = () => setRESTTestResults(null)
|
const clearContent = () => setRESTTestResults(null)
|
||||||
|
|
||||||
const haveEnvVariables = computed(() => {
|
const haveEnvVariables = computed(() => {
|
||||||
@@ -266,13 +275,4 @@ const addEnvToGlobal = () => {
|
|||||||
...testResults.value.envDiff.selected.additions,
|
...testResults.value.envDiff.selected.additions,
|
||||||
])
|
])
|
||||||
}
|
}
|
||||||
|
|
||||||
const createNewEnv = ({ name, index }: { name: string; index: number }) => {
|
|
||||||
if (!testResults.value?.envDiff.selected.additions) return
|
|
||||||
updateEnvironment(index, {
|
|
||||||
name,
|
|
||||||
variables: testResults.value.envDiff.selected.additions,
|
|
||||||
})
|
|
||||||
setCurrentEnvironment(index)
|
|
||||||
}
|
|
||||||
</script>
|
</script>
|
||||||
|
|||||||
Reference in New Issue
Block a user