editRequest.vue 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. <template>
  2. <modal v-if="show" @close="hideModal">
  3. <div slot="header">
  4. <ul>
  5. <li>
  6. <div class="flex-wrap">
  7. <h3 class="title">{{ $t("edit_request") }}</h3>
  8. <div>
  9. <button class="icon" @click="hideModal">
  10. <i class="material-icons">close</i>
  11. </button>
  12. </div>
  13. </div>
  14. </li>
  15. </ul>
  16. </div>
  17. <div slot="body">
  18. <ul>
  19. <li>
  20. <label for="selectLabel">{{ $t("label") }}</label>
  21. <input
  22. type="text"
  23. id="selectLabel"
  24. v-model="requestUpdateData.name"
  25. @keyup.enter="saveRequest"
  26. :placeholder="request.name"
  27. />
  28. <label for="selectCollection">{{ $t("collection") }}</label>
  29. <span class="select-wrapper">
  30. <select type="text" id="selectCollection" v-model="requestUpdateData.collectionIndex">
  31. <option :key="undefined" :value="undefined" hidden disabled selected>{{
  32. $t("current_collection")
  33. }}</option>
  34. <option
  35. v-for="(collection, index) in $store.state.postwoman.collections"
  36. :key="index"
  37. :value="index"
  38. >
  39. {{ collection.name }}
  40. </option>
  41. </select>
  42. </span>
  43. <label for="selectFolder">{{ $t("folder") }}</label>
  44. <span class="select-wrapper">
  45. <select type="text" id="selectFolder" v-model="requestUpdateData.folderIndex">
  46. <option :key="undefined" :value="undefined">/</option>
  47. <option v-for="(folder, index) in folders" :key="index" :value="index">
  48. {{ folder.name }}
  49. </option>
  50. </select>
  51. </span>
  52. </li>
  53. </ul>
  54. </div>
  55. <div slot="footer">
  56. <div class="flex-wrap">
  57. <span></span>
  58. <span>
  59. <button class="icon" @click="hideModal">
  60. {{ $t("cancel") }}
  61. </button>
  62. <button class="icon primary" @click="saveRequest">
  63. {{ $t("save") }}
  64. </button>
  65. </span>
  66. </div>
  67. </div>
  68. </modal>
  69. </template>
  70. <script>
  71. import { fb } from "../../functions/fb"
  72. export default {
  73. props: {
  74. show: Boolean,
  75. collectionIndex: Number,
  76. folderIndex: Number,
  77. request: Object,
  78. requestIndex: Number,
  79. },
  80. components: {
  81. modal: () => import("../../components/ui/modal"),
  82. },
  83. data() {
  84. return {
  85. requestUpdateData: {
  86. name: undefined,
  87. collectionIndex: undefined,
  88. folderIndex: undefined,
  89. },
  90. }
  91. },
  92. watch: {
  93. "requestUpdateData.collectionIndex": function resetFolderIndex() {
  94. // if user choosen some folder, than selected other collection, which doesn't have any folders
  95. // than `requestUpdateData.folderIndex` won't be reseted
  96. this.$data.requestUpdateData.folderIndex = undefined
  97. },
  98. },
  99. computed: {
  100. folders() {
  101. const userSelectedAnyCollection = this.$data.requestUpdateData.collectionIndex !== undefined
  102. if (!userSelectedAnyCollection) return []
  103. return this.$store.state.postwoman.collections[this.$data.requestUpdateData.collectionIndex]
  104. .folders
  105. },
  106. },
  107. methods: {
  108. syncCollections() {
  109. if (fb.currentUser !== null) {
  110. if (fb.currentSettings[0].value) {
  111. fb.writeCollections(JSON.parse(JSON.stringify(this.$store.state.postwoman.collections)))
  112. }
  113. }
  114. },
  115. saveRequest() {
  116. const userSelectedAnyCollection = this.$data.requestUpdateData.collectionIndex !== undefined
  117. const requestUpdated = {
  118. ...this.$props.request,
  119. name: this.$data.requestUpdateData.name || this.$props.request.name,
  120. collection: userSelectedAnyCollection
  121. ? this.$data.requestUpdateData.collectionIndex
  122. : this.$props.collectionIndex,
  123. folder: this.$data.requestUpdateData.folderIndex,
  124. }
  125. // pass data separately to don't depend on request's collection, folder fields
  126. // probably, they should be deprecated because they don't describe request itself
  127. this.$store.commit("postwoman/editRequest", {
  128. requestOldCollectionIndex: this.$props.collectionIndex,
  129. requestOldFolderIndex: this.$props.folderIndex,
  130. requestOldIndex: this.$props.requestIndex,
  131. requestNew: requestUpdated,
  132. requestNewCollectionIndex: requestUpdated.collection,
  133. requestNewFolderIndex: requestUpdated.folder,
  134. })
  135. this.hideModal()
  136. this.syncCollections()
  137. },
  138. hideModal() {
  139. this.$emit("hide-modal")
  140. },
  141. },
  142. }
  143. </script>