BulkActions.vue 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. <template>
  2. <div>
  3. <modal title="Bulk Actions" class="bulk-actions-modal">
  4. <template #body>
  5. <label class="label">Method</label>
  6. <div class="control is-expanded select">
  7. <select v-model="method">
  8. <option value="add">Add</option>
  9. <option value="remove">Remove</option>
  10. <option value="replace">Replace</option>
  11. </select>
  12. </div>
  13. <label class="label">{{ type.name.slice(0, -1) }}</label>
  14. <div class="control is-grouped input-with-button">
  15. <auto-suggest
  16. v-model="itemInput"
  17. :placeholder="`Enter ${type.name} to ${method}`"
  18. :all-items="allItems"
  19. @submitted="addItem()"
  20. />
  21. <p class="control">
  22. <button
  23. class="button is-primary material-icons"
  24. @click="addItem()"
  25. >
  26. add
  27. </button>
  28. </p>
  29. </div>
  30. <label class="label"
  31. >{{ type.name }} to be
  32. {{ method === "add" ? `added` : `${method}d` }}</label
  33. >
  34. <div v-if="items.length > 0">
  35. <div
  36. v-for="(item, index) in items"
  37. :key="`item-${item}`"
  38. class="pill"
  39. >
  40. {{ item }}
  41. <span
  42. class="material-icons remove-item"
  43. @click="removeItem(index)"
  44. content="Remove item"
  45. v-tippy
  46. >highlight_off</span
  47. >
  48. </div>
  49. </div>
  50. <p v-else>No {{ type.name }} specified</p>
  51. </template>
  52. <template #footer>
  53. <button
  54. class="button is-primary"
  55. :disabled="items.length === 0"
  56. @click="applyChanges()"
  57. >
  58. Apply Changes
  59. </button>
  60. </template>
  61. </modal>
  62. </div>
  63. </template>
  64. <script>
  65. import { mapGetters, mapActions } from "vuex";
  66. import Toast from "toasters";
  67. import AutoSuggest from "@/components/AutoSuggest.vue";
  68. import ws from "@/ws";
  69. import { mapModalState } from "@/vuex_helpers";
  70. export default {
  71. components: { AutoSuggest },
  72. props: {
  73. modalUuid: { type: String, default: "" }
  74. },
  75. data() {
  76. return {
  77. method: "add",
  78. items: [],
  79. itemInput: null,
  80. allItems: []
  81. };
  82. },
  83. computed: {
  84. ...mapModalState("modals/bulkActions/MODAL_UUID", {
  85. type: state => state.type
  86. }),
  87. ...mapGetters({
  88. socket: "websockets/getSocket"
  89. })
  90. },
  91. beforeUnmount() {
  92. this.itemInput = null;
  93. this.items = [];
  94. // Delete the VueX module that was created for this modal, after all other cleanup tasks are performed
  95. this.$store.unregisterModule(["modals", "bulkActions", this.modalUuid]);
  96. },
  97. mounted() {
  98. ws.onConnect(this.init);
  99. },
  100. methods: {
  101. init() {
  102. if (this.type.autosuggest && this.type.autosuggestDataAction)
  103. this.socket.dispatch(this.type.autosuggestDataAction, res => {
  104. if (res.status === "success") {
  105. const { items } = res.data;
  106. this.allItems = items;
  107. } else {
  108. new Toast(res.message);
  109. }
  110. });
  111. },
  112. addItem() {
  113. if (!this.itemInput) return;
  114. if (this.type.regex && !this.type.regex.test(this.itemInput)) {
  115. new Toast(`Invalid ${this.type.name} format.`);
  116. } else if (this.items.includes(this.itemInput)) {
  117. new Toast(`Duplicate ${this.type.name} specified.`);
  118. } else {
  119. this.items.push(this.itemInput);
  120. this.itemInput = null;
  121. }
  122. },
  123. removeItem(index) {
  124. this.items.splice(index, 1);
  125. },
  126. applyChanges() {
  127. let toast;
  128. this.socket.dispatch(
  129. this.type.action,
  130. this.method,
  131. this.items,
  132. this.type.items,
  133. {
  134. cb: () => {
  135. // new Toast(res.message);
  136. // if (res.status === "success")
  137. // this.closeModal("bulkActions");
  138. },
  139. onProgress: res => {
  140. if (!toast) {
  141. toast = new Toast({
  142. content: res.message,
  143. persistent: true,
  144. interactable: false
  145. });
  146. } else {
  147. toast.content = res.message;
  148. }
  149. if (
  150. res.status === "success" ||
  151. res.status === "error"
  152. ) {
  153. setTimeout(() => {
  154. toast.destroy();
  155. }, 4000);
  156. }
  157. }
  158. }
  159. );
  160. },
  161. ...mapActions("modalVisibility", ["closeModal"])
  162. }
  163. };
  164. </script>
  165. <style lang="less" scoped>
  166. .label {
  167. text-transform: capitalize;
  168. }
  169. .control.input-with-button > div {
  170. flex: 1;
  171. }
  172. .pill {
  173. display: inline-flex;
  174. .remove-item {
  175. font-size: 16px;
  176. margin: auto 2px auto 5px;
  177. cursor: pointer;
  178. }
  179. }
  180. :deep(.autosuggest-container) {
  181. width: calc(100% - 40px);
  182. top: unset;
  183. }
  184. </style>