api-client/components/collections/saveRequestAs.vue

192 lines
5.6 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">Save Request As</h3>
<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>
2019-10-25 09:19:47 +00:00
<label for="selectLabel">Label</label>
2019-11-02 05:32:21 +00:00
<input
type="text"
id="selectLabel"
v-model="requestData.name"
v-bind:placeholder="defaultRequestName"
2019-11-07 15:26:19 +00:00
@keyup.enter="saveRequestAs"
2019-11-14 17:48:44 +00:00
list="preCollectionLabels"
2019-11-02 05:32:21 +00:00
/>
2019-11-14 17:48:44 +00:00
<datalist id="preCollectionLabels">
<option value="Login"></option>
<option value="Logout"></option>
<option value="Bug"></option>
<option value="Users"></option>
</datalist>
2019-10-25 09:19:47 +00:00
<label for="selectCollection">Collection</label>
2019-11-12 04:52:50 +00:00
<select
type="text"
id="selectCollection"
v-model="requestData.collectionIndex"
>
<option :key="undefined" :value="undefined" hidden disabled selected
>Select a Collection</option
>
2019-10-25 08:14:34 +00:00
<option
v-for="(collection, index) in $store.state.postwoman.collections"
:key="index"
:value="index"
2019-11-12 04:52:50 +00:00
>{{ collection.name }}</option
>
2019-10-25 08:14:34 +00:00
</select>
2019-10-25 09:19:47 +00:00
<label for="selectFolder">Folder</label>
2019-11-12 04:52:50 +00:00
<select
type="text"
id="selectFolder"
v-model="requestData.folderIndex"
>
2019-10-25 09:19:47 +00:00
<option :key="undefined" :value="undefined">/</option>
2019-11-12 04:52:50 +00:00
<option
v-for="(folder, index) in folders"
:key="index"
:value="index"
>{{ folder.name }}</option
>
2019-10-25 08:14:34 +00:00
</select>
2019-10-25 09:19:47 +00:00
<label for="selectRequest">Request</label>
2019-11-12 04:52:50 +00:00
<select
type="text"
id="selectRequest"
v-model="requestData.requestIndex"
>
2019-10-25 09:19:47 +00:00
<option :key="undefined" :value="undefined">/</option>
2019-10-25 08:14:34 +00:00
<option
v-for="(folder, index) in requests"
:key="index"
:value="index"
2019-11-12 04:52:50 +00:00
>{{ folder.name }}</option
>
2019-10-25 08:14:34 +00:00
</select>
</li>
</ul>
</div>
2019-10-25 08:14:34 +00:00
<div slot="footer">
<ul>
<li>
<button class="icon" @click="saveRequestAs">
<i class="material-icons">save</i>
<span>Save</span>
</button>
</li>
</ul>
</div>
</modal>
</template>
<script>
2019-11-02 05:32:21 +00:00
export default {
props: {
show: Boolean,
editingRequest: Object
},
components: {
2019-11-15 18:55:05 +00:00
modal: () => import("../../components/modal")
2019-11-02 05:32:21 +00:00
},
data() {
return {
2019-11-18 01:02:30 +00:00
defaultRequestName: "My Request",
2019-11-02 05:32:21 +00:00
requestData: {
name: undefined,
collectionIndex: undefined,
folderIndex: undefined,
requestIndex: undefined
2019-10-25 08:14:34 +00:00
}
2019-11-02 05:32:21 +00:00
};
},
watch: {
"requestData.collectionIndex": function resetFolderAndRequestIndex() {
// if user choosen some folder, than selected other collection, which doesn't have any folders
// than `requestUpdateData.folderIndex` won't be reseted
this.$data.requestData.folderIndex = undefined;
this.$data.requestData.requestIndex = undefined;
},
2019-11-02 05:32:21 +00:00
"requestData.folderIndex": function resetRequestIndex() {
this.$data.requestData.requestIndex = undefined;
}
},
computed: {
folders() {
const userSelectedAnyCollection =
this.$data.requestData.collectionIndex !== undefined;
if (!userSelectedAnyCollection) return [];
2019-11-02 05:32:21 +00:00
return this.$store.state.postwoman.collections[
this.$data.requestData.collectionIndex
].folders;
},
requests() {
const userSelectedAnyCollection =
this.$data.requestData.collectionIndex !== undefined;
if (!userSelectedAnyCollection) return [];
2019-11-02 05:32:21 +00:00
const userSelectedAnyFolder =
this.$data.requestData.folderIndex !== undefined;
if (userSelectedAnyFolder) {
const collection = this.$store.state.postwoman.collections[
this.$data.requestData.collectionIndex
];
const folder = collection.folders[this.$data.requestData.folderIndex];
const requests = folder.requests;
return requests;
} else {
const collection = this.$store.state.postwoman.collections[
this.$data.requestData.collectionIndex
];
const requests = collection.requests;
return requests;
}
}
},
methods: {
saveRequestAs() {
const userDidntSpecifyCollection =
this.$data.requestData.collectionIndex === undefined;
if (userDidntSpecifyCollection) {
this.$toast.error("Select a Collection", {
icon: "error"
});
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,
collection: this.$data.requestData.collectionIndex
};
2019-11-02 05:32:21 +00:00
this.$store.commit("postwoman/saveRequestAs", {
request: requestUpdated,
collectionIndex: this.$data.requestData.collectionIndex,
folderIndex: this.$data.requestData.folderIndex,
requestIndex: this.$data.requestData.requestIndex
});
2019-11-02 05:32:21 +00:00
this.hideModal();
},
hideModal() {
this.$emit("hide-modal");
this.$emit("hide-model"); // for backward compatibility // TODO: use fixed event
2019-10-25 08:14:34 +00:00
}
2019-11-02 05:32:21 +00:00
}
};
</script>