Edit.vue 5.5 KB

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