edit-environment.vue 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. <template>
  2. <modal v-if="show" @close="hideModal">
  3. <div slot="header">
  4. <div class="row-wrapper">
  5. <h3 class="title">{{ $t("edit_environment") }}</h3>
  6. <div>
  7. <button class="icon" @click="hideModal">
  8. <i class="material-icons">close</i>
  9. </button>
  10. </div>
  11. </div>
  12. </div>
  13. <div slot="body" class="flex flex-col">
  14. <label for="selectLabel">{{ $t("label") }}</label>
  15. <input
  16. type="text"
  17. id="selectLabel"
  18. v-model="name"
  19. :placeholder="editingEnvironment.name"
  20. @keyup.enter="saveEnvironment"
  21. />
  22. <div class="row-wrapper">
  23. <label for="variableList">{{ $t("env_variable_list") }}</label>
  24. <div>
  25. <button class="icon" @click="clearContent($event)" v-tooltip.bottom="$t('clear')">
  26. <i class="material-icons">clear_all</i>
  27. </button>
  28. </div>
  29. </div>
  30. <ul
  31. v-for="(variable, index) in this.editingEnvCopy.variables"
  32. :key="index"
  33. class="border-b border-dashed divide-y md:divide-x border-brdColor divide-dashed divide-brdColor md:divide-y-0"
  34. :class="{ 'border-t': index == 0 }"
  35. >
  36. <li>
  37. <input
  38. :placeholder="$t('parameter_count', { count: index + 1 })"
  39. :name="'param' + index"
  40. :value="variable.key"
  41. @change="
  42. $store.commit('postwoman/setVariableKey', {
  43. index,
  44. value: $event.target.value,
  45. })
  46. "
  47. autofocus
  48. />
  49. </li>
  50. <li>
  51. <input
  52. :placeholder="$t('value_count', { count: index + 1 })"
  53. :name="'value' + index"
  54. :value="
  55. typeof variable.value === 'string' ? variable.value : JSON.stringify(variable.value)
  56. "
  57. @change="
  58. $store.commit('postwoman/setVariableValue', {
  59. index,
  60. value: $event.target.value,
  61. })
  62. "
  63. />
  64. </li>
  65. <div>
  66. <li>
  67. <button
  68. class="icon"
  69. @click="removeEnvironmentVariable(index)"
  70. v-tooltip.bottom="$t('delete')"
  71. id="variable"
  72. >
  73. <i class="material-icons">delete</i>
  74. </button>
  75. </li>
  76. </div>
  77. </ul>
  78. <ul>
  79. <li>
  80. <button class="icon" @click="addEnvironmentVariable">
  81. <i class="material-icons">add</i>
  82. <span>{{ $t("add_new") }}</span>
  83. </button>
  84. </li>
  85. </ul>
  86. </div>
  87. <div slot="footer">
  88. <div class="row-wrapper">
  89. <span></span>
  90. <span>
  91. <button class="icon" @click="hideModal">
  92. {{ $t("cancel") }}
  93. </button>
  94. <button class="icon primary" @click="saveEnvironment">
  95. {{ $t("save") }}
  96. </button>
  97. </span>
  98. </div>
  99. </div>
  100. </modal>
  101. </template>
  102. <script>
  103. import { fb } from "~/helpers/fb"
  104. export default {
  105. props: {
  106. show: Boolean,
  107. editingEnvironment: Object,
  108. editingEnvironmentIndex: Number,
  109. },
  110. data() {
  111. return {
  112. name: undefined,
  113. doneButton: '<i class="material-icons">done</i>',
  114. }
  115. },
  116. watch: {
  117. editingEnvironment(update) {
  118. this.name =
  119. this.$props.editingEnvironment && this.$props.editingEnvironment.name
  120. ? this.$props.editingEnvironment.name
  121. : undefined
  122. this.$store.commit("postwoman/setEditingEnvironment", this.$props.editingEnvironment)
  123. },
  124. },
  125. computed: {
  126. editingEnvCopy() {
  127. return this.$store.state.postwoman.editingEnvironment
  128. },
  129. variableString() {
  130. const result = this.editingEnvCopy.variables
  131. return result === "" ? "" : JSON.stringify(result)
  132. },
  133. },
  134. methods: {
  135. syncEnvironments() {
  136. if (fb.currentUser !== null && fb.currentSettings[1]) {
  137. if (fb.currentSettings[1].value) {
  138. fb.writeEnvironments(JSON.parse(JSON.stringify(this.$store.state.postwoman.environments)))
  139. }
  140. }
  141. },
  142. clearContent({ target }) {
  143. this.$store.commit("postwoman/removeVariables", [])
  144. target.innerHTML = this.doneButton
  145. this.$toast.info(this.$t("cleared"), {
  146. icon: "clear_all",
  147. })
  148. setTimeout(() => (target.innerHTML = '<i class="material-icons">clear_all</i>'), 1000)
  149. },
  150. addEnvironmentVariable() {
  151. let value = { key: "", value: "" }
  152. this.$store.commit("postwoman/addVariable", value)
  153. this.syncEnvironments()
  154. },
  155. removeEnvironmentVariable(index) {
  156. let variableIndex = index
  157. const oldVariables = this.editingEnvCopy.variables.slice()
  158. const newVariables = this.editingEnvCopy.variables.filter(
  159. (variable, index) => variableIndex !== index
  160. )
  161. this.$store.commit("postwoman/removeVariable", newVariables)
  162. this.$toast.error(this.$t("deleted"), {
  163. icon: "delete",
  164. action: {
  165. text: this.$t("undo"),
  166. onClick: (e, toastObject) => {
  167. this.$store.commit("postwoman/removeVariable", oldVariables)
  168. toastObject.remove()
  169. },
  170. },
  171. })
  172. this.syncEnvironments()
  173. },
  174. saveEnvironment() {
  175. if (!this.$data.name) {
  176. this.$toast.info(this.$t("invalid_environment_name"))
  177. return
  178. }
  179. const environmentUpdated = {
  180. ...this.editingEnvCopy,
  181. name: this.$data.name,
  182. }
  183. this.$store.commit("postwoman/saveEnvironment", {
  184. environment: environmentUpdated,
  185. environmentIndex: this.$props.editingEnvironmentIndex,
  186. })
  187. this.$emit("hide-modal")
  188. this.syncEnvironments()
  189. },
  190. hideModal() {
  191. this.$emit("hide-modal")
  192. this.$data.name = undefined
  193. },
  194. },
  195. }
  196. </script>