Initial Collection State System implementation

Co-authored-by: Liyas Thomas <hi@liyasthomas.com>
This commit is contained in:
Andrew Bastin
2021-05-27 23:22:17 -04:00
parent ac1937f9be
commit eda8c7da41
22 changed files with 872 additions and 487 deletions

View File

@@ -74,6 +74,7 @@
: $t('replace_current'), : $t('replace_current'),
}" }"
> >
<!-- TODO: wtf -->
<button <button
v-if="collectionsType.type == 'my-collections'" v-if="collectionsType.type == 'my-collections'"
:disabled="!fb.currentUser" :disabled="!fb.currentUser"
@@ -196,6 +197,11 @@
import { fb } from "~/helpers/fb" import { fb } from "~/helpers/fb"
import { getSettingSubject } from "~/newstore/settings" import { getSettingSubject } from "~/newstore/settings"
import * as teamUtils from "~/helpers/teams/utils" import * as teamUtils from "~/helpers/teams/utils"
import {
restCollections$,
setRESTCollections,
appendRESTCollections,
} from "~/newstore/collections"
export default { export default {
props: { props: {
@@ -204,7 +210,6 @@ export default {
}, },
data() { data() {
return { return {
fb,
showJsonCode: false, showJsonCode: false,
mode: "import_export", mode: "import_export",
mySelectedCollectionID: undefined, mySelectedCollectionID: undefined,
@@ -214,15 +219,9 @@ export default {
subscriptions() { subscriptions() {
return { return {
SYNC_COLLECTIONS: getSettingSubject("syncCollections"), SYNC_COLLECTIONS: getSettingSubject("syncCollections"),
myCollections: restCollections$,
} }
}, },
computed: {
myCollections() {
return fb.currentUser !== null
? fb.currentCollections
: this.$store.state.postwoman.collections
},
},
methods: { methods: {
async createCollectionGist() { async createCollectionGist() {
await this.$axios await this.$axios
@@ -266,10 +265,7 @@ export default {
}) })
.then(({ files }) => { .then(({ files }) => {
const collections = JSON.parse(Object.values(files)[0].content) const collections = JSON.parse(Object.values(files)[0].content)
this.$store.commit("postwoman/replaceCollections", { setRESTCollections(collections)
data: collections,
flag: "rest",
})
this.fileImported() this.fileImported()
this.syncToFBCollections() this.syncToFBCollections()
}) })
@@ -330,10 +326,7 @@ export default {
this.failedImport() this.failedImport()
}) })
} else { } else {
this.$store.commit("postwoman/replaceCollections", { setRESTCollections(collections)
data: collections,
flag: "rest",
})
this.fileImported() this.fileImported()
this.syncToFBCollections() this.syncToFBCollections()
} }
@@ -388,10 +381,7 @@ export default {
this.failedImport() this.failedImport()
}) })
} else { } else {
this.$store.commit("postwoman/importCollections", { appendRESTCollections(collections)
data: collections,
flag: "rest",
})
this.syncToFBCollections() this.syncToFBCollections()
this.fileImported() this.fileImported()
} }
@@ -421,11 +411,7 @@ export default {
}, },
async getJSONCollection() { async getJSONCollection() {
if (this.collectionsType.type === "my-collections") { if (this.collectionsType.type === "my-collections") {
this.collectionJson = JSON.stringify( this.collectionJson = JSON.stringify(myCollections, null, 2)
this.$store.state.postwoman.collections,
null,
2
)
} else { } else {
this.collectionJson = await teamUtils.exportAsJSON( this.collectionJson = await teamUtils.exportAsJSON(
this.$apollo, this.$apollo,
@@ -452,21 +438,6 @@ export default {
icon: "done", icon: "done",
}) })
}, },
syncCollections() {
this.$store.commit("postwoman/replaceCollections", {
data: fb.currentCollections,
flag: "rest",
})
this.fileImported()
},
syncToFBCollections() {
if (fb.currentUser !== null && this.SYNC_COLLECTIONS) {
fb.writeCollections(
JSON.parse(JSON.stringify(this.$store.state.postwoman.collections)),
"collections"
)
}
},
fileImported() { fileImported() {
this.$toast.info(this.$t("file_imported"), { this.$toast.info(this.$t("file_imported"), {
icon: "folder_shared", icon: "folder_shared",

View File

@@ -45,9 +45,9 @@
</template> </template>
<script> <script>
import { fb } from "~/helpers/fb"
import { getSettingSubject } from "~/newstore/settings" import { getSettingSubject } from "~/newstore/settings"
import * as teamUtils from "~/helpers/teams/utils" import * as teamUtils from "~/helpers/teams/utils"
import { saveRESTRequestAs, editRESTRequest } from "~/newstore/collections"
export default { export default {
props: { props: {
@@ -138,14 +138,6 @@ export default {
onSelect({ picked }) { onSelect({ picked }) {
this.picked = picked this.picked = picked
}, },
syncCollections() {
if (fb.currentUser !== null && this.SYNC_COLLECTIONS) {
fb.writeCollections(
JSON.parse(JSON.stringify(this.$store.state.postwoman.collections)),
"collections"
)
}
},
saveRequestAs() { saveRequestAs() {
if (this.picked == null) { if (this.picked == null) {
this.$toast.error(this.$t("select_collection"), { this.$toast.error(this.$t("select_collection"), {
@@ -163,36 +155,18 @@ export default {
const requestUpdated = { const requestUpdated = {
...this.$props.editingRequest, ...this.$props.editingRequest,
name: this.$data.requestData.name, name: this.$data.requestData.name,
collection: this.$data.requestData.collectionIndex,
} }
if (this.picked.pickedType === "my-request") { if (this.picked.pickedType === "my-request") {
this.$store.commit("postwoman/saveRequestAs", { editRESTRequest(
request: requestUpdated, this.picked.folderPath,
collectionIndex: this.picked.collectionIndex, this.picked.requestIndex,
folderName: this.picked.folderName, requestUpdated
requestIndex: this.picked.requestIndex, )
flag: "rest",
})
this.syncCollections()
} else if (this.picked.pickedType === "my-folder") { } else if (this.picked.pickedType === "my-folder") {
this.$store.commit("postwoman/saveRequestAs", { saveRESTRequestAs(this.picked.folderPath, requestUpdated)
request: requestUpdated,
collectionIndex: this.picked.collectionIndex,
folderName: this.picked.folderName,
flag: "rest",
})
this.syncCollections()
} else if (this.picked.pickedType === "my-collection") { } else if (this.picked.pickedType === "my-collection") {
this.$store.commit("postwoman/saveRequestAs", { saveRESTRequestAs(`${this.picked.collectionIndex}`, requestUpdated)
request: requestUpdated,
collectionIndex: this.picked.collectionIndex,
flag: "rest",
})
this.syncCollections()
} else if (this.picked.pickedType === "teams-request") { } else if (this.picked.pickedType === "teams-request") {
teamUtils.overwriteRequestTeams( teamUtils.overwriteRequestTeams(
this.$apollo, this.$apollo,

View File

@@ -36,41 +36,32 @@
</SmartModal> </SmartModal>
</template> </template>
<script> <script lang="ts">
import { fb } from "~/helpers/fb" import Vue from "vue"
import { addGraphqlCollection } from "~/newstore/collections"
export default { export default Vue.extend({
props: { props: {
show: Boolean, show: Boolean,
}, },
data() { data() {
return { return {
name: null, name: null as string | null,
} }
}, },
methods: { methods: {
syncCollections() {
if (fb.currentUser !== null && fb.currentSettings[0]) {
if (fb.currentSettings[0].value) {
fb.writeCollections(
JSON.parse(
JSON.stringify(this.$store.state.postwoman.collectionsGraphql)
),
"collectionsGraphql"
)
}
}
},
addNewCollection() { addNewCollection() {
if (!this.$data.name) { if (!this.name) {
this.$toast.info(this.$t("invalid_collection_name")) this.$toast.info(this.$t("invalid_collection_name").toString())
return return
} }
this.$store.commit("postwoman/addNewCollection", {
name: this.$data.name, addGraphqlCollection({
flag: "graphql", name: this.name,
folders: [],
requests: [],
}) })
this.syncCollections()
this.hideModal() this.hideModal()
}, },
hideModal() { hideModal() {
@@ -78,5 +69,5 @@ export default {
this.$emit("hide-modal") this.$emit("hide-modal")
}, },
}, },
} })
</script> </script>

View File

@@ -36,13 +36,12 @@
</SmartModal> </SmartModal>
</template> </template>
<script> <script lang="ts">
import { fb } from "~/helpers/fb" import Vue from "vue"
export default { export default Vue.extend({
props: { props: {
show: Boolean, show: Boolean,
folder: { type: Object, default: () => {} },
folderPath: { type: String, default: null }, folderPath: { type: String, default: null },
collectionIndex: { type: Number, default: null }, collectionIndex: { type: Number, default: null },
}, },
@@ -52,25 +51,13 @@ export default {
} }
}, },
methods: { methods: {
syncCollections() {
if (fb.currentUser !== null && fb.currentSettings[0]) {
if (fb.currentSettings[0].value) {
fb.writeCollections(
JSON.parse(
JSON.stringify(this.$store.state.postwoman.collectionsGraphql)
),
"collectionsGraphql"
)
}
}
},
addFolder() { addFolder() {
// TODO: Blocking when name is null ?
this.$emit("add-folder", { this.$emit("add-folder", {
name: this.name, name: this.name,
folder: this.folder,
path: this.folderPath || `${this.collectionIndex}`, path: this.folderPath || `${this.collectionIndex}`,
}) })
this.syncCollections()
this.hideModal() this.hideModal()
}, },
hideModal() { hideModal() {
@@ -78,5 +65,5 @@ export default {
this.$emit("hide-modal") this.$emit("hide-modal")
}, },
}, },
} })
</script> </script>

View File

@@ -42,7 +42,6 @@
class="icon" class="icon"
@click=" @click="
$emit('add-folder', { $emit('add-folder', {
folder: collection,
path: `${collectionIndex}`, path: `${collectionIndex}`,
}) })
" "
@@ -135,10 +134,11 @@
</div> </div>
</template> </template>
<script> <script lang="ts">
import { fb } from "~/helpers/fb" import Vue from "vue"
import { removeGraphqlCollection } from "~/newstore/collections"
export default { export default Vue.extend({
props: { props: {
collectionIndex: { type: Number, default: null }, collectionIndex: { type: Number, default: null },
collection: { type: Object, default: () => {} }, collection: { type: Object, default: () => {} },
@@ -154,38 +154,27 @@ export default {
} }
}, },
methods: { methods: {
syncCollections() {
if (fb.currentUser !== null && fb.currentSettings[0]) {
if (fb.currentSettings[0].value) {
fb.writeCollections(
JSON.parse(
JSON.stringify(this.$store.state.postwoman.collectionsGraphql)
),
"collectionsGraphql"
)
}
}
},
toggleShowChildren() { toggleShowChildren() {
this.showChildren = !this.showChildren this.showChildren = !this.showChildren
}, },
removeCollection() { removeCollection() {
this.$store.commit("postwoman/removeCollection", { removeGraphqlCollection(this.collectionIndex)
collectionIndex: this.collectionIndex,
flag: "graphql", this.$toast.error(this.$t("deleted").toString(), {
})
this.$toast.error(this.$t("deleted"), {
icon: "delete", icon: "delete",
}) })
this.syncCollections()
}, },
dropEvent({ dataTransfer }) { dropEvent({ dataTransfer }: any) {
this.dragging = !this.dragging this.dragging = !this.dragging
// TODO: Fix this
const oldCollectionIndex = dataTransfer.getData("oldCollectionIndex") const oldCollectionIndex = dataTransfer.getData("oldCollectionIndex")
const oldFolderIndex = dataTransfer.getData("oldFolderIndex") const oldFolderIndex = dataTransfer.getData("oldFolderIndex")
const oldFolderName = dataTransfer.getData("oldFolderName") const oldFolderName = dataTransfer.getData("oldFolderName")
const requestIndex = dataTransfer.getData("requestIndex") const requestIndex = dataTransfer.getData("requestIndex")
const flag = "graphql" const flag = "graphql"
this.$store.commit("postwoman/moveRequest", { this.$store.commit("postwoman/moveRequest", {
oldCollectionIndex, oldCollectionIndex,
newCollectionIndex: this.$props.collectionIndex, newCollectionIndex: this.$props.collectionIndex,
@@ -196,8 +185,8 @@ export default {
requestIndex, requestIndex,
flag, flag,
}) })
this.syncCollections() // this.syncCollections()
}, },
}, },
} })
</script> </script>

View File

@@ -36,10 +36,11 @@
</SmartModal> </SmartModal>
</template> </template>
<script> <script lang="ts">
import { fb } from "~/helpers/fb" import Vue from "vue"
import { editGraphqlCollection } from "~/newstore/collections"
export default { export default Vue.extend({
props: { props: {
show: Boolean, show: Boolean,
editingCollection: { type: Object, default: () => {} }, editingCollection: { type: Object, default: () => {} },
@@ -47,37 +48,21 @@ export default {
}, },
data() { data() {
return { return {
name: null, name: null as string | null,
} }
}, },
methods: { methods: {
syncCollections() {
if (fb.currentUser !== null && fb.currentSettings[0]) {
if (fb.currentSettings[0].value) {
fb.writeCollections(
JSON.parse(
JSON.stringify(this.$store.state.postwoman.collectionsGraphql)
),
"collectionsGraphql"
)
}
}
},
saveCollection() { saveCollection() {
if (!this.$data.name) { if (!this.name) {
this.$toast.info(this.$t("invalid_collection_name")) this.$toast.info(this.$t("invalid_collection_name").toString())
return return
} }
const collectionUpdated = { const collectionUpdated = {
...this.$props.editingCollection, ...this.$props.editingCollection,
name: this.$data.name, name: this.name,
} }
this.$store.commit("postwoman/editCollection", {
collection: collectionUpdated, editGraphqlCollection(this.editingCollectionIndex, collectionUpdated)
collectionIndex: this.$props.editingCollectionIndex,
flag: "graphql",
})
this.syncCollections()
this.hideModal() this.hideModal()
}, },
hideModal() { hideModal() {
@@ -85,5 +70,5 @@ export default {
this.$emit("hide-modal") this.$emit("hide-modal")
}, },
}, },
} })
</script> </script>

View File

@@ -36,15 +36,15 @@
</SmartModal> </SmartModal>
</template> </template>
<script> <script lang="ts">
import { fb } from "~/helpers/fb" import Vue from "vue"
import { editGraphqlFolder } from "~/newstore/collections"
export default { export default Vue.extend({
props: { props: {
show: Boolean, show: Boolean,
collectionIndex: { type: Number, default: null },
folder: { type: Object, default: () => {} }, folder: { type: Object, default: () => {} },
folderIndex: { type: Number, default: null }, folderPath: { type: String, default: null },
}, },
data() { data() {
return { return {
@@ -52,27 +52,8 @@ export default {
} }
}, },
methods: { methods: {
syncCollections() {
if (fb.currentUser !== null && fb.currentSettings[0]) {
if (fb.currentSettings[0].value) {
fb.writeCollections(
JSON.parse(
JSON.stringify(this.$store.state.postwoman.collectionsGraphql)
),
"collectionsGraphql"
)
}
}
},
editFolder() { editFolder() {
this.$store.commit("postwoman/editFolder", { editGraphqlFolder(this.folderPath, { ...this.folder, name: this.name })
collectionIndex: this.$props.collectionIndex,
folder: { ...this.$props.folder, name: this.$data.name },
folderIndex: this.$props.folderIndex,
folderName: this.$props.folder.name,
flag: "graphql",
})
this.syncCollections()
this.hideModal() this.hideModal()
}, },
hideModal() { hideModal() {
@@ -80,5 +61,5 @@ export default {
this.$emit("hide-modal") this.$emit("hide-modal")
}, },
}, },
} })
</script> </script>

View File

@@ -36,53 +36,33 @@
</SmartModal> </SmartModal>
</template> </template>
<script> <script lang="ts">
import { fb } from "~/helpers/fb" import Vue from "vue"
import { editGraphqlRequest } from "~/newstore/collections"
export default { export default Vue.extend({
props: { props: {
show: Boolean, show: Boolean,
collectionIndex: { type: Number, default: null }, folderPath: { type: String, default: null },
folderIndex: { type: Number, default: null },
folderName: { type: String, default: null },
request: { type: Object, default: () => {} }, request: { type: Object, default: () => {} },
requestIndex: { type: Number, default: null }, requestIndex: { type: Number, default: null },
}, },
data() { data() {
return { return {
requestUpdateData: { requestUpdateData: {
name: null, name: null as any | null,
}, },
} }
}, },
methods: { methods: {
syncCollections() {
if (fb.currentUser !== null && fb.currentSettings[0]) {
if (fb.currentSettings[0].value) {
fb.writeCollections(
JSON.parse(
JSON.stringify(this.$store.state.postwoman.collectionsGraphql)
),
"collectionsGraphql"
)
}
}
},
saveRequest() { saveRequest() {
const requestUpdated = { const requestUpdated = {
...this.$props.request, ...this.$props.request,
name: this.$data.requestUpdateData.name || this.$props.request.name, name: this.$data.requestUpdateData.name || this.$props.request.name,
} }
this.$store.commit("postwoman/editRequest", { editGraphqlRequest(this.folderPath, this.requestIndex, requestUpdated)
requestCollectionIndex: this.$props.collectionIndex,
requestFolderName: this.$props.folderName,
requestFolderIndex: this.$props.folderIndex,
requestNew: requestUpdated,
requestIndex: this.$props.requestIndex,
flag: "graphql",
})
this.syncCollections()
this.hideModal() this.hideModal()
}, },
hideModal() { hideModal() {
@@ -90,5 +70,5 @@ export default {
this.$emit("hide-modal") this.$emit("hide-modal")
}, },
}, },
} })
</script> </script>

View File

@@ -43,9 +43,7 @@
<button <button
v-close-popover v-close-popover
class="icon" class="icon"
@click=" @click="$emit('edit-folder', { folder, folderPath })"
$emit('edit-folder', { folder, folderIndex, collectionIndex })
"
> >
<i class="material-icons">edit</i> <i class="material-icons">edit</i>
<span>{{ $t("edit") }}</span> <span>{{ $t("edit") }}</span>
@@ -122,10 +120,11 @@
</div> </div>
</template> </template>
<script> <script lang="ts">
import { fb } from "~/helpers/fb" import Vue from "vue"
import { removeGraphqlFolder } from "~/newstore/collections"
export default { export default Vue.extend({
name: "Folder", name: "Folder",
props: { props: {
folder: { type: Object, default: () => {} }, folder: { type: Object, default: () => {} },
@@ -143,34 +142,16 @@ export default {
} }
}, },
methods: { methods: {
syncCollections() {
if (fb.currentUser !== null && fb.currentSettings[0]) {
if (fb.currentSettings[0].value) {
fb.writeCollections(
JSON.parse(
JSON.stringify(this.$store.state.postwoman.collectionsGraphql)
),
"collectionsGraphql"
)
}
}
},
toggleShowChildren() { toggleShowChildren() {
this.showChildren = !this.showChildren this.showChildren = !this.showChildren
}, },
removeFolder() { removeFolder() {
this.$store.commit("postwoman/removeFolder", { removeGraphqlFolder(this.folderPath)
collectionIndex: this.$props.collectionIndex, this.$toast.error(this.$t("deleted").toString(), {
folderName: this.$props.folder.name,
folderIndex: this.$props.folderIndex,
flag: "graphql",
})
this.syncCollections()
this.$toast.error(this.$t("deleted"), {
icon: "delete", icon: "delete",
}) })
}, },
dropEvent({ dataTransfer }) { dropEvent({ dataTransfer }: any) {
this.dragging = !this.dragging this.dragging = !this.dragging
const oldCollectionIndex = dataTransfer.getData("oldCollectionIndex") const oldCollectionIndex = dataTransfer.getData("oldCollectionIndex")
const oldFolderIndex = dataTransfer.getData("oldFolderIndex") const oldFolderIndex = dataTransfer.getData("oldFolderIndex")
@@ -178,6 +159,8 @@ export default {
const requestIndex = dataTransfer.getData("requestIndex") const requestIndex = dataTransfer.getData("requestIndex")
const flag = "graphql" const flag = "graphql"
// TODO: Discuss
this.$store.commit("postwoman/moveRequest", { this.$store.commit("postwoman/moveRequest", {
oldCollectionIndex, oldCollectionIndex,
newCollectionIndex: this.$props.collectionIndex, newCollectionIndex: this.$props.collectionIndex,
@@ -188,8 +171,8 @@ export default {
requestIndex, requestIndex,
flag, flag,
}) })
this.syncCollections() // this.syncCollections()
}, },
}, },
} })
</script> </script>

View File

@@ -131,6 +131,10 @@
<script> <script>
import { fb } from "~/helpers/fb" import { fb } from "~/helpers/fb"
import {
graphqlCollections$,
setGraphqlCollections,
} from "~/newstore/collections"
export default { export default {
props: { props: {
@@ -142,13 +146,14 @@ export default {
showJsonCode: false, showJsonCode: false,
} }
}, },
subscriptions() {
return {
collections: graphqlCollections$,
}
},
computed: { computed: {
collectionJson() { collectionJson() {
return JSON.stringify( return JSON.stringify(this.collections, null, 2)
this.$store.state.postwoman.collectionsGraphql,
null,
2
)
}, },
}, },
methods: { methods: {
@@ -194,12 +199,8 @@ export default {
}) })
.then(({ files }) => { .then(({ files }) => {
const collections = JSON.parse(Object.values(files)[0].content) const collections = JSON.parse(Object.values(files)[0].content)
this.$store.commit("postwoman/replaceCollections", { setGraphqlCollections(collections)
data: collections,
flag: "graphql",
})
this.fileImported() this.fileImported()
this.syncToFBCollections()
}) })
.catch((error) => { .catch((error) => {
this.failedImport() this.failedImport()
@@ -238,12 +239,8 @@ export default {
this.failedImport() this.failedImport()
return return
} }
this.$store.commit("postwoman/replaceCollections", { setGraphqlCollections(collections)
data: collections,
flag: "graphql",
})
this.fileImported() this.fileImported()
this.syncToFBCollections()
} }
reader.readAsText(this.$refs.inputChooseFileToReplaceWith.files[0]) reader.readAsText(this.$refs.inputChooseFileToReplaceWith.files[0])
this.$refs.inputChooseFileToReplaceWith.value = "" this.$refs.inputChooseFileToReplaceWith.value = ""
@@ -310,18 +307,6 @@ export default {
}) })
this.fileImported() this.fileImported()
}, },
syncToFBCollections() {
if (fb.currentUser !== null && fb.currentSettings[0]) {
if (fb.currentSettings[0].value) {
fb.writeCollections(
JSON.parse(
JSON.stringify(this.$store.state.postwoman.collectionsGraphql)
),
"collectionsGraphql"
)
}
}
},
fileImported() { fileImported() {
this.$toast.info(this.$t("file_imported"), { this.$toast.info(this.$t("file_imported"), {
icon: "folder_shared", icon: "folder_shared",

View File

@@ -32,11 +32,9 @@
class="icon" class="icon"
@click=" @click="
$emit('edit-request', { $emit('edit-request', {
collectionIndex,
folderIndex,
folderName,
request, request,
requestIndex, requestIndex,
folderPath,
}) })
" "
> >
@@ -62,15 +60,14 @@
</div> </div>
</template> </template>
<script> <script lang="ts">
import { fb } from "~/helpers/fb" import Vue from "vue"
import { removeGraphqlRequest } from "~/newstore/collections"
export default { export default Vue.extend({
props: { props: {
request: { type: Object, default: () => {} }, request: { type: Object, default: () => {} },
collectionIndex: { type: Number, default: null }, folderPath: { type: String, default: null },
folderIndex: { type: Number, default: null },
folderName: { type: String, default: null },
requestIndex: { type: Number, default: null }, requestIndex: { type: Number, default: null },
doc: Boolean, doc: Boolean,
}, },
@@ -81,43 +78,26 @@ export default {
} }
}, },
methods: { methods: {
syncCollections() {
if (fb.currentUser !== null && fb.currentSettings[0]) {
if (fb.currentSettings[0].value) {
fb.writeCollections(
JSON.parse(
JSON.stringify(this.$store.state.postwoman.collectionsGraphql)
),
"collectionsGraphql"
)
}
}
},
selectRequest() { selectRequest() {
this.$store.commit("postwoman/selectGraphqlRequest", { this.$store.commit("postwoman/selectGraphqlRequest", {
request: this.request, request: this.request,
}) })
}, },
dragStart({ dataTransfer }) { dragStart({ dataTransfer }: any) {
this.dragging = !this.dragging this.dragging = !this.dragging
// TODO: Discuss
dataTransfer.setData("oldCollectionIndex", this.$props.collectionIndex) dataTransfer.setData("oldCollectionIndex", this.$props.collectionIndex)
dataTransfer.setData("oldFolderIndex", this.$props.folderIndex) dataTransfer.setData("oldFolderIndex", this.$props.folderIndex)
dataTransfer.setData("oldFolderName", this.$props.folderName) dataTransfer.setData("oldFolderName", this.$props.folderName)
dataTransfer.setData("requestIndex", this.$props.requestIndex) dataTransfer.setData("requestIndex", this.$props.requestIndex)
}, },
removeRequest() { removeRequest() {
this.$store.commit("postwoman/removeRequest", { removeGraphqlRequest(this.folderPath, this.requestIndex)
collectionIndex: this.$props.collectionIndex, this.$toast.error(this.$t("deleted").toString(), {
folderName: this.$props.folderName,
requestIndex: this.$props.requestIndex,
flag: "graphql",
})
this.$toast.error(this.$t("deleted"), {
icon: "delete", icon: "delete",
}) })
this.confirmRemove = false this.confirmRemove = false
this.syncCollections()
}, },
}, },
} })
</script> </script>

View File

@@ -26,7 +26,6 @@
/> />
<CollectionsGraphqlAddFolder <CollectionsGraphqlAddFolder
:show="showModalAddFolder" :show="showModalAddFolder"
:folder="editingFolder"
:folder-path="editingFolderPath" :folder-path="editingFolderPath"
@add-folder="onAddFolder($event)" @add-folder="onAddFolder($event)"
@hide-modal="displayModalAddFolder(false)" @hide-modal="displayModalAddFolder(false)"
@@ -36,13 +35,12 @@
:collection-index="editingCollectionIndex" :collection-index="editingCollectionIndex"
:folder="editingFolder" :folder="editingFolder"
:folder-index="editingFolderIndex" :folder-index="editingFolderIndex"
:folder-path="editingFolderPath"
@hide-modal="displayModalEditFolder(false)" @hide-modal="displayModalEditFolder(false)"
/> />
<CollectionsGraphqlEditRequest <CollectionsGraphqlEditRequest
:show="showModalEditRequest" :show="showModalEditRequest"
:collection-index="editingCollectionIndex" :folder-path="editingFolderPath"
:folder-index="editingFolderIndex"
:folder-name="editingFolderName"
:request="editingRequest" :request="editingRequest"
:request-index="editingRequestIndex" :request-index="editingRequestIndex"
@hide-modal="displayModalEditRequest(false)" @hide-modal="displayModalEditRequest(false)"
@@ -94,7 +92,7 @@
</template> </template>
<script> <script>
import { fb } from "~/helpers/fb" import { graphqlCollections$, addGraphqlFolder } from "~/newstore/collections"
export default { export default {
props: { props: {
@@ -119,17 +117,14 @@ export default {
filterText: "", filterText: "",
} }
}, },
subscriptions() {
return {
collections: graphqlCollections$,
}
},
computed: { computed: {
collections() {
return fb.currentUser !== null
? fb.currentGraphqlCollections
: this.$store.state.postwoman.collectionsGraphql
},
filteredCollections() { filteredCollections() {
const collections = const collections = this.collections
fb.currentUser !== null
? fb.currentGraphqlCollections
: this.$store.state.postwoman.collectionsGraphql
if (!this.filterText) return collections if (!this.filterText) return collections
@@ -216,32 +211,21 @@ export default {
this.$data.editingCollection = collection this.$data.editingCollection = collection
this.$data.editingCollectionIndex = collectionIndex this.$data.editingCollectionIndex = collectionIndex
this.displayModalEdit(true) this.displayModalEdit(true)
this.syncCollections()
}, },
onAddFolder({ name, path }) { onAddFolder({ name, path }) {
const flag = "graphql" addGraphqlFolder(name, path)
this.$store.commit("postwoman/addFolder", {
name,
path,
flag,
})
this.displayModalAddFolder(false) this.displayModalAddFolder(false)
this.syncCollections()
}, },
addFolder(payload) { addFolder(payload) {
const { folder, path } = payload const { path } = payload
this.$data.editingFolder = folder
this.$data.editingFolderPath = path this.$data.editingFolderPath = path
this.displayModalAddFolder(true) this.displayModalAddFolder(true)
}, },
editFolder(payload) { editFolder(payload) {
const { collectionIndex, folder, folderIndex } = payload const { folder, folderPath } = payload
this.$data.editingCollectionIndex = collectionIndex this.editingFolder = folder
this.$data.editingFolder = folder this.editingFolderPath = folderPath
this.$data.editingFolderIndex = folderIndex
this.displayModalEditFolder(true) this.displayModalEditFolder(true)
this.syncCollections()
}, },
editRequest(payload) { editRequest(payload) {
const { const {
@@ -257,7 +241,6 @@ export default {
this.$data.editingRequest = request this.$data.editingRequest = request
this.$data.editingRequestIndex = requestIndex this.$data.editingRequestIndex = requestIndex
this.displayModalEditRequest(true) this.displayModalEditRequest(true)
this.syncCollections()
}, },
resetSelectedData() { resetSelectedData() {
this.$data.editingCollection = undefined this.$data.editingCollection = undefined
@@ -267,18 +250,6 @@ export default {
this.$data.editingRequest = undefined this.$data.editingRequest = undefined
this.$data.editingRequestIndex = undefined this.$data.editingRequestIndex = undefined
}, },
syncCollections() {
if (fb.currentUser !== null && fb.currentSettings[0]) {
if (fb.currentSettings[0].value) {
fb.writeCollections(
JSON.parse(
JSON.stringify(this.$store.state.postwoman.collectionsGraphql)
),
"collectionsGraphql"
)
}
}
},
}, },
} }
</script> </script>

View File

@@ -141,9 +141,18 @@
import gql from "graphql-tag" import gql from "graphql-tag"
import cloneDeep from "lodash/cloneDeep" import cloneDeep from "lodash/cloneDeep"
import { fb } from "~/helpers/fb" import { fb } from "~/helpers/fb"
import { getSettingSubject } from "~/newstore/settings"
import TeamCollectionAdapter from "~/helpers/teams/TeamCollectionAdapter" import TeamCollectionAdapter from "~/helpers/teams/TeamCollectionAdapter"
import * as teamUtils from "~/helpers/teams/utils" import * as teamUtils from "~/helpers/teams/utils"
import {
restCollections$,
addRESTCollection,
editRESTCollection,
addRESTFolder,
removeRESTCollection,
editRESTFolder,
removeRESTRequest,
editRESTRequest,
} from "~/newstore/collections"
export default { export default {
props: { props: {
@@ -179,7 +188,7 @@ export default {
}, },
subscriptions() { subscriptions() {
return { return {
SYNC_COLLECTIONS: getSettingSubject("syncCollections"), collections: restCollections$,
} }
}, },
computed: { computed: {
@@ -189,21 +198,11 @@ export default {
} }
return true return true
}, },
collections() {
return fb.currentUser !== null
? fb.currentCollections
: this.$store.state.postwoman.collections
},
filteredCollections() { filteredCollections() {
let collections = null const collections =
if (this.collectionsType.type === "my-collections") { this.collectionsType.type === "my-collections"
collections = ? this.collections
fb.currentUser !== null : this.teamCollectionsNew
? fb.currentCollections
: this.$store.state.postwoman.collections
} else {
collections = this.teamCollectionsNew
}
if (!this.filterText) { if (!this.filterText) {
return collections return collections
@@ -301,12 +300,11 @@ export default {
return return
} }
if (this.collectionsType.type === "my-collections") { if (this.collectionsType.type === "my-collections") {
this.$store.commit("postwoman/addNewCollection", { addRESTCollection({
name, name,
flag: "rest", folders: [],
requests: [],
}) })
this.syncCollections()
} else if ( } else if (
this.collectionsType.type === "team-collections" && this.collectionsType.type === "team-collections" &&
this.collectionsType.selectedTeam.myRole !== "VIEWER" this.collectionsType.selectedTeam.myRole !== "VIEWER"
@@ -342,12 +340,8 @@ export default {
...this.editingCollection, ...this.editingCollection,
name: newName, name: newName,
} }
this.$store.commit("postwoman/editCollection", {
collection: collectionUpdated, editRESTCollection(this.editingCollectionIndex, collectionUpdated)
collectionIndex: this.editingCollectionIndex,
flag: "rest",
})
this.syncCollections()
} else if ( } else if (
this.collectionsType.type === "team-collections" && this.collectionsType.type === "team-collections" &&
this.collectionsType.selectedTeam.myRole !== "VIEWER" this.collectionsType.selectedTeam.myRole !== "VIEWER"
@@ -372,14 +366,7 @@ export default {
// Intended to be called by CollectionEditFolder modal submit event // Intended to be called by CollectionEditFolder modal submit event
updateEditingFolder(name) { updateEditingFolder(name) {
if (this.collectionsType.type === "my-collections") { if (this.collectionsType.type === "my-collections") {
this.$store.commit("postwoman/editFolder", { editRESTFolder(this.editingFolderPath, { ...this.editingFolder, name })
collectionIndex: this.editingCollectionIndex,
folder: { ...this.editingFolder, name },
folderIndex: this.editingFolderIndex,
folderName: this.editingFolder.name,
flag: "rest",
})
this.syncCollections()
} else if ( } else if (
this.collectionsType.type === "team-collections" && this.collectionsType.type === "team-collections" &&
this.collectionsType.selectedTeam.myRole !== "VIEWER" this.collectionsType.selectedTeam.myRole !== "VIEWER"
@@ -411,15 +398,11 @@ export default {
} }
if (this.collectionsType.type === "my-collections") { if (this.collectionsType.type === "my-collections") {
this.$store.commit("postwoman/editRequest", { editRESTRequest(
requestCollectionIndex: this.editingCollectionIndex, this.editingFolderPath,
requestFolderName: this.editingFolderName, this.editingRequestIndex,
requestFolderIndex: this.editingFolderIndex, requestUpdated
requestNew: requestUpdated, )
requestIndex: this.editingRequestIndex,
flag: "rest",
})
this.syncCollections()
} else if ( } else if (
this.collectionsType.type === "team-collections" && this.collectionsType.type === "team-collections" &&
this.collectionsType.selectedTeam.myRole !== "VIEWER" this.collectionsType.selectedTeam.myRole !== "VIEWER"
@@ -478,17 +461,10 @@ export default {
this.$data.editingCollection = collection this.$data.editingCollection = collection
this.$data.editingCollectionIndex = collectionIndex this.$data.editingCollectionIndex = collectionIndex
this.displayModalEdit(true) this.displayModalEdit(true)
this.syncCollections()
}, },
onAddFolder({ name, folder, path }) { onAddFolder({ name, folder, path }) {
const flag = "rest"
if (this.collectionsType.type === "my-collections") { if (this.collectionsType.type === "my-collections") {
this.$store.commit("postwoman/addFolder", { addRESTFolder(name, path)
name,
path,
flag,
})
this.syncCollections()
} else if (this.collectionsType.type === "team-collections") { } else if (this.collectionsType.type === "team-collections") {
if (this.collectionsType.selectedTeam.myRole !== "VIEWER") { if (this.collectionsType.selectedTeam.myRole !== "VIEWER") {
this.$apollo this.$apollo
@@ -538,30 +514,33 @@ export default {
this.displayModalAddFolder(true) this.displayModalAddFolder(true)
}, },
editFolder(payload) { editFolder(payload) {
const { collectionIndex, folder, folderIndex } = payload console.log(payload)
const { collectionIndex, folder, folderIndex, folderPath } = payload
this.$data.editingCollectionIndex = collectionIndex this.$data.editingCollectionIndex = collectionIndex
this.$data.editingFolder = folder this.$data.editingFolder = folder
this.$data.editingFolderIndex = folderIndex this.$data.editingFolderIndex = folderIndex
this.$data.editingFolderPath = folderPath
this.$data.collectionsType = this.collectionsType this.$data.collectionsType = this.collectionsType
this.displayModalEditFolder(true) this.displayModalEditFolder(true)
this.syncCollections()
}, },
editRequest(payload) { editRequest(payload) {
console.log(payload)
const { const {
collectionIndex, collectionIndex,
folderIndex, folderIndex,
folderName, folderName,
request, request,
requestIndex, requestIndex,
folderPath,
} = payload } = payload
this.$data.editingCollectionIndex = collectionIndex this.$data.editingCollectionIndex = collectionIndex
this.$data.editingFolderIndex = folderIndex this.$data.editingFolderIndex = folderIndex
this.$data.editingFolderName = folderName this.$data.editingFolderName = folderName
this.$data.editingRequest = request this.$data.editingRequest = request
this.$data.editingRequestIndex = requestIndex this.$data.editingRequestIndex = requestIndex
this.editingFolderPath = folderPath
this.$emit("select-request", requestIndex) this.$emit("select-request", requestIndex)
this.displayModalEditRequest(true) this.displayModalEditRequest(true)
this.syncCollections()
}, },
resetSelectedData() { resetSelectedData() {
this.$data.editingCollection = undefined this.$data.editingCollection = undefined
@@ -571,27 +550,16 @@ export default {
this.$data.editingRequest = undefined this.$data.editingRequest = undefined
this.$data.editingRequestIndex = undefined this.$data.editingRequestIndex = undefined
}, },
syncCollections() {
if (fb.currentUser !== null && this.SYNC_COLLECTIONS) {
fb.writeCollections(
JSON.parse(JSON.stringify(this.$store.state.postwoman.collections)),
"collections"
)
}
},
expandCollection(collectionID) { expandCollection(collectionID) {
this.teamCollectionAdapter.expandCollection(collectionID) this.teamCollectionAdapter.expandCollection(collectionID)
}, },
removeCollection({ collectionsType, collectionIndex, collectionID }) { removeCollection({ collectionsType, collectionIndex, collectionID }) {
if (collectionsType.type === "my-collections") { if (collectionsType.type === "my-collections") {
this.$store.commit("postwoman/removeCollection", { removeRESTCollection(collectionIndex)
collectionIndex,
flag: "rest",
})
this.$toast.error(this.$t("deleted"), { this.$toast.error(this.$t("deleted"), {
icon: "delete", icon: "delete",
}) })
this.syncCollections()
} else if (collectionsType.type === "team-collections") { } else if (collectionsType.type === "team-collections") {
if (collectionsType.selectedTeam.myRole !== "VIEWER") { if (collectionsType.selectedTeam.myRole !== "VIEWER") {
this.$apollo this.$apollo
@@ -623,18 +591,13 @@ export default {
} }
} }
}, },
removeRequest({ collectionIndex, folderName, requestIndex }) { removeRequest({ requestIndex, folderPath }) {
if (this.collectionsType.type === "my-collections") { if (this.collectionsType.type === "my-collections") {
this.$store.commit("postwoman/removeRequest", { removeRESTRequest(folderPath, requestIndex)
collectionIndex,
folderName,
requestIndex,
flag: "rest",
})
this.$toast.error(this.$t("deleted"), { this.$toast.error(this.$t("deleted"), {
icon: "delete", icon: "delete",
}) })
this.syncCollections()
} else if (this.collectionsType.type === "team-collections") { } else if (this.collectionsType.type === "team-collections") {
teamUtils teamUtils
.deleteRequest(this.$apollo, requestIndex) .deleteRequest(this.$apollo, requestIndex)

View File

@@ -109,7 +109,7 @@
@edit-folder="$emit('edit-folder', $event)" @edit-folder="$emit('edit-folder', $event)"
@edit-request="$emit('edit-request', $event)" @edit-request="$emit('edit-request', $event)"
@select="$emit('select', $event)" @select="$emit('select', $event)"
@remove-request="removeRequest" @remove-request="$emit('remove-request', $event)"
/> />
</li> </li>
</ul> </ul>
@@ -132,7 +132,7 @@
:picked="picked" :picked="picked"
@edit-request="editRequest($event)" @edit-request="editRequest($event)"
@select="$emit('select', $event)" @select="$emit('select', $event)"
@remove-request="removeRequest" @remove-request="$emit('remove-request', $event)"
/> />
</li> </li>
</ul> </ul>
@@ -253,13 +253,6 @@ export default {
}) })
this.syncCollections() this.syncCollections()
}, },
removeRequest({ collectionIndex, folderName, requestIndex }) {
this.$emit("remove-request", {
collectionIndex,
folderName,
requestIndex,
})
},
}, },
} }
</script> </script>

View File

@@ -47,7 +47,12 @@
v-close-popover v-close-popover
class="icon" class="icon"
@click=" @click="
$emit('edit-folder', { folder, folderIndex, collectionIndex }) $emit('edit-folder', {
folder,
folderIndex,
collectionIndex,
folderPath,
})
" "
> >
<i class="material-icons">edit</i> <i class="material-icons">edit</i>
@@ -137,8 +142,8 @@
</template> </template>
<script> <script>
import { fb } from "~/helpers/fb"
import { getSettingSubject } from "~/newstore/settings" import { getSettingSubject } from "~/newstore/settings"
import { removeRESTFolder, removeRESTRequest } from "~/newstore/collections"
export default { export default {
name: "Folder", name: "Folder",
@@ -177,14 +182,6 @@ export default {
}, },
}, },
methods: { methods: {
syncCollections() {
if (fb.currentUser !== null && this.SYNC_COLLECTIONS) {
fb.writeCollections(
JSON.parse(JSON.stringify(this.$store.state.postwoman.collections)),
"collections"
)
}
},
toggleShowChildren() { toggleShowChildren() {
if (this.$props.saveRequest) if (this.$props.saveRequest)
this.$emit("select", { this.$emit("select", {
@@ -198,13 +195,8 @@ export default {
this.showChildren = !this.showChildren this.showChildren = !this.showChildren
}, },
removeFolder() { removeFolder() {
this.$store.commit("postwoman/removeFolder", { removeRESTFolder(this.folderPath)
collectionIndex: this.$props.collectionIndex,
folderName: this.$props.folder.name,
folderIndex: this.$props.folderIndex,
flag: "rest",
})
this.syncCollections()
this.$toast.error(this.$t("deleted"), { this.$toast.error(this.$t("deleted"), {
icon: "delete", icon: "delete",
}) })
@@ -217,6 +209,8 @@ export default {
const requestIndex = dataTransfer.getData("requestIndex") const requestIndex = dataTransfer.getData("requestIndex")
const flag = "rest" const flag = "rest"
// TODO: Implement for the new collection state system
this.$store.commit("postwoman/moveRequest", { this.$store.commit("postwoman/moveRequest", {
oldCollectionIndex, oldCollectionIndex,
newCollectionIndex: this.$props.collectionIndex, newCollectionIndex: this.$props.collectionIndex,
@@ -229,12 +223,8 @@ export default {
}) })
this.syncCollections() this.syncCollections()
}, },
removeRequest({ collectionIndex, folderName, requestIndex }) { removeRequest({ requestIndex }) {
this.$emit("remove-request", { removeRESTRequest(this.folderPath, requestIndex)
collectionIndex,
folderName,
requestIndex,
})
}, },
}, },
} }

View File

@@ -43,6 +43,7 @@
folderName, folderName,
request, request,
requestIndex, requestIndex,
folderPath,
}) })
" "
> >
@@ -132,6 +133,7 @@ export default {
this.$emit("remove-request", { this.$emit("remove-request", {
collectionIndex: this.$props.collectionIndex, collectionIndex: this.$props.collectionIndex,
folderName: this.$props.folderName, folderName: this.$props.folderName,
folderPath: this.folderPath,
requestIndex: this.$props.requestIndex, requestIndex: this.$props.requestIndex,
}) })
}, },

View File

@@ -44,7 +44,12 @@
v-close-popover v-close-popover
class="icon" class="icon"
@click=" @click="
$emit('edit-folder', { folder, folderIndex, collectionIndex }) $emit('edit-folder', {
folder,
folderIndex,
collectionIndex,
folderPath: '',
})
" "
> >
<i class="material-icons">edit</i> <i class="material-icons">edit</i>

View File

@@ -10,6 +10,12 @@ import {
setGraphqlHistoryEntries, setGraphqlHistoryEntries,
HISTORY_LIMIT, HISTORY_LIMIT,
} from "~/newstore/history" } from "~/newstore/history"
import {
restCollectionStore,
setRESTCollections,
graphqlCollectionStore,
setGraphqlCollections,
} from "~/newstore/collections"
// Initialize Firebase, copied from cloud console // Initialize Firebase, copied from cloud console
const firebaseConfig = { const firebaseConfig = {
@@ -39,8 +45,6 @@ export class FirebaseInstance {
this.idToken = null this.idToken = null
this.currentFeeds = [] this.currentFeeds = []
this.currentSettings = [] this.currentSettings = []
this.currentCollections = []
this.currentGraphqlCollections = []
this.currentEnvironments = [] this.currentEnvironments = []
this.currentUser$ = new ReplaySubject(1) this.currentUser$ = new ReplaySubject(1)
@@ -49,6 +53,28 @@ export class FirebaseInstance {
let loadedSettings = false let loadedSettings = false
let loadedRESTHistory = false let loadedRESTHistory = false
let loadedGraphqlHistory = false let loadedGraphqlHistory = false
let loadedRESTCollections = false
let loadedGraphqlCollections = false
graphqlCollectionStore.subject$.subscribe(({ state }) => {
if (
loadedGraphqlCollections &&
this.currentUser &&
settingsStore.value.syncCollections
) {
this.writeCollections(state, "collectionsGraphql")
}
})
restCollectionStore.subject$.subscribe(({ state }) => {
if (
loadedRESTCollections &&
this.currentUser &&
settingsStore.value.syncCollections
) {
this.writeCollections(state, "collections")
}
})
restHistoryStore.dispatches$.subscribe((dispatch) => { restHistoryStore.dispatches$.subscribe((dispatch) => {
if ( if (
@@ -87,15 +113,17 @@ export class FirebaseInstance {
}) })
settingsStore.dispatches$.subscribe((dispatch) => { settingsStore.dispatches$.subscribe((dispatch) => {
if ( if (this.currentSettings && loadedSettings) {
this.currentSettings && if (dispatch.dispatcher === "bulkApplySettings") {
loadedSettings && Object.keys(dispatch.payload).forEach((key) => {
dispatch.dispatcher !== "applySettingFB" this.writeSettings(key, dispatch.payload[key])
) { })
this.writeSettings( } else if (dispatch.dispatcher !== "applySettingFB") {
dispatch.payload.settingKey, this.writeSettings(
settingsStore.value[dispatch.payload.settingKey] dispatch.payload.settingKey,
) settingsStore.value[dispatch.payload.settingKey]
)
}
} }
}) })
@@ -220,9 +248,17 @@ export class FirebaseInstance {
collection.id = doc.id collection.id = doc.id
collections.push(collection) collections.push(collection)
}) })
// Prevent infinite ping-pong of updates
loadedRESTCollections = false
// TODO: Wth is with collections[0]
if (collections.length > 0) { if (collections.length > 0) {
this.currentCollections = collections[0].collection console.log(collections[0].collection)
setRESTCollections(collections[0].collection)
} }
loadedRESTCollections = true
}) })
this.usersCollection this.usersCollection
@@ -235,9 +271,16 @@ export class FirebaseInstance {
collection.id = doc.id collection.id = doc.id
collections.push(collection) collections.push(collection)
}) })
// Prevent infinite ping-pong of updates
loadedGraphqlCollections = false
// TODO: Wth is with collections[0]
if (collections.length > 0) { if (collections.length > 0) {
this.currentGraphqlCollections = collections[0].collection setGraphqlCollections(collections[0].collection)
} }
loadedGraphqlCollections = true
}) })
this.usersCollection this.usersCollection
@@ -320,6 +363,7 @@ export class FirebaseInstance {
} }
async writeSettings(setting, value) { async writeSettings(setting, value) {
console.log(setting)
const st = { const st = {
updatedOn: new Date(), updatedOn: new Date(),
author: this.currentUser.uid, author: this.currentUser.uid,
@@ -458,6 +502,7 @@ export class FirebaseInstance {
.set(cl) .set(cl)
} catch (e) { } catch (e) {
console.error("error updating", cl, e) console.error("error updating", cl, e)
console.log(collection)
throw e throw e
} }

579
newstore/collections.ts Normal file
View File

@@ -0,0 +1,579 @@
import { pluck } from "rxjs/operators"
import DispatchingStore, { defineDispatchers } from "./DispatchingStore"
interface Collection {
name: string
folders: Collection[]
requests: any[]
}
const defaultRESTCollectionState = {
state: [
<Collection>{
name: "My Collection",
folders: [],
requests: [],
},
],
}
const defaultGraphqlCollectionState = {
state: [
<Collection>{
name: "My GraphQL Collection",
folders: [],
requests: [],
},
],
}
type RESTCollectionStoreType = typeof defaultRESTCollectionState
type GraphqlCollectionStoreType = typeof defaultGraphqlCollectionState
type CollectionStoreType = RESTCollectionStoreType | GraphqlCollectionStoreType
function navigateToFolderWithIndexPath(
collections: Collection[],
indexPaths: number[]
) {
if (indexPaths.length === 0) return null
let target = collections[indexPaths.shift() as number]
while (indexPaths.length > 0)
target = target.folders[indexPaths.shift() as number]
return target !== undefined ? target : null
}
const collectionDispatchers = defineDispatchers({
setCollections(
_: CollectionStoreType,
{ entries }: { entries: Collection[] }
) {
return {
state: entries,
}
},
appendCollections(
{ state }: CollectionStoreType,
{ entries }: { entries: Collection[] }
) {
return {
state: [...state, ...entries],
}
},
addCollection(
{ state }: CollectionStoreType,
{ collection }: { collection: Collection }
) {
return {
state: [...state, collection],
}
},
removeCollection(
{ state }: CollectionStoreType,
{ collectionIndex }: { collectionIndex: number }
) {
return {
state: state.filter((_, i) => i !== collectionIndex),
}
},
editCollection(
{ state }: CollectionStoreType,
{
collectionIndex,
collection,
}: { collectionIndex: number; collection: Collection }
) {
return {
state: state.map((col, index) =>
index === collectionIndex ? collection : col
),
}
},
addFolder(
{ state }: CollectionStoreType,
{ name, path }: { name: string; path: string }
) {
const newFolder: Collection = {
name,
folders: [],
requests: [],
}
const newState = state
const indexPaths = path.split("/").map((x) => parseInt(x))
const target = navigateToFolderWithIndexPath(newState, indexPaths)
if (target === null) {
console.log(`Could not parse path '${path}'. Ignoring add folder request`)
return {}
}
target.folders.push(newFolder)
return {
state: newState,
}
},
editFolder(
{ state }: CollectionStoreType,
{ path, folder }: { path: string; folder: string }
) {
const newState = state
const indexPaths = path.split("/").map((x) => parseInt(x))
const target = navigateToFolderWithIndexPath(newState, indexPaths)
if (target === null) {
console.log(
`Could not parse path '${path}'. Ignoring edit folder request`
)
return {}
}
Object.assign(target, folder)
return {
state: newState,
}
},
removeFolder({ state }: CollectionStoreType, { path }: { path: string }) {
const newState = state
const indexPaths = path.split("/").map((x) => parseInt(x))
if (indexPaths.length === 0) {
console.log(
"Given path too short. If this is a collection, use removeCollection dispatcher instead. Skipping request."
)
return {}
}
// We get the index path to the folder itself,
// we have to find the folder containing the target folder,
// so we pop the last path index
const folderIndex = indexPaths.pop() as number
const containingFolder = navigateToFolderWithIndexPath(newState, indexPaths)
if (containingFolder === null) {
console.log(
`Could not resolve path '${path}'. Skipping removeFolder dispatch.`
)
return {}
}
containingFolder.folders.splice(folderIndex, 1)
return {
state: newState,
}
},
editRequest(
{ state }: CollectionStoreType,
{
path,
requestIndex,
requestNew,
}: { path: string; requestIndex: number; requestNew: any }
) {
const newState = state
const indexPaths = path.split("/").map((x) => parseInt(x))
const targetLocation = navigateToFolderWithIndexPath(newState, indexPaths)
if (targetLocation === null) {
console.log(
`Could not resolve path '${path}'. Ignoring editRequest dispatch.`
)
return {}
}
targetLocation.requests = targetLocation.requests.map((req, index) =>
index !== requestIndex ? req : requestNew
)
return {
state: newState,
}
},
saveRequestAs(
{ state }: CollectionStoreType,
{ path, request }: { path: string; request: any }
) {
const newState = state
const indexPaths = path.split("/").map((x) => parseInt(x))
const targetLocation = navigateToFolderWithIndexPath(newState, indexPaths)
if (targetLocation === null) {
console.log(
`Could not resolve path '${path}'. Ignoring saveRequestAs dispatch.`
)
return {}
}
targetLocation.requests.push(request)
return {
state: newState,
}
},
removeRequest(
{ state }: CollectionStoreType,
{ path, requestIndex }: { path: string; requestIndex: number }
) {
const newState = state
const indexPaths = path.split("/").map((x) => parseInt(x))
const targetLocation = navigateToFolderWithIndexPath(newState, indexPaths)
if (targetLocation === null) {
console.log(
`Could not resolve path '${path}'. Ignoring removeRequest dispatch.`
)
return {}
}
targetLocation.requests.splice(requestIndex, 1)
return {
state: newState,
}
},
moveRequest(
{ state }: CollectionStoreType,
{
path,
requestIndex,
destinationPath,
}: { path: string; requestIndex: number; destinationPath: string }
) {
const newState = state
const indexPaths = path.split("/").map((x) => parseInt(x))
const targetLocation = navigateToFolderWithIndexPath(newState, indexPaths)
if (targetLocation === null) {
console.log(
`Could not resolve source path '${path}'. Skipping moveRequest dispatch.`
)
return {}
}
const req = targetLocation.requests[requestIndex]
const destIndexPaths = destinationPath.split("/").map((x) => parseInt(x))
const destLocation = navigateToFolderWithIndexPath(newState, destIndexPaths)
if (destLocation === null) {
console.log(
`Could not resolve destination path '${destinationPath}'. Skipping moveRequest dispatch.`
)
return {}
}
destLocation.requests.push(req)
targetLocation.requests.splice(requestIndex, 1)
return {
state: newState,
}
},
})
export const restCollectionStore = new DispatchingStore(
defaultRESTCollectionState,
collectionDispatchers
)
export const graphqlCollectionStore = new DispatchingStore(
defaultGraphqlCollectionState,
collectionDispatchers
)
export function setRESTCollections(entries: Collection[]) {
restCollectionStore.dispatch({
dispatcher: "setCollections",
payload: {
entries,
},
})
}
export const restCollections$ = restCollectionStore.subject$.pipe(
pluck("state")
)
export const graphqlCollections$ = graphqlCollectionStore.subject$.pipe(
pluck("state")
)
export function appendRESTCollections(entries: Collection[]) {
restCollectionStore.dispatch({
dispatcher: "appendCollections",
payload: {
entries,
},
})
}
export function addRESTCollection(collection: Collection) {
restCollectionStore.dispatch({
dispatcher: "addCollection",
payload: {
collection,
},
})
}
export function removeRESTCollection(collectionIndex: number) {
restCollectionStore.dispatch({
dispatcher: "removeCollection",
payload: {
collectionIndex,
},
})
}
export function editRESTCollection(
collectionIndex: number,
collection: Collection
) {
restCollectionStore.dispatch({
dispatcher: "editCollection",
payload: {
collectionIndex,
collection,
},
})
}
export function addRESTFolder(name: string, path: string) {
restCollectionStore.dispatch({
dispatcher: "addFolder",
payload: {
name,
path,
},
})
}
export function editRESTFolder(path: string, folder: Collection) {
restCollectionStore.dispatch({
dispatcher: "editFolder",
payload: {
path,
folder,
},
})
}
export function removeRESTFolder(path: string) {
restCollectionStore.dispatch({
dispatcher: "removeFolder",
payload: {
path,
},
})
}
export function editRESTRequest(
path: string,
requestIndex: number,
requestNew: any
) {
restCollectionStore.dispatch({
dispatcher: "editRequest",
payload: {
path,
requestIndex,
requestNew,
},
})
}
export function saveRESTRequestAs(path: string, request: any) {
restCollectionStore.dispatch({
dispatcher: "saveRequestAs",
payload: {
path,
request,
},
})
}
export function removeRESTRequest(path: string, requestIndex: number) {
restCollectionStore.dispatch({
dispatcher: "removeRequest",
payload: {
path,
requestIndex,
},
})
}
export function moveRESTRequest(
path: string,
requestIndex: number,
destinationPath: string
) {
restCollectionStore.dispatch({
dispatcher: "moveRequest",
payload: {
path,
requestIndex,
destinationPath,
},
})
}
export function setGraphqlCollections(entries: Collection[]) {
graphqlCollectionStore.dispatch({
dispatcher: "setCollections",
payload: {
entries,
},
})
}
export function appendGraphqlCollections(entries: Collection[]) {
graphqlCollectionStore.dispatch({
dispatcher: "appendCollections",
payload: {
entries,
},
})
}
export function addGraphqlCollection(collection: Collection) {
graphqlCollectionStore.dispatch({
dispatcher: "addCollection",
payload: {
collection,
},
})
}
export function removeGraphqlCollection(collectionIndex: number) {
graphqlCollectionStore.dispatch({
dispatcher: "removeCollection",
payload: {
collectionIndex,
},
})
}
export function editGraphqlCollection(
collectionIndex: number,
collection: Collection
) {
graphqlCollectionStore.dispatch({
dispatcher: "editCollection",
payload: {
collectionIndex,
collection,
},
})
}
export function addGraphqlFolder(name: string, path: string) {
graphqlCollectionStore.dispatch({
dispatcher: "addFolder",
payload: {
name,
path,
},
})
}
export function editGraphqlFolder(path: string, folder: Collection) {
graphqlCollectionStore.dispatch({
dispatcher: "editFolder",
payload: {
path,
folder,
},
})
}
export function removeGraphqlFolder(path: string) {
graphqlCollectionStore.dispatch({
dispatcher: "removeFolder",
payload: {
path,
},
})
}
export function editGraphqlRequest(
path: string,
requestIndex: number,
requestNew: any
) {
graphqlCollectionStore.dispatch({
dispatcher: "editRequest",
payload: {
path,
requestIndex,
requestNew,
},
})
}
export function saveGraphqlRequestAs(path: string, request: any) {
graphqlCollectionStore.dispatch({
dispatcher: "saveRequestAs",
payload: {
path,
request,
},
})
}
export function removeGraphqlRequest(path: string, requestIndex: number) {
graphqlCollectionStore.dispatch({
dispatcher: "removeRequest",
payload: {
path,
requestIndex,
},
})
}
export function moveGraphqlRequest(
path: string,
requestIndex: number,
destinationPath: string
) {
graphqlCollectionStore.dispatch({
dispatcher: "moveRequest",
payload: {
path,
requestIndex,
destinationPath,
},
})
}

View File

@@ -8,6 +8,12 @@ import {
setRESTHistoryEntries, setRESTHistoryEntries,
setGraphqlHistoryEntries, setGraphqlHistoryEntries,
} from "./history" } from "./history"
import {
restCollectionStore,
graphqlCollectionStore,
setGraphqlCollections,
setRESTCollections,
} from "./collections"
function checkAndMigrateOldSettings() { function checkAndMigrateOldSettings() {
const vuexData = JSON.parse(window.localStorage.getItem("vuex") || "{}") const vuexData = JSON.parse(window.localStorage.getItem("vuex") || "{}")
@@ -22,6 +28,22 @@ function checkAndMigrateOldSettings() {
delete vuexData.postwoman.settings delete vuexData.postwoman.settings
window.localStorage.setItem("vuex", JSON.stringify(vuexData)) window.localStorage.setItem("vuex", JSON.stringify(vuexData))
} }
if (vuexData.postwoman && vuexData.postwoman.collections) {
const restColls = vuexData.postwoman.collections
window.localStorage.setItem("collections", JSON.stringify(restColls))
delete vuexData.postwoman.collections
window.localStorage.setItem("vuex", JSON.stringify(vuexData))
}
if (vuexData.postwoman && vuexData.postwoman.collectionsGraphql) {
const gqlColls = vuexData.postwoman.collectionsGraphql
window.localStorage.setItem("collectionsGraphql", JSON.stringify(gqlColls))
delete vuexData.postwoman.collectionsGraphql
window.localStorage.setItem("vuex", JSON.stringify(vuexData))
}
} }
function setupSettingsPersistence() { function setupSettingsPersistence() {
@@ -59,9 +81,31 @@ function setupHistoryPersistence() {
}) })
} }
function setupCollectionsPersistence() {
const restCollectionData = JSON.parse(
window.localStorage.getItem("collections") || "[]"
)
const graphqlCollectionData = JSON.parse(
window.localStorage.getItem("collectionsGraphql") || "[]"
)
setRESTCollections(restCollectionData)
setGraphqlCollections(graphqlCollectionData)
restCollectionStore.subject$.subscribe(({ state }) => {
window.localStorage.setItem("collections", JSON.stringify(state))
})
graphqlCollectionStore.subject$.subscribe(({ state }) => {
window.localStorage.setItem("collectionsGraphql", JSON.stringify(state))
})
}
export function setupLocalPersistence() { export function setupLocalPersistence() {
checkAndMigrateOldSettings() checkAndMigrateOldSettings()
setupSettingsPersistence() setupSettingsPersistence()
setupHistoryPersistence() setupHistoryPersistence()
setupCollectionsPersistence()
} }

View File

@@ -333,6 +333,7 @@ export default Vue.extend({
1000 1000
) )
}, },
// TODO: Use the new collection store
syncCollections(): void { syncCollections(): void {
if (fb.currentUser !== null && this.SYNC_COLLECTIONS) { if (fb.currentUser !== null && this.SYNC_COLLECTIONS) {
if (this.$store.state.postwoman.collections) if (this.$store.state.postwoman.collections)

View File

@@ -42,20 +42,6 @@ export const SETTINGS_KEYS = [
export const state = () => ({ export const state = () => ({
settings: {}, settings: {},
collections: [
{
name: "My Collection",
folders: [],
requests: [],
},
],
collectionsGraphql: [
{
name: "My GraphQL Collection",
folders: [],
requests: [],
},
],
environments: [ environments: [
{ {
name: "My Environment Variables", name: "My Environment Variables",