Skip to content

Open-Source API Development Ecosystem • https://hoppscotch.io • Offline, On-Prem & Cloud • Web, Desktop & CLI • Open-Source Alternative to Postman, Insomnia

feat: unsaved change popup (#2239)

Nivedin99148a0a0e94
diff --git a/packages/hoppscotch-app/.gitignore b/packages/hoppscotch-app/.gitignore
index a3ab74b17..7c831b208 100644
--- a/packages/hoppscotch-app/.gitignore
+++ b/packages/hoppscotch-app/.gitignore
@@ -115,4 +115,4 @@ shims-volar.d.ts
helpers/backend/backend-schema.json
# GraphQL Type Generation
-helpers/backend/graphql.ts
\ No newline at end of file
+helpers/backend/graphql.ts
diff --git a/packages/hoppscotch-app/components/collections/my/Request.vue b/packages/hoppscotch-app/components/collections/my/Request.vue
index 41ee80cd1..abeb983e3 100644
--- a/packages/hoppscotch-app/components/collections/my/Request.vue
+++ b/packages/hoppscotch-app/components/collections/my/Request.vue
@@ -32,15 +32,10 @@
{{ request.name }}
</span>
<span
- v-if="
- active &&
- active.originLocation === 'user-collection' &&
- active.folderPath === folderPath &&
- active.requestIndex === requestIndex
- "
+ v-if="isActive"
v-tippy="{ theme: 'tooltip' }"
class="relative h-1.5 w-1.5 flex flex-shrink-0 mx-3"
- :title="`${$t('collection.request_in_use')}`"
+ :title="`${t('collection.request_in_use')}`"
>
<span
class="absolute inline-flex flex-shrink-0 w-full h-full bg-green-500 rounded-full opacity-75 animate-ping"
@@ -56,7 +51,7 @@
v-if="!saveRequest && !doc"
v-tippy="{ theme: 'tooltip' }"
svg="rotate-ccw"
- :title="$t('action.restore')"
+ :title="t('action.restore')"
class="hidden group-hover:inline-flex"
@click.native="!doc ? selectRequest() : {}"
/>
@@ -72,7 +67,7 @@
<template #trigger>
<ButtonSecondary
v-tippy="{ theme: 'tooltip' }"
- :title="$t('action.more')"
+ :title="t('action.more')"
svg="more-vertical"
/>
</template>
@@ -89,11 +84,11 @@
<SmartItem
ref="edit"
svg="edit"
- :label="$t('action.edit')"
+ :label="t('action.edit')"
:shortcut="['E']"
@click.native="
() => {
- $emit('edit-request', {
+ emit('edit-request', {
collectionIndex,
folderIndex,
folderName,
@@ -112,7 +107,7 @@
:shortcut="['D']"
@click.native="
() => {
- $emit('duplicate-request', {
+ emit('duplicate-request', {
collectionIndex,
folderIndex,
folderName,
@@ -127,7 +122,7 @@
<SmartItem
ref="deleteAction"
svg="trash-2"
- :label="$t('action.delete')"
+ :label="t('action.delete')"
:shortcut="['⌫']"
@click.native="
() => {
@@ -143,129 +138,312 @@
</div>
<SmartConfirmModal
:show="confirmRemove"
- :title="$t('confirm.remove_request')"
+ :title="t('confirm.remove_request')"
@hide-modal="confirmRemove = false"
@resolve="removeRequest"
/>
+ <HttpReqChangeConfirmModal
+ :show="confirmChange"
+ @hide-modal="confirmChange = false"
+ @save-change="saveRequestChange"
+ @discard-change="discardRequestChange"
+ />
+ <CollectionsSaveRequest
+ mode="rest"
+ :show="showSaveRequestModal"
+ @hide-modal="showSaveRequestModal = false"
+ />
</div>
</template>
-<script lang="ts">
-import { defineComponent, ref } from "@nuxtjs/composition-api"
+<script setup lang="ts">
+import { ref, computed } from "@nuxtjs/composition-api"
import {
+ HoppRESTRequest,
safelyExtractRESTRequest,
translateToNewRequest,
} from "@hoppscotch/data"
-import { useReadonlyStream } from "~/helpers/utils/composables"
+import isEqual from "lodash/isEqual"
+import * as E from "fp-ts/Either"
+import {
+ useI18n,
+ useToast,
+ useReadonlyStream,
+} from "~/helpers/utils/composables"
import {
getDefaultRESTRequest,
+ getRESTRequest,
restSaveContext$,
setRESTRequest,
setRESTSaveContext,
+ getRESTSaveContext,
} from "~/newstore/RESTSession"
+import { editRESTRequest } from "~/newstore/collections"
+import { runMutation } from "~/helpers/backend/GQLClient"
+import { UpdateRequestDocument } from "~/helpers/backend/graphql"
+import { HoppRequestSaveContext } from "~/helpers/types/HoppRequestSaveContext"
-export default defineComponent({
- props: {
- request: { type: Object, default: () => {} },
- collectionIndex: { type: Number, default: null },
- folderIndex: { type: Number, default: null },
- folderName: { type: String, default: null },
- // eslint-disable-next-line vue/require-default-prop
- requestIndex: [Number, String],
- doc: Boolean,
- saveRequest: Boolean,
- collectionsType: { type: Object, default: () => {} },
- folderPath: { type: String, default: null },
- picked: { type: Object, default: () => {} },
- },
- setup() {
- const active = useReadonlyStream(restSaveContext$, null)
- return {
- active,
- tippyActions: ref<any | null>(null),
- options: ref<any | null>(null),
- edit: ref<any | null>(null),
- duplicate: ref<any | null>(null),
- deleteAction: ref<any | null>(null),
- }
- },
- data() {
- return {
- dragging: false,
- requestMethodLabels: {
- get: "text-green-500",
- post: "text-yellow-500",
- put: "text-blue-500",
- delete: "text-red-500",
- default: "text-gray-500",
- },
- confirmRemove: false,
- }
- },
- computed: {
- isSelected(): boolean {
- return (
- this.picked &&
- this.picked.pickedType === "my-request" &&
- this.picked.folderPath === this.folderPath &&
- this.picked.requestIndex === this.requestIndex
- )
- },
- },
- methods: {
- selectRequest() {
- if (
- this.active &&
- this.active.originLocation === "user-collection" &&
- this.active.folderPath === this.folderPath &&
- this.active.requestIndex === this.requestIndex
- ) {
- setRESTSaveContext(null)
- return
- }
- if (this.$props.saveRequest)
- this.$emit("select", {
+const props = defineProps<{
+ request: HoppRESTRequest
+ collectionIndex: number
+ folderIndex: number
+ folderName: string
+ requestIndex: number
+ doc: boolean
+ saveRequest: boolean
+ collectionsType: object
+ folderPath: string
+ picked?: {
+ pickedType: string
+ collectionIndex: number
+ folderPath: string
+ folderName: string
+ requestIndex: number
+ }
+}>()
+
+const emit = defineEmits<{
+ (
+ e: "select",
+ data:
+ | {
picked: {
- pickedType: "my-request",
- collectionIndex: this.collectionIndex,
- folderPath: this.folderPath,
- folderName: this.folderName,
- requestIndex: this.requestIndex,
- },
- })
- else {
- setRESTRequest(
- safelyExtractRESTRequest(
- translateToNewRequest(this.request),
- getDefaultRESTRequest()
- ),
- {
- originLocation: "user-collection",
- folderPath: this.folderPath,
- requestIndex: this.requestIndex,
+ pickedType: string
+ collectionIndex: number
+ folderPath: string
+ folderName: string
+ requestIndex: number
}
- )
- }
- },
- dragStart({ dataTransfer }) {
- this.dragging = !this.dragging
- dataTransfer.setData("folderPath", this.folderPath)
- dataTransfer.setData("requestIndex", this.requestIndex)
- },
- removeRequest() {
- this.$emit("remove-request", {
- collectionIndex: this.$props.collectionIndex,
- folderName: this.$props.folderName,
- folderPath: this.folderPath,
- requestIndex: this.$props.requestIndex,
+ }
+ | undefined
+ ): void
+
+ (
+ e: "remove-request",
+ data: {
+ collectionIndex: number
+ folderName: string
+ folderPath: string
+ requestIndex: number
+ }
+ ): void
+
+ (
+ e: "duplicate-request",
+ data: {
+ collectionIndex: number
+ folderIndex: number
+ folderName: string
+ request: HoppRESTRequest
+ folderPath: string
+ requestIndex: number
+ }
+ ): void
+
+ (
+ e: "edit-request",
+ data: {
+ collectionIndex: number
+ folderIndex: number
+ folderName: string
+ request: HoppRESTRequest
+ folderPath: string
+ requestIndex: number
+ }
+ ): void
+}>()
+
+const t = useI18n()
+const toast = useToast()
+
+const dragging = ref(false)
+const requestMethodLabels = {
+ get: "text-green-500",
+ post: "text-yellow-500",
+ put: "text-blue-500",
+ delete: "text-red-500",
+ default: "text-gray-500",
+}
+const confirmRemove = ref(false)
+const confirmChange = ref(false)
+const showSaveRequestModal = ref(false)
+
+// Template refs
+const tippyActions = ref<any | null>(null)
+const options = ref<any | null>(null)
+const edit = ref<any | null>(null)
+const duplicate = ref<any | null>(null)
+const deleteAction = ref<any | null>(null)
+
+const active = useReadonlyStream(restSaveContext$, null)
+
+const isSelected = computed(
+ () =>
+ props.picked &&
+ props.picked.pickedType === "my-request" &&
+ props.picked.folderPath === props.folderPath &&
+ props.picked.requestIndex === props.requestIndex
+)
+
+const isActive = computed(
+ () =>
+ active.value &&
+ active.value.originLocation === "user-collection" &&
+ active.value.folderPath === props.folderPath &&
+ active.value.requestIndex === props.requestIndex
+)
+
+const dragStart = ({ dataTransfer }: DragEvent) => {
+ if (dataTransfer) {
+ dragging.value = !dragging.value
+ dataTransfer.setData("folderPath", props.folderPath)
+ dataTransfer.setData("requestIndex", props.requestIndex.toString())
+ }
+}
+
+const removeRequest = () => {
+ emit("remove-request", {
+ collectionIndex: props.collectionIndex,
+ folderName: props.folderName,
+ folderPath: props.folderPath,
+ requestIndex: props.requestIndex,
+ })
+}
+
+const getRequestLabelColor = (method: string) =>
+ requestMethodLabels[
+ method.toLowerCase() as keyof typeof requestMethodLabels
+ ] || requestMethodLabels.default
+
+const setRestReq = (request: any) => {
+ setRESTRequest(
+ safelyExtractRESTRequest(
+ translateToNewRequest(request),
+ getDefaultRESTRequest()
+ ),
+ {
+ originLocation: "user-collection",
+ folderPath: props.folderPath,
+ requestIndex: props.requestIndex,
+ req: request,
+ }
+ )
+}
+
+const selectRequest = () => {
+ if (!active.value) {
+ confirmChange.value = true
+
+ if (props.saveRequest)
+ emit("select", {
+ picked: {
+ pickedType: "my-request",
+ collectionIndex: props.collectionIndex,
+ folderPath: props.folderPath,
+ folderName: props.folderName,
+ requestIndex: props.requestIndex,
+ },
})
- },
- getRequestLabelColor(method: string): string {
- return (
- this.requestMethodLabels[method.toLowerCase()] ||
- this.requestMethodLabels.default
+ } else {
+ const currentReqWithNoChange = active.value.req
+ const currentFullReq = getRESTRequest()
+
+ // Check if whether user clicked the same request or not
+ if (!isActive.value) {
+ // Check if there is any changes done on the current request
+ if (isEqual(currentReqWithNoChange, currentFullReq)) {
+ setRestReq(props.request)
+ if (props.saveRequest)
+ emit("select", {
+ picked: {
+ pickedType: "my-request",
+ collectionIndex: props.collectionIndex,
+ folderPath: props.folderPath,
+ folderName: props.folderName,
+ requestIndex: props.requestIndex,
+ },
+ })
+ } else {
+ confirmChange.value = true
+ }
+ } else {
+ setRESTSaveContext(null)
+ }
+ }
+}
+
+/** Save current request to the collection */
+const saveRequestChange = () => {
+ const saveCtx = getRESTSaveContext()
+ saveCurrentRequest(saveCtx)
+ confirmChange.value = false
+}
+
+/** Discard changes and change the current request and context */
+const discardRequestChange = () => {
+ setRestReq(props.request)
+ if (props.saveRequest)
+ emit("select", {
+ picked: {
+ pickedType: "my-request",
+ collectionIndex: props.collectionIndex,
+ folderPath: props.folderPath,
+ folderName: props.folderName,
+ requestIndex: props.requestIndex,
+ },
+ })
+ if (!isActive.value) {
+ setRESTSaveContext({
+ originLocation: "user-collection",
+ folderPath: props.folderPath,
+ requestIndex: props.requestIndex,
+ req: props.request,
+ })
+ }
+
+ confirmChange.value = false
+}
+
+const saveCurrentRequest = (saveCtx: HoppRequestSaveContext | null) => {
+ if (!saveCtx) {
+ showSaveRequestModal.value = true
+ return
+ }
+ if (saveCtx.originLocation === "user-collection") {
+ try {
+ editRESTRequest(
+ saveCtx.folderPath,
+ saveCtx.requestIndex,
+ getRESTRequest()
)
- },
- },
-})
+ setRestReq(props.request)
+ toast.success(`${t("request.saved")}`)
+ } catch (e) {
+ setRESTSaveContext(null)
+ saveCurrentRequest(saveCtx)
+ }
+ } else if (saveCtx.originLocation === "team-collection") {
+ const req = getRESTRequest()
+ try {
+ runMutation(UpdateRequestDocument, {
+ requestID: saveCtx.requestID,
+ data: {
+ title: req.name,
+ request: JSON.stringify(req),
+ },
+ })().then((result) => {
+ if (E.isLeft(result)) {
+ toast.error(`${t("profile.no_permission")}`)
+ } else {
+ toast.success(`${t("request.saved")}`)
+ }
+ })
+ setRestReq(props.request)
+ } catch (error) {
+ showSaveRequestModal.value = true
+ toast.error(`${t("error.something_went_wrong")}`)
+ console.error(error)
+ }
+ }
+}
</script>
diff --git a/packages/hoppscotch-app/components/collections/teams/Request.vue b/packages/hoppscotch-app/components/collections/teams/Request.vue
index 5ac37e6e1..477e61e23 100644
--- a/packages/hoppscotch-app/components/collections/teams/Request.vue
+++ b/packages/hoppscotch-app/components/collections/teams/Request.vue
@@ -32,11 +32,7 @@
{{ request.name }}
</span>
<span
- v-if="
- active &&
- active.originLocation === 'team-collection' &&
- active.requestID === requestIndex
- "
+ v-if="isActive"
v-tippy="{ theme: 'tooltip' }"
class="relative h-1.5 w-1.5 flex flex-shrink-0 mx-3"
:title="`${$t('collection.request_in_use')}`"
@@ -93,7 +89,7 @@
:shortcut="['E']"
@click.native="
() => {
- $emit('edit-request', {
+ emit('edit-request', {
collectionIndex,
folderIndex,
folderName,
@@ -111,7 +107,7 @@
:shortcut="['D']"
@click.native="
() => {
- $emit('duplicate-request', {
+ emit('duplicate-request', {
request,
requestIndex,
collectionID,
@@ -143,116 +139,288 @@
@hide-modal="confirmRemove = false"
@resolve="removeRequest"
/>
+ <HttpReqChangeConfirmModal
+ :show="confirmChange"
+ @hide-modal="confirmChange = false"
+ @save-change="saveRequestChange"
+ @discard-change="discardRequestChange"
+ />
+ <CollectionsSaveRequest
+ mode="rest"
+ :show="showSaveRequestModal"
+ @hide-modal="showSaveRequestModal = false"
+ />
</div>
</template>
-<script lang="ts">
-import { defineComponent, ref } from "@nuxtjs/composition-api"
+<script setup lang="ts">
+import { ref, computed } from "@nuxtjs/composition-api"
import {
+ HoppRESTRequest,
safelyExtractRESTRequest,
translateToNewRequest,
} from "@hoppscotch/data"
-import { useReadonlyStream } from "~/helpers/utils/composables"
+import * as E from "fp-ts/Either"
+import isEqual from "lodash/isEqual"
+import {
+ useI18n,
+ useToast,
+ useReadonlyStream,
+} from "~/helpers/utils/composables"
import {
getDefaultRESTRequest,
restSaveContext$,
setRESTRequest,
setRESTSaveContext,
+ getRESTSaveContext,
+ getRESTRequest,
} from "~/newstore/RESTSession"
+import { editRESTRequest } from "~/newstore/collections"
+import { runMutation } from "~/helpers/backend/GQLClient"
+import { Team, UpdateRequestDocument } from "~/helpers/backend/graphql"
+import { HoppRequestSaveContext } from "~/helpers/types/HoppRequestSaveContext"
+
+const props = defineProps<{
+ request: HoppRESTRequest
+ collectionIndex: number
+ folderIndex: number
+ folderName?: string
+ requestIndex: string
+ doc: boolean
+ saveRequest: boolean
+ collectionsType: {
+ type: "my-collections" | "team-collections"
+ selectedTeam: Team | undefined
+ }
+ collectionID: string
+ picked?: {
+ pickedType: string
+ requestID: string
+ }
+}>()
+
+const emit = defineEmits<{
+ (
+ e: "select",
+ data:
+ | {
+ picked: {
+ pickedType: string
+ requestID: string
+ }
+ }
+ | undefined
+ ): void
-export default defineComponent({
- props: {
- request: { type: Object, default: () => {} },
- collectionIndex: { type: Number, default: null },
- folderIndex: { type: Number, default: null },
- folderName: { type: String, default: null },
- // eslint-disable-next-line vue/require-default-prop
- requestIndex: [Number, String],
- doc: Boolean,
- saveRequest: Boolean,
- collectionsType: { type: Object, default: () => {} },
- picked: { type: Object, default: () => {} },
- collectionID: { type: String, default: null },
- },
- setup() {
- const active = useReadonlyStream(restSaveContext$, null)
- return {
- active,
- tippyActions: ref<any | null>(null),
- options: ref<any | null>(null),
- edit: ref<any | null>(null),
- deleteAction: ref<any | null>(null),
- duplicate: ref<any | null>(null),
+ (
+ e: "remove-request",
+ data: {
+ collectionIndex: number
+ folderName: string | undefined
+ requestIndex: string
}
- },
- data() {
- return {
- dragging: false,
- requestMethodLabels: {
- get: "text-green-500",
- post: "text-yellow-500",
- put: "text-blue-500",
- delete: "text-red-500",
- default: "text-gray-500",
- },
- confirmRemove: false,
+ ): void
+
+ (
+ e: "edit-request",
+ data: {
+ collectionIndex: number
+ folderIndex: number
+ folderName: string | undefined
+ requestIndex: string
+ request: HoppRESTRequest
}
- },
- computed: {
- isSelected(): boolean {
- return (
- this.picked &&
- this.picked.pickedType === "teams-request" &&
- this.picked.requestID === this.requestIndex
- )
- },
- },
- methods: {
- selectRequest() {
- if (
- this.active &&
- this.active.originLocation === "team-collection" &&
- this.active.requestID === this.requestIndex
- ) {
- setRESTSaveContext(null)
- return
+ ): void
+
+ (
+ e: "duplicate-request",
+ data: {
+ collectionID: number | string
+ requestIndex: string
+ request: HoppRESTRequest
+ }
+ ): void
+}>()
+
+const t = useI18n()
+const toast = useToast()
+
+const dragging = ref(false)
+const requestMethodLabels = {
+ get: "text-green-500",
+ post: "text-yellow-500",
+ put: "text-blue-500",
+ delete: "text-red-500",
+ default: "text-gray-500",
+}
+const confirmRemove = ref(false)
+const confirmChange = ref(false)
+const showSaveRequestModal = ref(false)
+
+// Template refs
+const tippyActions = ref<any | null>(null)
+const options = ref<any | null>(null)
+const edit = ref<any | null>(null)
+const duplicate = ref<any | null>(null)
+const deleteAction = ref<any | null>(null)
+
+const active = useReadonlyStream(restSaveContext$, null)
+
+const isSelected = computed(
+ () =>
+ props.picked &&
+ props.picked.pickedType === "team-collection" &&
+ props.picked.requestID === props.requestIndex
+)
+
+const isActive = computed(
+ () =>
+ active.value &&
+ active.value.originLocation === "team-collection" &&
+ active.value.requestID === props.requestIndex &&
+ isEqual(active.value.req, props.request)
+)
+
+const dragStart = ({ dataTransfer }: DragEvent) => {
+ if (dataTransfer) {
+ dragging.value = !dragging.value
+ dataTransfer.setData("requestIndex", props.requestIndex)
+ }
+}
+
+const removeRequest = () => {
+ emit("remove-request", {
+ collectionIndex: props.collectionIndex,
+ folderName: props.folderName,
+ requestIndex: props.requestIndex,
+ })
+}
+
+const getRequestLabelColor = (method: string): string => {
+ return (
+ (requestMethodLabels as any)[method.toLowerCase()] ||
+ requestMethodLabels.default
+ )
+}
+
+const setRestReq = (request: HoppRESTRequest) => {
+ setRESTRequest(
+ safelyExtractRESTRequest(
+ translateToNewRequest(request),
+ getDefaultRESTRequest()
+ ),
+ {
+ originLocation: "team-collection",
+ requestID: props.requestIndex,
+ req: request,
+ }
+ )
+}
+
+const selectRequest = () => {
+ if (!active.value) {
+ confirmChange.value = true
+
+ if (props.saveRequest)
+ emit("select", {
+ picked: {
+ pickedType: "team-collection",
+ requestID: props.requestIndex,
+ },
+ })
+ } else {
+ const currentReqWithNoChange = active.value.req
+ const currentFullReq = getRESTRequest()
+
+ // Check if whether user clicked the same request or not
+ if (!isActive.value) {
+ // Check if there is any changes done on the current request
+ if (isEqual(currentReqWithNoChange, currentFullReq)) {
+ setRestReq(props.request)
+ if (props.saveRequest)
+ emit("select", {
+ picked: {
+ pickedType: "team-collection",
+ requestID: props.requestIndex,
+ },
+ })
+ } else {
+ confirmChange.value = true
}
- if (this.$props.saveRequest)
- this.$emit("select", {
- picked: {
- pickedType: "teams-request",
- requestID: this.requestIndex,
- },
- })
- else
- setRESTRequest(
- safelyExtractRESTRequest(
- translateToNewRequest(this.request),
- getDefaultRESTRequest()
- ),
- {
- originLocation: "team-collection",
- requestID: this.requestIndex as string,
- }
- )
- },
- dragStart({ dataTransfer }) {
- this.dragging = !this.dragging
- dataTransfer.setData("requestIndex", this.requestIndex)
- },
- removeRequest() {
- this.$emit("remove-request", {
- collectionIndex: this.$props.collectionIndex,
- folderName: this.$props.folderName,
- requestIndex: this.$props.requestIndex,
+ } else {
+ setRESTSaveContext(null)
+ }
+ }
+}
+
+/** Save current request to the collection */
+const saveRequestChange = () => {
+ const saveCtx = getRESTSaveContext()
+ saveCurrentRequest(saveCtx)
+ confirmChange.value = false
+}
+
+/** Discard changes and change the current request and context */
+const discardRequestChange = () => {
+ setRestReq(props.request)
+ if (props.saveRequest)
+ emit("select", {
+ picked: {
+ pickedType: "team-collection",
+ requestID: props.requestIndex,
+ },
+ })
+ if (!isActive.value) {
+ setRESTSaveContext({
+ originLocation: "team-collection",
+ requestID: props.requestIndex,
+ req: props.request,
+ })
+ }
+
+ confirmChange.value = false
+}
+
+const saveCurrentRequest = (saveCtx: HoppRequestSaveContext | null) => {
+ if (!saveCtx) {
+ showSaveRequestModal.value = true
+ return
+ }
+ if (saveCtx.originLocation === "team-collection") {
+ const req = getRESTRequest()
+ try {
+ runMutation(UpdateRequestDocument, {
+ requestID: saveCtx.requestID,
+ data: {
+ title: req.name,
+ request: JSON.stringify(req),
+ },
+ })().then((result) => {
+ if (E.isLeft(result)) {
+ toast.error(`${t("profile.no_permission")}`)
+ } else {
+ toast.success(`${t("request.saved")}`)
+ }
})
- },
- getRequestLabelColor(method: any) {
- return (
- (this.requestMethodLabels as any)[method.toLowerCase()] ||
- this.requestMethodLabels.default
+ setRestReq(props.request)
+ } catch (error) {
+ showSaveRequestModal.value = true
+ toast.error(`${t("error.something_went_wrong")}`)
+ console.error(error)
+ }
+ } else if (saveCtx.originLocation === "user-collection") {
+ try {
+ editRESTRequest(
+ saveCtx.folderPath,
+ saveCtx.requestIndex,
+ getRESTRequest()
)
- },
- },
-})
+ setRestReq(props.request)
+ toast.success(`${t("request.saved")}`)
+ } catch (e) {
+ setRESTSaveContext(null)
+ saveCurrentRequest(null)
+ }
+ }
+}
</script>
diff --git a/packages/hoppscotch-app/components/history/index.vue b/packages/hoppscotch-app/components/history/index.vue
index d14580f00..f757d4c79 100644
--- a/packages/hoppscotch-app/components/history/index.vue
+++ b/packages/hoppscotch-app/components/history/index.vue
@@ -97,16 +97,29 @@
@hide-modal="confirmRemove = false"
@resolve="clearHistory"
/>
+ <HttpReqChangeConfirmModal
+ :show="confirmChange"
+ @hide-modal="confirmChange = false"
+ @save-change="saveRequestChange"
+ @discard-change="discardRequestChange"
+ />
+ <CollectionsSaveRequest
+ mode="rest"
+ :show="showSaveRequestModal"
+ @hide-modal="showSaveRequestModal = false"
+ />
</div>
</template>
<script setup lang="ts">
import { computed, ref, Ref } from "@nuxtjs/composition-api"
-import { safelyExtractRESTRequest } from "@hoppscotch/data"
+import { HoppRESTRequest, safelyExtractRESTRequest } from "@hoppscotch/data"
import groupBy from "lodash/groupBy"
import { useTimeAgo } from "@vueuse/core"
import { pipe } from "fp-ts/function"
import * as A from "fp-ts/Array"
+import * as E from "fp-ts/Either"
+import isEqual from "lodash/isEqual"
import {
useI18n,
useReadonlyStream,
@@ -124,24 +137,39 @@ import {
RESTHistoryEntry,
GQLHistoryEntry,
} from "~/newstore/history"
-import { getDefaultRESTRequest, setRESTRequest } from "~/newstore/RESTSession"
+import {
+ getDefaultRESTRequest,
+ getRESTRequest,
+ getRESTSaveContext,
+ setRESTRequest,
+ setRESTSaveContext,
+} from "~/newstore/RESTSession"
+import { editRESTRequest } from "~/newstore/collections"
+import { runMutation } from "~/helpers/backend/GQLClient"
+import { UpdateRequestDocument } from "~/helpers/backend/graphql"
+import { HoppRequestSaveContext } from "~/helpers/types/HoppRequestSaveContext"
+
+type HistoryEntry = GQLHistoryEntry | RESTHistoryEntry
+
+type TimedHistoryEntry = {
+ entry: HistoryEntry
+ timeAgo: Ref<string>
+}
const props = defineProps<{
page: "rest" | "graphql"
}>()
+const toast = useToast()
+const t = useI18n()
+
const filterText = ref("")
const showMore = ref(false)
const confirmRemove = ref(false)
-const toast = useToast()
-const t = useI18n()
-type HistoryEntry = GQLHistoryEntry | RESTHistoryEntry
-
-type TimedHistoryEntry = {
- entry: HistoryEntry
- timeAgo: Ref<string>
-}
+const clickedHistory = ref<HistoryEntry | null>(null)
+const confirmChange = ref(false)
+const showSaveRequestModal = ref(false)
const history = useReadonlyStream<RESTHistoryEntry[] | GQLHistoryEntry[]>(
props.page === "rest" ? restHistory$ : graphqlHistory$,
@@ -199,11 +227,93 @@ const clearHistory = () => {
toast.success(`${t("state.history_deleted")}`)
}
-const useHistory = (entry: any) => {
- if (props.page === "rest")
- setRESTRequest(
- safelyExtractRESTRequest(entry.request, getDefaultRESTRequest())
- )
+const setRestReq = (request: HoppRESTRequest | null | undefined) => {
+ setRESTRequest(safelyExtractRESTRequest(request, getDefaultRESTRequest()))
+}
+
+const useHistory = (entry: HistoryEntry) => {
+ const currentFullReq = getRESTRequest()
+ // Initial state trigers a popup
+ if (!clickedHistory.value) {
+ clickedHistory.value = entry
+ confirmChange.value = true
+ return
+ }
+ // Checks if there are any change done in current request and the history request
+ if (!isEqual(currentFullReq, clickedHistory.value.request)) {
+ clickedHistory.value = entry
+ confirmChange.value = true
+ } else {
+ props.page === "rest" && setRestReq(entry.request as HoppRESTRequest)
+ clickedHistory.value = entry
+ }
+}
+
+/** Save current request to the collection */
+const saveRequestChange = () => {
+ const saveCtx = getRESTSaveContext()
+ saveCurrentRequest(saveCtx)
+ confirmChange.value = false
+}
+
+/** Discard changes and change the current request and remove the collection context */
+const discardRequestChange = () => {
+ const saveCtx = getRESTSaveContext()
+ if (saveCtx) {
+ setRESTSaveContext(null)
+ }
+ clickedHistory.value &&
+ setRestReq(clickedHistory.value.request as HoppRESTRequest)
+ confirmChange.value = false
+}
+
+const saveCurrentRequest = (saveCtx: HoppRequestSaveContext | null) => {
+ if (!saveCtx) {
+ showSaveRequestModal.value = true
+ return
+ }
+ if (saveCtx.originLocation === "user-collection") {
+ try {
+ editRESTRequest(
+ saveCtx.folderPath,
+ saveCtx.requestIndex,
+ getRESTRequest()
+ )
+ clickedHistory.value &&
+ setRestReq(clickedHistory.value.request as HoppRESTRequest)
+ setRESTSaveContext(null)
+ toast.success(`${t("request.saved")}`)
+ } catch (e) {
+ console.error(e)
+ setRESTSaveContext(null)
+ saveCurrentRequest(null)
+ }
+ } else if (saveCtx.originLocation === "team-collection") {
+ const req = getRESTRequest()
+ try {
+ runMutation(UpdateRequestDocument, {
+ requestID: saveCtx.requestID,
+ data: {
+ title: req.name,
+ request: JSON.stringify(req),
+ },
+ })().then((result) => {
+ if (E.isLeft(result)) {
+ toast.error(`${t("profile.no_permission")}`)
+ } else {
+ toast.success(`${t("request.saved")}`)
+ }
+ })
+ clickedHistory.value &&
+ setRestReq(clickedHistory.value.request as HoppRESTRequest)
+ setRESTSaveContext(null)
+ } catch (error) {
+ showSaveRequestModal.value = true
+ toast.error(`${t("error.something_went_wrong")}`)
+ console.error(error)
+ setRESTSaveContext(null)
+ }
+ }
}
const isRESTHistoryEntry = (
@@ -225,14 +335,16 @@ const deleteBatchHistoryEntry = (entries: TimedHistoryEntry[]) => {
toast.success(`${t("state.deleted")}`)
}
-const deleteHistory = (entry: any) => {
- if (props.page === "rest") deleteRESTHistoryEntry(entry)
- else deleteGraphqlHistoryEntry(entry)
+const deleteHistory = (entry: HistoryEntry) => {
+ if (props.page === "rest") deleteRESTHistoryEntry(entry as RESTHistoryEntry)
+ else deleteGraphqlHistoryEntry(entry as GQLHistoryEntry)
toast.success(`${t("state.deleted")}`)
}
-const toggleStar = (entry: any) => {
- if (props.page === "rest") toggleRESTHistoryEntryStar(entry)
- else toggleGraphqlHistoryEntryStar(entry)
+const toggleStar = (entry: HistoryEntry) => {
+ //History entry type specified because function does not know the type
+ if (props.page === "rest")
+ toggleRESTHistoryEntryStar(entry as RESTHistoryEntry)
+ else toggleGraphqlHistoryEntryStar(entry as GQLHistoryEntry)
}
</script>
diff --git a/packages/hoppscotch-app/components/http/ReqChangeConfirmModal.vue b/packages/hoppscotch-app/components/http/ReqChangeConfirmModal.vue
new file mode 100644
index 000000000..3e0376aaa
--- /dev/null
+++ b/packages/hoppscotch-app/components/http/ReqChangeConfirmModal.vue
@@ -0,0 +1,59 @@
+<template>
+ <SmartModal
+ v-if="show"
+ dialog
+ :title="$t('modal.confirm')"
+ aria-modal="true"
+ @close="hideModal"
+ >
+ <template #body>
+ <div class="flex flex-col px-2">
+ <label>
+ {{ t("confirm.request_change") }}
+ </label>
+ </div>
+ </template>
+ <template #footer>
+ <span>
+ <ButtonPrimary
+ v-focus
+ :label="t('action.save')"
+ @click.native="saveApiChange"
+ />
+ <ButtonSecondary
+ :label="t('action.dont_save')"
+ @click.native="discardApiChange"
+ />
+ </span>
+ <ButtonSecondary :label="t('action.cancel')" @click.native="hideModal" />
+ </template>
+ </SmartModal>
+</template>
+
+<script setup lang="ts">
+import { useI18n } from "~/helpers/utils/composables"
+
+const t = useI18n()
+
+defineProps<{
+ show: Boolean
+}>()
+
+const emit = defineEmits<{
+ (e: "save-change"): void
+ (e: "discard-change"): void
+ (e: "hide-modal"): void
+}>()
+
+const saveApiChange = () => {
+ emit("save-change")
+}
+
+const discardApiChange = () => {
+ emit("discard-change")
+}
+
+const hideModal = () => {
+ emit("hide-modal")
+}
+</script>
diff --git a/packages/hoppscotch-app/helpers/types/HoppRequestSaveContext.ts b/packages/hoppscotch-app/helpers/types/HoppRequestSaveContext.ts
index f80def03b..014b76b39 100644
--- a/packages/hoppscotch-app/helpers/types/HoppRequestSaveContext.ts
+++ b/packages/hoppscotch-app/helpers/types/HoppRequestSaveContext.ts
@@ -1,3 +1,5 @@
+import { HoppRESTRequest } from "@hoppscotch/data"
+
/**
* We use the save context to figure out
* how a loaded request is to be saved.
@@ -18,6 +20,10 @@ export type HoppRequestSaveContext =
* Index to the request
*/
requestIndex: number
+ /**
+ * Current request
+ */
+ req?: HoppRESTRequest
}
| {
/**
@@ -36,4 +42,8 @@ export type HoppRequestSaveContext =
* ID of the collection loaded
*/
collectionID?: string
+ /**
+ * Current request
+ */
+ req?: HoppRESTRequest
}
diff --git a/packages/hoppscotch-app/locales/en.json b/packages/hoppscotch-app/locales/en.json
index 58319455e..feff6b09e 100644
--- a/packages/hoppscotch-app/locales/en.json
+++ b/packages/hoppscotch-app/locales/en.json
@@ -10,6 +10,7 @@
"disconnect": "Disconnect",
"dismiss": "Dismiss",
"download_file": "Download file",
+ "dont_save": "Don't save",
"duplicate": "Duplicate",
"edit": "Edit",
"go_back": "Go back",
@@ -121,6 +122,7 @@
"team_collections": "Team Collections"
},
"confirm": {
+ "request_change": "Are you sure you want to discard current request, unsaved changes will be lost.",
"exit_team": "Are you sure you want to leave this team?",
"logout": "Are you sure you want to logout?",
"remove_collection": "Are you sure you want to permanently delete this collection?",