Edit.vue 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. <template>
  2. <SmartModal v-if="show" :title="$t('environment.edit')" @close="hideModal">
  3. <template #body>
  4. <div class="flex flex-col px-2">
  5. <div class="flex relative">
  6. <input
  7. id="selectLabelEnvEdit"
  8. v-model="name"
  9. v-focus
  10. class="input floating-input"
  11. placeholder=" "
  12. type="text"
  13. autocomplete="off"
  14. :disabled="editingEnvironmentIndex === 'Global'"
  15. @keyup.enter="saveEnvironment"
  16. />
  17. <label for="selectLabelEnvEdit">
  18. {{ $t("action.label") }}
  19. </label>
  20. </div>
  21. <div class="flex flex-1 justify-between items-center">
  22. <label for="variableList" class="p-4">
  23. {{ $t("environment.variable_list") }}
  24. </label>
  25. <div class="flex">
  26. <ButtonSecondary
  27. v-tippy="{ theme: 'tooltip' }"
  28. :title="$t('action.clear_all')"
  29. :svg="clearIcon"
  30. class="rounded"
  31. @click.native="clearContent()"
  32. />
  33. <ButtonSecondary
  34. v-tippy="{ theme: 'tooltip' }"
  35. svg="plus"
  36. :title="$t('add.new')"
  37. class="rounded"
  38. @click.native="addEnvironmentVariable"
  39. />
  40. </div>
  41. </div>
  42. <div class="divide-y divide-dividerLight border-divider border rounded">
  43. <div
  44. v-for="(variable, index) in vars"
  45. :key="`variable-${index}`"
  46. class="divide-x divide-dividerLight flex"
  47. >
  48. <input
  49. v-model="variable.key"
  50. class="bg-transparent flex flex-1 py-2 px-4"
  51. :placeholder="$t('count.variable', { count: index + 1 })"
  52. :name="'param' + index"
  53. />
  54. <input
  55. v-model="variable.value"
  56. class="bg-transparent flex flex-1 py-2 px-4"
  57. :placeholder="$t('count.value', { count: index + 1 })"
  58. :name="'value' + index"
  59. />
  60. <div class="flex">
  61. <ButtonSecondary
  62. id="variable"
  63. v-tippy="{ theme: 'tooltip' }"
  64. :title="$t('action.remove')"
  65. svg="trash"
  66. color="red"
  67. @click.native="removeEnvironmentVariable(index)"
  68. />
  69. </div>
  70. </div>
  71. <div
  72. v-if="vars.length === 0"
  73. class="
  74. flex flex-col
  75. text-secondaryLight
  76. p-4
  77. items-center
  78. justify-center
  79. "
  80. >
  81. <SmartIcon class="opacity-75 pb-2" name="layers" />
  82. <span class="text-center pb-4">
  83. {{ $t("empty.environments") }}
  84. </span>
  85. <ButtonSecondary
  86. :label="$t('add.new')"
  87. filled
  88. @click.native="addEnvironmentVariable"
  89. />
  90. </div>
  91. </div>
  92. </div>
  93. </template>
  94. <template #footer>
  95. <span>
  96. <ButtonPrimary
  97. :label="$t('action.save')"
  98. @click.native="saveEnvironment"
  99. />
  100. <ButtonSecondary
  101. :label="$t('action.cancel')"
  102. @click.native="hideModal"
  103. />
  104. </span>
  105. </template>
  106. </SmartModal>
  107. </template>
  108. <script lang="ts">
  109. import clone from "lodash/clone"
  110. import { computed, defineComponent, PropType } from "@nuxtjs/composition-api"
  111. import {
  112. Environment,
  113. getEnviroment,
  114. getGlobalVariables,
  115. setGlobalEnvVariables,
  116. updateEnvironment,
  117. } from "~/newstore/environments"
  118. export default defineComponent({
  119. props: {
  120. show: Boolean,
  121. editingEnvironmentIndex: {
  122. type: [Number, String] as PropType<number | "Global" | null>,
  123. default: null,
  124. },
  125. },
  126. setup(props) {
  127. const workingEnv = computed(() => {
  128. if (props.editingEnvironmentIndex === null) return null
  129. if (props.editingEnvironmentIndex === "Global") {
  130. return {
  131. name: "Global",
  132. variables: getGlobalVariables(),
  133. } as Environment
  134. } else {
  135. return getEnviroment(props.editingEnvironmentIndex)
  136. }
  137. })
  138. return {
  139. workingEnv,
  140. }
  141. },
  142. data() {
  143. return {
  144. name: null as string | null,
  145. vars: [] as { key: string; value: string }[],
  146. clearIcon: "trash-2",
  147. }
  148. },
  149. watch: {
  150. show() {
  151. this.name = this.workingEnv?.name ?? null
  152. this.vars = clone(this.workingEnv?.variables ?? [])
  153. },
  154. },
  155. methods: {
  156. clearContent() {
  157. this.vars = []
  158. this.clearIcon = "check"
  159. this.$toast.success(this.$t("state.cleared").toString(), {
  160. icon: "clear_all",
  161. })
  162. setTimeout(() => (this.clearIcon = "trash-2"), 1000)
  163. },
  164. addEnvironmentVariable() {
  165. this.vars.push({
  166. key: "",
  167. value: "",
  168. })
  169. },
  170. removeEnvironmentVariable(index: number) {
  171. this.vars.splice(index, 1)
  172. },
  173. saveEnvironment() {
  174. if (!this.name) {
  175. this.$toast.error(this.$t("environment.invalid_name").toString(), {
  176. icon: "error_outline",
  177. })
  178. return
  179. }
  180. const environmentUpdated: Environment = {
  181. name: this.name,
  182. variables: this.vars,
  183. }
  184. if (this.editingEnvironmentIndex === "Global")
  185. setGlobalEnvVariables(environmentUpdated.variables)
  186. else updateEnvironment(this.editingEnvironmentIndex!, environmentUpdated)
  187. this.hideModal()
  188. },
  189. hideModal() {
  190. this.name = null
  191. this.$emit("hide-modal")
  192. },
  193. },
  194. })
  195. </script>