api-client/components/collections/saveRequestAs.vue

191 lines
6.1 KiB
Vue
Raw Normal View History

<template>
2019-10-25 08:14:34 +00:00
<modal v-if="show" @close="hideModal">
<div slot="header">
<ul>
<li>
<div class="flex-wrap">
<h3 class="title">{{ $t("save_request_as") }}</h3>
2019-10-25 08:14:34 +00:00
<div>
<button class="icon" @click="hideModal">
<i class="material-icons">close</i>
</button>
</div>
2019-10-25 08:14:34 +00:00
</div>
</li>
</ul>
</div>
<div slot="body">
<ul>
<li>
<label for="selectLabel">{{ $t("label") }}</label>
2019-11-02 05:32:21 +00:00
<input
type="text"
id="selectLabel"
v-model="requestData.name"
:placeholder="defaultRequestName"
2019-11-07 15:26:19 +00:00
@keyup.enter="saveRequestAs"
2019-11-02 05:32:21 +00:00
/>
<label for="selectCollection">{{ $t("collection") }}</label>
2019-12-14 23:59:03 +00:00
<span class="select-wrapper">
2020-02-24 18:44:50 +00:00
<select type="text" id="selectCollection" v-model="requestData.collectionIndex">
<option :key="undefined" :value="undefined" hidden disabled selected>{{
$t("select_collection")
2020-02-24 18:44:50 +00:00
}}</option>
2019-12-14 23:59:03 +00:00
<option
2020-02-24 18:44:50 +00:00
v-for="(collection, index) in $store.state.postwoman.collections"
2019-12-14 23:59:03 +00:00
:key="index"
:value="index"
>
{{ collection.name }}
</option>
</select>
</span>
<label for="selectFolder">{{ $t("folder") }}</label>
2019-12-14 23:59:03 +00:00
<span class="select-wrapper">
2020-02-24 18:44:50 +00:00
<select type="text" id="selectFolder" v-model="requestData.folderIndex">
2019-12-14 23:59:03 +00:00
<option :key="undefined" :value="undefined">/</option>
2020-02-24 18:44:50 +00:00
<option v-for="(folder, index) in folders" :key="index" :value="index">
2019-12-14 23:59:03 +00:00
{{ folder.name }}
</option>
</select>
</span>
<label for="selectRequest">{{ $t("request") }}</label>
2019-12-14 23:59:03 +00:00
<span class="select-wrapper">
2020-02-24 18:44:50 +00:00
<select type="text" id="selectRequest" v-model="requestData.requestIndex">
2019-12-14 23:59:03 +00:00
<option :key="undefined" :value="undefined">/</option>
2020-02-24 18:44:50 +00:00
<option v-for="(folder, index) in requests" :key="index" :value="index">
2019-12-14 23:59:03 +00:00
{{ folder.name }}
</option>
</select>
</span>
2019-10-25 08:14:34 +00:00
</li>
</ul>
</div>
2019-10-25 08:14:34 +00:00
<div slot="footer">
2019-12-17 19:13:15 +00:00
<div class="flex-wrap">
<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>
</modal>
</template>
<script>
import { fb } from "../../functions/fb"
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
},
components: {
2020-03-03 11:57:53 +00:00
modal: () => import("../../components/ui/modal"),
2019-11-02 05:32:21 +00:00
},
data() {
return {
defaultRequestName: "My Request",
2019-11-02 05:32:21 +00:00
requestData: {
name: undefined,
collectionIndex: undefined,
folderIndex: undefined,
2020-02-24 18:44:50 +00:00
requestIndex: undefined,
},
}
2019-11-02 05:32:21 +00:00
},
watch: {
"requestData.collectionIndex": function resetFolderAndRequestIndex() {
2019-11-02 05:32:21 +00:00
// if user choosen some folder, than selected other collection, which doesn't have any folders
// than `requestUpdateData.folderIndex` won't be reseted
2020-02-24 18:44:50 +00:00
this.$data.requestData.folderIndex = undefined
this.$data.requestData.requestIndex = undefined
},
"requestData.folderIndex": function resetRequestIndex() {
2020-02-24 18:44:50 +00:00
this.$data.requestData.requestIndex = undefined
},
2019-11-02 05:32:21 +00:00
},
computed: {
folders() {
2020-02-24 18:44:50 +00:00
const userSelectedAnyCollection = this.$data.requestData.collectionIndex !== undefined
if (!userSelectedAnyCollection) return []
2019-12-14 23:59:03 +00:00
const noCollectionAvailable =
2020-02-24 18:44:50 +00:00
this.$store.state.postwoman.collections[this.$data.requestData.collectionIndex] !==
undefined
if (!noCollectionAvailable) return []
2019-12-14 23:59:03 +00:00
2020-02-24 18:44:50 +00:00
return this.$store.state.postwoman.collections[this.$data.requestData.collectionIndex].folders
2019-11-02 05:32:21 +00:00
},
requests() {
2020-02-24 18:44:50 +00:00
const userSelectedAnyCollection = this.$data.requestData.collectionIndex !== undefined
if (!userSelectedAnyCollection) return []
2020-02-24 18:44:50 +00:00
const userSelectedAnyFolder = this.$data.requestData.folderIndex !== undefined
2019-11-02 05:32:21 +00:00
if (userSelectedAnyFolder) {
const collection = this.$store.state.postwoman.collections[
this.$data.requestData.collectionIndex
2020-02-24 18:44:50 +00:00
]
const folder = collection.folders[this.$data.requestData.folderIndex]
const requests = folder.requests
return requests
2019-11-02 05:32:21 +00:00
} else {
const collection = this.$store.state.postwoman.collections[
this.$data.requestData.collectionIndex
2020-02-24 18:44:50 +00:00
]
2019-12-14 23:59:03 +00:00
const noCollectionAvailable =
2020-02-24 18:44:50 +00:00
this.$store.state.postwoman.collections[this.$data.requestData.collectionIndex] !==
undefined
if (!noCollectionAvailable) return []
2020-02-24 18:44:50 +00:00
const requests = collection.requests
return 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: {
syncCollections() {
if (fb.currentUser !== null) {
if (fb.currentSettings[0].value) {
2020-02-24 18:44:50 +00:00
fb.writeCollections(JSON.parse(JSON.stringify(this.$store.state.postwoman.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
}
2019-11-02 05:32:21 +00:00
const requestUpdated = {
...this.$props.editingRequest,
name: this.$data.requestData.name || this.$data.defaultRequestName,
2020-02-24 18:44:50 +00:00
collection: this.$data.requestData.collectionIndex,
}
this.$store.commit("postwoman/saveRequestAs", {
2019-11-02 05:32:21 +00:00
request: requestUpdated,
collectionIndex: this.$data.requestData.collectionIndex,
folderIndex: this.$data.requestData.folderIndex,
2020-02-24 18:44:50 +00:00
requestIndex: this.$data.requestData.requestIndex,
})
2020-02-24 18:44:50 +00:00
this.hideModal()
this.syncCollections()
2019-11-02 05:32:21 +00:00
},
hideModal() {
this.$emit("hide-modal")
this.$emit("hide-model") // for backward compatibility // TODO: use fixed event
2020-02-24 18:44:50 +00:00
},
},
}
</script>