123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243 |
- <template>
- <SmartModal v-if="show" :title="$t('collection.save_as')" @close="hideModal">
- <template #body>
- <div class="flex flex-col px-2">
- <div class="flex relative">
- <input
- id="selectLabelSaveReq"
- v-model="requestName"
- v-focus
- class="input floating-input"
- placeholder=" "
- type="text"
- autocomplete="off"
- @keyup.enter="saveRequestAs"
- />
- <label for="selectLabelSaveReq">
- {{ $t("request.name") }}
- </label>
- </div>
- <label class="px-4 pt-4 pb-4">
- {{ $t("collection.select_location") }}
- </label>
- <CollectionsGraphql
- v-if="mode === 'graphql'"
- :doc="false"
- :show-coll-actions="false"
- :picked="picked"
- :saving-mode="true"
- @select="onSelect"
- />
- <Collections
- v-else
- :picked="picked"
- :save-request="true"
- @select="onSelect"
- @update-collection="collectionsType.type = $event"
- @update-coll-type="onUpdateCollType"
- />
- </div>
- </template>
- <template #footer>
- <span>
- <ButtonPrimary
- :label="$t('action.save')"
- @click.native="saveRequestAs"
- />
- <ButtonSecondary
- :label="$t('action.cancel')"
- @click.native="hideModal"
- />
- </span>
- </template>
- </SmartModal>
- </template>
- <script>
- import { defineComponent } from "@nuxtjs/composition-api"
- import * as teamUtils from "~/helpers/teams/utils"
- import {
- saveRESTRequestAs,
- editRESTRequest,
- editGraphqlRequest,
- saveGraphqlRequestAs,
- } from "~/newstore/collections"
- import { getGQLSession, useGQLRequestName } from "~/newstore/GQLSession"
- import {
- getRESTRequest,
- useRESTRequestName,
- setRESTSaveContext,
- } from "~/newstore/RESTSession"
- export default defineComponent({
- props: {
- // mode can be either "graphql" or "rest"
- mode: { type: String, default: "rest" },
- show: Boolean,
- },
- setup(props) {
- return {
- requestName:
- props.mode === "rest" ? useRESTRequestName() : useGQLRequestName(),
- }
- },
- data() {
- return {
- requestData: {
- name: this.requestName,
- collectionIndex: undefined,
- folderName: undefined,
- requestIndex: undefined,
- },
- collectionsType: {
- type: "my-collections",
- selectedTeam: undefined,
- },
- picked: null,
- }
- },
- 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
- this.$data.requestData.requestIndex = undefined
- },
- "requestData.folderName": function resetRequestIndex() {
- this.$data.requestData.requestIndex = undefined
- },
- editingRequest({ name }) {
- this.$data.requestData.name = name || this.$data.defaultRequestName
- },
- },
- methods: {
- onUpdateCollType(newCollType) {
- this.collectionsType = newCollType
- },
- onSelect({ picked }) {
- this.picked = picked
- },
- async saveRequestAs() {
- if (!this.requestName) {
- this.$toast.error(this.$t("error.empty_req_name"), {
- icon: "error_outline",
- })
- return
- }
- if (this.picked == null) {
- this.$toast.error(this.$t("collection.select"), {
- icon: "error_outline",
- })
- return
- }
- const requestUpdated =
- this.mode === "rest" ? getRESTRequest() : getGQLSession()
- // Filter out all REST file inputs
- if (this.mode === "rest" && requestUpdated.bodyParams) {
- requestUpdated.bodyParams = requestUpdated.bodyParams.map((param) =>
- param?.value?.[0] instanceof File ? { ...param, value: "" } : param
- )
- }
- if (this.picked.pickedType === "my-request") {
- editRESTRequest(
- this.picked.folderPath,
- this.picked.requestIndex,
- requestUpdated
- )
- setRESTSaveContext({
- originLocation: "user-collection",
- folderPath: this.picked.folderPath,
- requestIndex: this.picked.requestIndex,
- })
- } else if (this.picked.pickedType === "my-folder") {
- const insertionIndex = saveRESTRequestAs(
- this.picked.folderPath,
- requestUpdated
- )
- setRESTSaveContext({
- originLocation: "user-collection",
- folderPath: this.picked.folderPath,
- requestIndex: insertionIndex,
- })
- } else if (this.picked.pickedType === "my-collection") {
- const insertionIndex = saveRESTRequestAs(
- `${this.picked.collectionIndex}`,
- requestUpdated
- )
- setRESTSaveContext({
- originLocation: "user-collection",
- folderPath: `${this.picked.collectionIndex}`,
- requestIndex: insertionIndex,
- })
- } else if (this.picked.pickedType === "teams-request") {
- teamUtils.overwriteRequestTeams(
- this.$apollo,
- JSON.stringify(requestUpdated),
- requestUpdated.name,
- this.picked.requestID
- )
- setRESTSaveContext({
- originLocation: "team-collection",
- requestID: this.picked.requestID,
- })
- } else if (this.picked.pickedType === "teams-folder") {
- const req = await teamUtils.saveRequestAsTeams(
- this.$apollo,
- JSON.stringify(requestUpdated),
- requestUpdated.name,
- this.collectionsType.selectedTeam.id,
- this.picked.folderID
- )
- if (req && req.id) {
- setRESTSaveContext({
- originLocation: "team-collection",
- requestID: req.id,
- teamID: this.collectionsType.selectedTeam.id,
- collectionID: this.picked.folderID,
- })
- }
- } else if (this.picked.pickedType === "teams-collection") {
- const req = await teamUtils.saveRequestAsTeams(
- this.$apollo,
- JSON.stringify(requestUpdated),
- requestUpdated.name,
- this.collectionsType.selectedTeam.id,
- this.picked.collectionID
- )
- if (req && req.id) {
- setRESTSaveContext({
- originLocation: "team-collection",
- requestID: req.id,
- teamID: this.collectionsType.selectedTeam.id,
- collectionID: this.picked.collectionID,
- })
- }
- } else if (this.picked.pickedType === "gql-my-request") {
- editGraphqlRequest(
- this.picked.folderPath,
- this.picked.requestIndex,
- requestUpdated
- )
- } else if (this.picked.pickedType === "gql-my-folder") {
- saveGraphqlRequestAs(this.picked.folderPath, requestUpdated)
- } else if (this.picked.pickedType === "gql-my-collection") {
- saveGraphqlRequestAs(`${this.picked.collectionIndex}`, requestUpdated)
- }
- this.$toast.success(this.$t("request.added"), {
- icon: "post_add",
- })
- this.hideModal()
- },
- hideModal() {
- this.picked = null
- this.$emit("hide-modal")
- },
- },
- })
- </script>
|