Edit.vue 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  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 justify-between items-center">
  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-y divide-dividerLight border-divider border rounded">
  47. <div
  48. v-for="(variable, index) in vars"
  49. :key="`variable-${index}`"
  50. class="divide-x divide-dividerLight flex"
  51. >
  52. <input
  53. v-model="variable.key"
  54. class="bg-transparent flex flex-1 py-2 px-4"
  55. :placeholder="`${$t('count.variable', { count: index + 1 })}`"
  56. :name="'param' + index"
  57. />
  58. <input
  59. v-model="variable.value"
  60. class="bg-transparent flex flex-1 py-2 px-4"
  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="
  78. flex flex-col
  79. text-secondaryLight
  80. p-4
  81. items-center
  82. justify-center
  83. "
  84. >
  85. <img
  86. :src="`/images/states/${$colorMode.value}/blockchain.svg`"
  87. loading="lazy"
  88. class="
  89. flex-col
  90. my-4
  91. object-contain object-center
  92. h-16
  93. w-16
  94. inline-flex
  95. "
  96. :alt="$t('empty.environments')"
  97. />
  98. <span class="text-center pb-4">
  99. {{ $t("empty.environments") }}
  100. </span>
  101. <ButtonSecondary
  102. :label="`${$t('add.new')}`"
  103. filled
  104. class="mb-4"
  105. @click.native="addEnvironmentVariable"
  106. />
  107. </div>
  108. </div>
  109. </div>
  110. </template>
  111. <template #footer>
  112. <span>
  113. <ButtonPrimary
  114. :label="`${$t('action.save')}`"
  115. @click.native="saveEnvironment"
  116. />
  117. <ButtonSecondary
  118. :label="`${$t('action.cancel')}`"
  119. @click.native="hideModal"
  120. />
  121. </span>
  122. </template>
  123. </SmartModal>
  124. </template>
  125. <script lang="ts">
  126. import clone from "lodash/clone"
  127. import { computed, defineComponent, PropType } from "@nuxtjs/composition-api"
  128. import {
  129. Environment,
  130. getEnviroment,
  131. getGlobalVariables,
  132. setGlobalEnvVariables,
  133. updateEnvironment,
  134. } from "~/newstore/environments"
  135. export default defineComponent({
  136. props: {
  137. show: Boolean,
  138. editingEnvironmentIndex: {
  139. type: [Number, String] as PropType<number | "Global" | null>,
  140. default: null,
  141. },
  142. },
  143. setup(props) {
  144. const workingEnv = computed(() => {
  145. if (props.editingEnvironmentIndex === null) return null
  146. if (props.editingEnvironmentIndex === "Global") {
  147. return {
  148. name: "Global",
  149. variables: getGlobalVariables(),
  150. } as Environment
  151. } else {
  152. return getEnviroment(props.editingEnvironmentIndex)
  153. }
  154. })
  155. return {
  156. workingEnv,
  157. }
  158. },
  159. data() {
  160. return {
  161. name: null as string | null,
  162. vars: [] as { key: string; value: string }[],
  163. clearIcon: "trash-2",
  164. }
  165. },
  166. watch: {
  167. show() {
  168. this.name = this.workingEnv?.name ?? null
  169. this.vars = clone(this.workingEnv?.variables ?? [])
  170. },
  171. },
  172. methods: {
  173. clearContent() {
  174. this.vars = []
  175. this.clearIcon = "check"
  176. this.$toast.success(`${this.$t("state.cleared")}`, {
  177. icon: "clear_all",
  178. })
  179. setTimeout(() => (this.clearIcon = "trash-2"), 1000)
  180. },
  181. addEnvironmentVariable() {
  182. this.vars.push({
  183. key: "",
  184. value: "",
  185. })
  186. },
  187. removeEnvironmentVariable(index: number) {
  188. this.vars.splice(index, 1)
  189. },
  190. saveEnvironment() {
  191. if (!this.name) {
  192. this.$toast.error(`${this.$t("environment.invalid_name")}`, {
  193. icon: "error_outline",
  194. })
  195. return
  196. }
  197. const environmentUpdated: Environment = {
  198. name: this.name,
  199. variables: this.vars,
  200. }
  201. if (this.editingEnvironmentIndex === "Global")
  202. setGlobalEnvVariables(environmentUpdated.variables)
  203. else updateEnvironment(this.editingEnvironmentIndex!, environmentUpdated)
  204. this.hideModal()
  205. },
  206. hideModal() {
  207. this.name = null
  208. this.$emit("hide-modal")
  209. },
  210. },
  211. })
  212. </script>