api-client/components/collections/SaveRequest.vue

234 lines
6.9 KiB
Vue
Raw Normal View History

<template>
<SmartModal v-if="show" @close="hideModal">
2019-10-25 08:14:34 +00:00
<div slot="header">
2020-12-11 10:29:03 +00:00
<div class="row-wrapper">
<h3 class="title">{{ $t("save_request_as") }}</h3>
<div>
<button class="icon" @click="hideModal">
<i class="material-icons">close</i>
</button>
</div>
</div>
2019-10-25 08:14:34 +00:00
</div>
2020-12-11 16:54:34 +00:00
<div slot="body" class="flex flex-col">
2020-12-11 10:29:03 +00:00
<label for="selectLabel">{{ $t("token_req_name") }}</label>
<input type="text" id="selectLabel" v-model="requestData.name" @keyup.enter="saveRequestAs" />
<label for="selectLabel">Select location</label>
<!-- <input readonly :value="path" /> -->
<Collections
@select-folder="changeRequestDetails($event)"
@update-collection="collectionsType.type = $event"
:saveRequest="true"
2020-12-11 10:29:03 +00:00
/>
</div>
2019-10-25 08:14:34 +00:00
<div slot="footer">
2020-09-22 17:06:37 +00:00
<div class="row-wrapper">
2019-12-17 19:13:15 +00:00
<span></span>
<span>
<button class="icon" @click="hideModal">
{{ $t("cancel") }}
2019-10-25 08:14:34 +00:00
</button>
2019-12-17 19:13:15 +00:00
<button class="icon primary" @click="saveRequestAs">
{{ $t("save") }}
2019-12-17 19:13:15 +00:00
</button>
</span>
</div>
2019-10-25 08:14:34 +00:00
</div>
</SmartModal>
</template>
<script>
import { fb } from "~/helpers/fb"
import { getSettingSubject } from "~/newstore/settings"
2021-05-06 16:21:53 +00:00
import * as team_utils from "~/helpers/teams/utils"
2019-11-02 05:32:21 +00:00
export default {
props: {
show: Boolean,
2020-02-24 18:44:50 +00:00
editingRequest: Object,
2019-11-02 05:32:21 +00:00
},
data() {
return {
defaultRequestName: "Untitled Request",
path: "Path will appear here",
2019-11-02 05:32:21 +00:00
requestData: {
name: undefined,
collectionIndex: undefined,
folderName: undefined,
2020-02-24 18:44:50 +00:00
requestIndex: undefined,
},
collectionsType: {
type: "my-collections",
selectedTeam: undefined,
},
2020-02-24 18:44:50 +00:00
}
2019-11-02 05:32:21 +00:00
},
subscriptions() {
return {
SYNC_COLLECTIONS: getSettingSubject("syncCollections"),
}
},
2019-11-02 05:32:21 +00:00
watch: {
"requestData.collectionIndex": function resetFolderAndRequestIndex() {
// if user has chosen some folder, than selected other collection, which doesn't have any folders
// than `requestUpdateData.folderName` won't be reseted
this.$data.requestData.folderName = undefined
2020-02-24 18:44:50 +00:00
this.$data.requestData.requestIndex = undefined
},
"requestData.folderName": function resetRequestIndex() {
2020-02-24 18:44:50 +00:00
this.$data.requestData.requestIndex = undefined
},
editingRequest({ name }) {
this.$data.requestData.name = name || this.$data.defaultRequestName
},
2019-11-02 05:32:21 +00:00
},
computed: {
folders() {
2020-10-21 06:50:32 +00:00
const collections = this.$store.state.postwoman.collections
const collectionIndex = this.$data.requestData.collectionIndex
const userSelectedAnyCollection = collectionIndex !== undefined
2020-02-24 18:44:50 +00:00
if (!userSelectedAnyCollection) return []
2019-12-14 23:59:03 +00:00
const noCollectionAvailable = collections[collectionIndex] !== undefined
2020-02-24 18:44:50 +00:00
if (!noCollectionAvailable) return []
2019-12-14 23:59:03 +00:00
return getFolderNames(collections[collectionIndex].folders, [])
2019-11-02 05:32:21 +00:00
},
requests() {
const collections = this.$store.state.postwoman.collections
const collectionIndex = this.$data.requestData.collectionIndex
const folderName = this.$data.requestData.folderName
const userSelectedAnyCollection = collectionIndex !== undefined
if (!userSelectedAnyCollection) {
return []
}
2020-10-21 06:50:32 +00:00
const userSelectedAnyFolder = folderName !== undefined && folderName !== ""
2019-11-02 05:32:21 +00:00
if (userSelectedAnyFolder) {
const collection = collections[collectionIndex]
const folder = findFolder(folderName, collection)
return folder.requests
2019-11-02 05:32:21 +00:00
} else {
const collection = collections[collectionIndex]
const noCollectionAvailable = collection !== undefined
2020-10-21 06:50:32 +00:00
if (!noCollectionAvailable) {
return []
}
return collection.requests
2019-11-02 05:32:21 +00:00
}
2020-02-24 18:44:50 +00:00
},
2019-11-02 05:32:21 +00:00
},
methods: {
changeRequestDetails(data) {
this.$data.requestData.folderName = data.folderName.split("/").slice(-2)[0]
this.$data.path = data.folderName
this.$data.requestData.collectionIndex = data.collectionIndex
this.$data.requestData.requestIndex = data.reqIdx
if (data.collectionsType.type !== "my-collections") {
this.$data.collectionsType = data.collectionsType
}
},
syncCollections() {
if (fb.currentUser !== null && this.SYNC_COLLECTIONS) {
fb.writeCollections(
JSON.parse(JSON.stringify(this.$store.state.postwoman.collections)),
"collections"
)
}
},
2019-11-02 05:32:21 +00:00
saveRequestAs() {
2020-02-24 18:44:50 +00:00
const userDidntSpecifyCollection = this.$data.requestData.collectionIndex === undefined
2019-11-02 05:32:21 +00:00
if (userDidntSpecifyCollection) {
this.$toast.error(this.$t("select_collection"), {
icon: "error",
2020-02-24 18:44:50 +00:00
})
return
2019-10-25 08:14:34 +00:00
}
if (this.$data.requestData.name.length === 0) {
this.$toast.error(this.$t("empty_req_name"), {
icon: "error",
})
return
}
2019-11-02 05:32:21 +00:00
const requestUpdated = {
...this.$props.editingRequest,
name: this.$data.requestData.name,
2020-02-24 18:44:50 +00:00
collection: this.$data.requestData.collectionIndex,
}
if (this.$data.collectionsType.type === "my-collections") {
this.$store.commit("postwoman/saveRequestAs", {
request: requestUpdated,
collectionIndex: this.$data.requestData.collectionIndex,
folderName: this.$data.requestData.folderName,
requestIndex: this.$data.requestData.requestIndex,
flag: "rest",
})
this.syncCollections()
} else {
if (this.$data.requestData.requestIndex) {
team_utils.overwriteRequestTeams(
this.$apollo,
JSON.stringify(requestUpdated),
requestUpdated.name,
this.$data.requestData.requestIndex
)
} else {
team_utils.saveRequestAsTeams(
this.$apollo,
JSON.stringify(requestUpdated),
requestUpdated.name,
this.$data.collectionsType.selectedTeam.id,
this.$data.requestData.collectionIndex
)
}
}
this.$toast.success("Requested added", {
icon: "done",
})
2020-02-24 18:44:50 +00:00
this.hideModal()
2019-11-02 05:32:21 +00:00
},
hideModal() {
this.$emit("hide-modal")
2020-02-24 18:44:50 +00:00
},
},
}
function getFolderNames(folders, namesList, folderName = "") {
if (folders.length) {
2020-10-21 06:50:32 +00:00
folders.forEach((folder) => {
namesList.push(folderName + folder.name)
if (folder.folders && folder.folders.length) {
getFolderNames(folder.folders, namesList, folder.name + "/")
}
})
}
return namesList
}
function findFolder(folderName, currentFolder) {
2020-10-21 06:50:32 +00:00
let selectedFolder
let result
2020-10-21 06:50:32 +00:00
if (folderName === currentFolder.name) {
return currentFolder
}
for (let i = 0; i < currentFolder.folders.length; i++) {
2020-10-21 06:50:32 +00:00
selectedFolder = currentFolder.folders[i]
result = findFolder(folderName, selectedFolder)
if (result !== false) {
return result
}
}
return false
}
</script>