Edit.vue 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  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. mb-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. @click.native="addEnvironmentVariable"
  105. />
  106. </div>
  107. </div>
  108. </div>
  109. </template>
  110. <template #footer>
  111. <span>
  112. <ButtonPrimary
  113. :label="`${$t('action.save')}`"
  114. @click.native="saveEnvironment"
  115. />
  116. <ButtonSecondary
  117. :label="`${$t('action.cancel')}`"
  118. @click.native="hideModal"
  119. />
  120. </span>
  121. </template>
  122. </SmartModal>
  123. </template>
  124. <script lang="ts">
  125. import clone from "lodash/clone"
  126. import { computed, defineComponent, PropType } from "@nuxtjs/composition-api"
  127. import {
  128. Environment,
  129. getEnviroment,
  130. getGlobalVariables,
  131. setGlobalEnvVariables,
  132. updateEnvironment,
  133. } from "~/newstore/environments"
  134. export default defineComponent({
  135. props: {
  136. show: Boolean,
  137. editingEnvironmentIndex: {
  138. type: [Number, String] as PropType<number | "Global" | null>,
  139. default: null,
  140. },
  141. },
  142. setup(props) {
  143. const workingEnv = computed(() => {
  144. if (props.editingEnvironmentIndex === null) return null
  145. if (props.editingEnvironmentIndex === "Global") {
  146. return {
  147. name: "Global",
  148. variables: getGlobalVariables(),
  149. } as Environment
  150. } else {
  151. return getEnviroment(props.editingEnvironmentIndex)
  152. }
  153. })
  154. return {
  155. workingEnv,
  156. }
  157. },
  158. data() {
  159. return {
  160. name: null as string | null,
  161. vars: [] as { key: string; value: string }[],
  162. clearIcon: "trash-2",
  163. }
  164. },
  165. watch: {
  166. show() {
  167. this.name = this.workingEnv?.name ?? null
  168. this.vars = clone(this.workingEnv?.variables ?? [])
  169. },
  170. },
  171. methods: {
  172. clearContent() {
  173. this.vars = []
  174. this.clearIcon = "check"
  175. this.$toast.success(`${this.$t("state.cleared")}`, {
  176. icon: "clear_all",
  177. })
  178. setTimeout(() => (this.clearIcon = "trash-2"), 1000)
  179. },
  180. addEnvironmentVariable() {
  181. this.vars.push({
  182. key: "",
  183. value: "",
  184. })
  185. },
  186. removeEnvironmentVariable(index: number) {
  187. this.vars.splice(index, 1)
  188. },
  189. saveEnvironment() {
  190. if (!this.name) {
  191. this.$toast.error(`${this.$t("environment.invalid_name")}`, {
  192. icon: "error_outline",
  193. })
  194. return
  195. }
  196. const environmentUpdated: Environment = {
  197. name: this.name,
  198. variables: this.vars,
  199. }
  200. if (this.editingEnvironmentIndex === "Global")
  201. setGlobalEnvVariables(environmentUpdated.variables)
  202. else updateEnvironment(this.editingEnvironmentIndex!, environmentUpdated)
  203. this.hideModal()
  204. },
  205. hideModal() {
  206. this.name = null
  207. this.$emit("hide-modal")
  208. },
  209. },
  210. })
  211. </script>