From e2b15cedd43b11e9cb00ac4214c73722b6cef426 Mon Sep 17 00:00:00 2001 From: Andrew Bastin Date: Wed, 25 Oct 2023 22:28:41 +0530 Subject: [PATCH] feat: clean up cookies implementation --- packages/hoppscotch-common/locales/en.json | 7 +- .../src/components/cookies/AllModal.vue | 72 +++++++++++++---- .../src/components/cookies/EditCookie.vue | 77 ++++++++++++++++--- .../__tests__/interceptor.service.spec.ts | 55 +++++++++++++ .../src/services/interceptor.service.ts | 10 +++ 5 files changed, 196 insertions(+), 25 deletions(-) diff --git a/packages/hoppscotch-common/locales/en.json b/packages/hoppscotch-common/locales/en.json index cc8ed13f9..5b07b71c5 100644 --- a/packages/hoppscotch-common/locales/en.json +++ b/packages/hoppscotch-common/locales/en.json @@ -59,13 +59,16 @@ "modal": { "new_domain_name": "New domain name", "set": "Set a cookie", - "cookie_string": "Cookie string", + "cookie_string": "Set-Cookie string", "cookie_name": "Name", "cookie_value": "Value", "cookie_path": "Path", "cookie_expires": "Expires", "managed_tab": "Managed", - "raw_tab": "Raw" + "raw_tab": "Raw", + "interceptor_no_support": "Your currently selected interceptor does not support cookies.", + "no_domains": "No domains defined", + "no_cookies_in_domain": "No cookies set for this domain" } }, "app": { diff --git a/packages/hoppscotch-common/src/components/cookies/AllModal.vue b/packages/hoppscotch-common/src/components/cookies/AllModal.vue index a91706a6c..7736ce3a0 100644 --- a/packages/hoppscotch-common/src/components/cookies/AllModal.vue +++ b/packages/hoppscotch-common/src/components/cookies/AllModal.vue @@ -7,6 +7,14 @@ @close="hideModal" > @@ -107,7 +131,9 @@ import IconEdit from "~icons/lucide/edit" import IconTrash2 from "~icons/lucide/trash-2" import IconPlus from "~icons/lucide/plus" import { cloneDeep } from "lodash-es" -import { ref, watch } from "vue" +import { ref, watch, computed } from "vue" +import { InterceptorService } from "~/services/interceptor.service" +import { EditCookieConfig } from "./EditCookie.vue" const props = defineProps<{ show: boolean @@ -121,10 +147,19 @@ const t = useI18n() const newDomainText = ref("") +const interceptorService = useService(InterceptorService) const cookieJarService = useService(CookieJarService) const workingCookieJar = ref(cloneDeep(cookieJarService.cookieJar.value)) +const currentInterceptorSupportsCookies = computed(() => { + const currentInterceptor = interceptorService.currentInterceptor.value + + if (!currentInterceptor) return true + + return currentInterceptor.supportsCookies ?? false +}) + function addNewDomain() { workingCookieJar.value.set(newDomainText.value, []) newDomainText.value = "" @@ -135,11 +170,7 @@ function deleteDomain(domain: string) { } function addCookieToDomain(domain: string) { - const entry = workingCookieJar.value.get(domain) - - if (entry) { - entry.push("") - } + showEditModalFor.value = { type: "create", domain } } watch( @@ -151,8 +182,7 @@ watch( } ) -// Tuple of [domain, entryIndex] -const showEditModalFor = ref<[string, number, string] | null>(null) +const showEditModalFor = ref(null) function saveCookieChanges() { cookieJarService.cookieJar.value = workingCookieJar.value @@ -164,7 +194,12 @@ function cancelCookieChanges() { } function editCookie(domain: string, entryIndex: number, cookieEntry: string) { - showEditModalFor.value = [domain, entryIndex, cookieEntry] + showEditModalFor.value = { + type: "edit", + domain, + entryIndex, + currentCookieEntry: cookieEntry, + } } function deleteCookie(domain: string, entryIndex: number) { @@ -175,10 +210,21 @@ function deleteCookie(domain: string, entryIndex: number) { } } -function saveCookieUpdate(cookie: string) { - if (!showEditModalFor.value) return +function saveCookie(cookie: string) { + if (showEditModalFor.value?.type === "create") { + const { domain } = showEditModalFor.value - const [domain, entryIndex] = showEditModalFor.value! + const entry = workingCookieJar.value.get(domain)! + entry.push(cookie) + + showEditModalFor.value = null + + return + } + + if (showEditModalFor.value?.type !== "edit") return + + const { domain, entryIndex } = showEditModalFor.value! const entry = workingCookieJar.value.get(domain) diff --git a/packages/hoppscotch-common/src/components/cookies/EditCookie.vue b/packages/hoppscotch-common/src/components/cookies/EditCookie.vue index ef6d1f0bf..b18085c0d 100644 --- a/packages/hoppscotch-common/src/components/cookies/EditCookie.vue +++ b/packages/hoppscotch-common/src/components/cookies/EditCookie.vue @@ -7,10 +7,12 @@ @close="hideModal" > + +