Edit.vue 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  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="relative flex">
  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 items-center justify-between flex-1">
  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. class="rounded"
  35. @click.native="clearContent()"
  36. />
  37. <ButtonSecondary
  38. v-tippy="{ theme: 'tooltip' }"
  39. svg="plus"
  40. :title="$t('add.new')"
  41. class="rounded"
  42. @click.native="addEnvironmentVariable"
  43. />
  44. </div>
  45. </div>
  46. <div class="divide-dividerLight border-divider border divide-y rounded">
  47. <div
  48. v-for="(variable, index) in vars"
  49. :key="`variable-${index}`"
  50. class="divide-dividerLight flex divide-x"
  51. >
  52. <input
  53. v-model="variable.key"
  54. class="flex flex-1 px-4 py-2 bg-transparent"
  55. :placeholder="`${$t('count.variable', { count: index + 1 })}`"
  56. :name="'param' + index"
  57. />
  58. <input
  59. v-model="variable.value"
  60. class="flex flex-1 px-4 py-2 bg-transparent"
  61. :placeholder="`${$t('count.value', { count: index + 1 })}`"
  62. :name="'value' + index"
  63. />
  64. <div class="flex">
  65. <ButtonSecondary
  66. id="variable"
  67. v-tippy="{ theme: 'tooltip' }"
  68. :title="$t('action.remove')"
  69. svg="trash"
  70. color="red"
  71. @click.native="removeEnvironmentVariable(index)"
  72. />
  73. </div>
  74. </div>
  75. <div
  76. v-if="vars.length === 0"
  77. class="text-secondaryLight flex flex-col items-center justify-center p-4"
  78. >
  79. <img
  80. :src="`/images/states/${$colorMode.value}/blockchain.svg`"
  81. loading="lazy"
  82. class="inline-flex flex-col object-contain object-center w-16 h-16 my-4"
  83. :alt="$t('empty.environments')"
  84. />
  85. <span class="pb-4 text-center">
  86. {{ $t("empty.environments") }}
  87. </span>
  88. <ButtonSecondary
  89. :label="`${$t('add.new')}`"
  90. filled
  91. class="mb-4"
  92. @click.native="addEnvironmentVariable"
  93. />
  94. </div>
  95. </div>
  96. </div>
  97. </template>
  98. <template #footer>
  99. <span>
  100. <ButtonPrimary
  101. :label="`${$t('action.save')}`"
  102. @click.native="saveEnvironment"
  103. />
  104. <ButtonSecondary
  105. :label="`${$t('action.cancel')}`"
  106. @click.native="hideModal"
  107. />
  108. </span>
  109. </template>
  110. </SmartModal>
  111. </template>
  112. <script lang="ts">
  113. import clone from "lodash/clone"
  114. import { computed, defineComponent, PropType } from "@nuxtjs/composition-api"
  115. import {
  116. Environment,
  117. getEnviroment,
  118. getGlobalVariables,
  119. setGlobalEnvVariables,
  120. updateEnvironment,
  121. } from "~/newstore/environments"
  122. export default defineComponent({
  123. props: {
  124. show: Boolean,
  125. editingEnvironmentIndex: {
  126. type: [Number, String] as PropType<number | "Global" | null>,
  127. default: null,
  128. },
  129. },
  130. setup(props) {
  131. const workingEnv = computed(() => {
  132. if (props.editingEnvironmentIndex === null) return null
  133. if (props.editingEnvironmentIndex === "Global") {
  134. return {
  135. name: "Global",
  136. variables: getGlobalVariables(),
  137. } as Environment
  138. } else {
  139. return getEnviroment(props.editingEnvironmentIndex)
  140. }
  141. })
  142. return {
  143. workingEnv,
  144. }
  145. },
  146. data() {
  147. return {
  148. name: null as string | null,
  149. vars: [] as { key: string; value: string }[],
  150. clearIcon: "trash-2",
  151. }
  152. },
  153. watch: {
  154. show() {
  155. this.name = this.workingEnv?.name ?? null
  156. this.vars = clone(this.workingEnv?.variables ?? [])
  157. },
  158. },
  159. methods: {
  160. clearContent() {
  161. this.vars = []
  162. this.clearIcon = "check"
  163. this.$toast.success(`${this.$t("state.cleared")}`)
  164. setTimeout(() => (this.clearIcon = "trash-2"), 1000)
  165. },
  166. addEnvironmentVariable() {
  167. this.vars.push({
  168. key: "",
  169. value: "",
  170. })
  171. },
  172. removeEnvironmentVariable(index: number) {
  173. this.vars.splice(index, 1)
  174. },
  175. saveEnvironment() {
  176. if (!this.name) {
  177. this.$toast.error(`${this.$t("environment.invalid_name")}`)
  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>