Edit.vue 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  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. <SmartIcon class="opacity-75 pb-2" name="layers" />
  86. <span class="text-center pb-4">
  87. {{ $t("empty.environments") }}
  88. </span>
  89. <ButtonSecondary
  90. :label="`${$t('add.new')}`"
  91. filled
  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. icon: "clear_all",
  165. })
  166. setTimeout(() => (this.clearIcon = "trash-2"), 1000)
  167. },
  168. addEnvironmentVariable() {
  169. this.vars.push({
  170. key: "",
  171. value: "",
  172. })
  173. },
  174. removeEnvironmentVariable(index: number) {
  175. this.vars.splice(index, 1)
  176. },
  177. saveEnvironment() {
  178. if (!this.name) {
  179. this.$toast.error(`${this.$t("environment.invalid_name")}`, {
  180. icon: "error_outline",
  181. })
  182. return
  183. }
  184. const environmentUpdated: Environment = {
  185. name: this.name,
  186. variables: this.vars,
  187. }
  188. if (this.editingEnvironmentIndex === "Global")
  189. setGlobalEnvVariables(environmentUpdated.variables)
  190. else updateEnvironment(this.editingEnvironmentIndex!, environmentUpdated)
  191. this.hideModal()
  192. },
  193. hideModal() {
  194. this.name = null
  195. this.$emit("hide-modal")
  196. },
  197. },
  198. })
  199. </script>