Skip to content

Commit

Permalink
refactor: use workpace service to switch collection and env workspace…
Browse files Browse the repository at this point in the history
… state
  • Loading branch information
nivedin committed May 3, 2024
1 parent 3bc353c commit 056f21b
Show file tree
Hide file tree
Showing 10 changed files with 64 additions and 130 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@ import { useI18n } from "~/composables/i18n"
import { useToast } from "~/composables/toast"
import { appendRESTCollections, restCollections$ } from "~/newstore/collections"
import MyCollectionImport from "~/components/importExport/ImportExportSteps/MyCollectionImport.vue"
import { GetMyTeamsQuery } from "~/helpers/backend/graphql"
import IconFolderPlus from "~icons/lucide/folder-plus"
import IconOpenAPI from "~icons/lucide/file"
Expand All @@ -55,16 +54,15 @@ import { teamCollectionsExporter } from "~/helpers/import-export/export/teamColl
import { GistSource } from "~/helpers/import-export/import/import-sources/GistSource"
import { ImporterOrExporter } from "~/components/importExport/types"
import { MyWorkspace } from "~/services/workspace.service"
const t = useI18n()
const toast = useToast()
type SelectedTeam = GetMyTeamsQuery["myTeams"][number] | undefined
type CollectionType =
| {
type: "team-collections"
selectedTeam: SelectedTeam
selectedTeam: MyWorkspace
}
| { type: "my-collections" }
Expand Down Expand Up @@ -433,7 +431,7 @@ const HoppTeamCollectionsExporter: ImporterOrExporter = {
props.collectionsType.selectedTeam
) {
const res = await teamCollectionsExporter(
props.collectionsType.selectedTeam.id
props.collectionsType.selectedTeam.teamID
)
if (E.isRight(res)) {
Expand Down Expand Up @@ -569,26 +567,26 @@ const hasTeamWriteAccess = computed(() => {
}
return (
collectionsType.selectedTeam.myRole === "EDITOR" ||
collectionsType.selectedTeam.myRole === "OWNER"
collectionsType.selectedTeam.role === "EDITOR" ||
collectionsType.selectedTeam.role === "OWNER"
)
})
const selectedTeamID = computed(() => {
const { collectionsType } = props
return collectionsType.type === "team-collections"
? collectionsType.selectedTeam?.id
? collectionsType.selectedTeam?.teamID
: undefined
})
const getCollectionJSON = async () => {
if (
props.collectionsType.type === "team-collections" &&
props.collectionsType.selectedTeam?.id
props.collectionsType.selectedTeam?.teamID
) {
const res = await getTeamCollectionJSON(
props.collectionsType.selectedTeam?.id
props.collectionsType.selectedTeam?.teamID
)
return E.isRight(res)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,6 @@ import {
} from "@hoppscotch/data"
import { pipe } from "fp-ts/function"
import * as TE from "fp-ts/TaskEither"
import { GetMyTeamsQuery } from "~/helpers/backend/graphql"
import {
createRequestInCollection,
updateTeamRequest,
Expand All @@ -86,19 +85,18 @@ import { platform } from "~/platform"
import { useService } from "dioc/vue"
import { RESTTabService } from "~/services/tab/rest"
import { GQLTabService } from "~/services/tab/graphql"
import { MyWorkspace } from "~/services/workspace.service"
const t = useI18n()
const toast = useToast()
const RESTTabs = useService(RESTTabService)
const GQLTabs = useService(GQLTabService)
type SelectedTeam = GetMyTeamsQuery["myTeams"][number] | undefined
type CollectionType =
| {
type: "team-collections"
selectedTeam: SelectedTeam
selectedTeam: MyWorkspace
}
| { type: "my-collections"; selectedTeam: undefined }
Expand Down Expand Up @@ -192,7 +190,7 @@ watch(
}
)
const updateTeam = (newTeam: SelectedTeam) => {
const updateTeam = (newTeam: MyWorkspace) => {
collectionsType.value.selectedTeam = newTeam
}
Expand Down Expand Up @@ -493,7 +491,7 @@ const updateTeamCollectionOrFolder = (
const data = {
title: requestUpdated.name,
request: JSON.stringify(requestUpdated),
teamID: collectionsType.value.selectedTeam.id,
teamID: collectionsType.value.selectedTeam.teamID,
}
pipe(
createRequestInCollection(collectionID, data),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -387,7 +387,6 @@ import IconPlus from "~icons/lucide/plus"
import IconHelpCircle from "~icons/lucide/help-circle"
import IconImport from "~icons/lucide/folder-down"
import { computed, PropType, Ref, toRef } from "vue"
import { GetMyTeamsQuery } from "~/helpers/backend/graphql"
import { useI18n } from "@composables/i18n"
import { useColorMode } from "@composables/theming"
import { TeamCollection } from "~/helpers/teams/TeamCollection"
Expand All @@ -400,17 +399,16 @@ import * as O from "fp-ts/Option"
import { Picked } from "~/helpers/types/HoppPicked.js"
import { RESTTabService } from "~/services/tab/rest"
import { useService } from "dioc/vue"
import { MyWorkspace } from "~/services/workspace.service"
const t = useI18n()
const colorMode = useColorMode()
const tabs = useService(RESTTabService)
type SelectedTeam = GetMyTeamsQuery["myTeams"][number] | undefined
type CollectionType =
| {
type: "team-collections"
selectedTeam: SelectedTeam
selectedTeam: MyWorkspace
}
| { type: "my-collections"; selectedTeam: undefined }
Expand Down Expand Up @@ -614,7 +612,7 @@ const hasNoTeamAccess = computed(
() =>
props.collectionsType.type === "team-collections" &&
(props.collectionsType.selectedTeam === undefined ||
props.collectionsType.selectedTeam.myRole === "VIEWER")
props.collectionsType.selectedTeam.role === "VIEWER")
)
const isSelected = ({
Expand Down
77 changes: 17 additions & 60 deletions packages/hoppscotch-common/src/components/collections/index.vue
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,6 @@ import { useI18n } from "@composables/i18n"
import { Picked } from "~/helpers/types/HoppPicked"
import { useReadonlyStream } from "~/composables/stream"
import { useLocalState } from "~/newstore/localstate"
import { GetMyTeamsQuery } from "~/helpers/backend/graphql"
import { pipe } from "fp-ts/function"
import * as TE from "fp-ts/TaskEither"
import {
Expand Down Expand Up @@ -245,7 +244,7 @@ import {
} from "~/helpers/collection/collection"
import { currentReorderingStatus$ } from "~/newstore/reordering"
import { defineActionHandler, invokeAction } from "~/helpers/actions"
import { WorkspaceService } from "~/services/workspace.service"
import { MyWorkspace, WorkspaceService } from "~/services/workspace.service"
import { useService } from "dioc/vue"
import { RESTTabService } from "~/services/tab/rest"
import { HoppInheritedProperty } from "~/helpers/types/HoppInheritedProperties"
Expand Down Expand Up @@ -274,16 +273,14 @@ const props = defineProps({
const emit = defineEmits<{
(event: "select", payload: Picked | null): void
(event: "update-team", team: SelectedTeam): void
(event: "update-team", team: MyWorkspace): void
(event: "update-collection-type", type: CollectionType["type"]): void
}>()
type SelectedTeam = GetMyTeamsQuery["myTeams"][number] | undefined
type CollectionType =
| {
type: "team-collections"
selectedTeam: SelectedTeam
selectedTeam: MyWorkspace
}
| { type: "my-collections"; selectedTeam: undefined }
Expand Down Expand Up @@ -330,9 +327,7 @@ const requestMoveLoading = ref<string[]>([])
// TeamList-Adapter
const workspaceService = useService(WorkspaceService)
const teamListAdapter = workspaceService.acquireTeamListAdapter(null)
const myTeams = useReadonlyStream(teamListAdapter.teamList$, null)
const REMEMBERED_TEAM_ID = useLocalState("REMEMBERED_TEAM_ID")
const teamListFetched = ref(false)
// Team Collection Adapter
const teamCollectionAdapter = new TeamCollectionAdapter(null)
Expand Down Expand Up @@ -378,7 +373,7 @@ watch(
filterTexts,
(newFilterText) => {
if (collectionsType.value.type === "team-collections") {
const selectedTeamID = collectionsType.value.selectedTeam?.id
const selectedTeamID = collectionsType.value.selectedTeam?.teamID
selectedTeamID &&
debouncedSearch(newFilterText, selectedTeamID)?.catch(() => {})
Expand Down Expand Up @@ -435,19 +430,6 @@ onMounted(() => {
}
})
watch(
() => myTeams.value,
(newTeams) => {
if (newTeams && !teamListFetched.value) {
teamListFetched.value = true
if (REMEMBERED_TEAM_ID.value && currentUser.value) {
const team = newTeams.find((t) => t.id === REMEMBERED_TEAM_ID.value)
if (team) updateSelectedTeam(team)
}
}
}
)
const switchToMyCollections = () => {
collectionsType.value.type = "my-collections"
collectionsType.value.selectedTeam = undefined
Expand Down Expand Up @@ -479,11 +461,12 @@ const expandTeamCollection = (collectionID: string) => {
teamCollectionAdapter.expandCollection(collectionID)
}
const updateSelectedTeam = (team: SelectedTeam) => {
const updateSelectedTeam = (team: MyWorkspace) => {
if (team) {
collectionsType.value.type = "team-collections"
teamCollectionAdapter.changeTeamID(team.teamID)
collectionsType.value.selectedTeam = team
REMEMBERED_TEAM_ID.value = team.id
REMEMBERED_TEAM_ID.value = team.teamID
emit("update-team", team)
emit("update-collection-type", "team-collections")
}
Expand All @@ -492,46 +475,20 @@ const updateSelectedTeam = (team: SelectedTeam) => {
const workspace = workspaceService.currentWorkspace
// Used to switch collection type and team when user switch workspace in the global workspace switcher
// Check if there is a teamID in the workspace, if yes, switch to team collections and select the team
// If there is no teamID, switch to my collections
watch(
() => {
const space = workspace.value
return space.type === "personal" ? undefined : space.teamID
},
(teamID) => {
if (teamID) {
const team = myTeams.value?.find((t) => t.id === teamID)
if (team) {
updateSelectedTeam(team)
}
return
workspace,
(newWorkspace) => {
if (newWorkspace.type === "personal") {
switchToMyCollections()
} else if (newWorkspace.type === "team") {
updateSelectedTeam(newWorkspace)
}
return switchToMyCollections()
},
{
immediate: true,
}
)
// look for the teamID change in the workspace and change the collection adapter teamID
watch(
() => {
const teamID =
workspace.value.type === "team" && workspace.value.teamID
? workspace.value.teamID
: null
return teamID
},
(newTeamID) => {
if (newTeamID) {
teamCollectionAdapter.changeTeamID(newTeamID)
}
}
)
// Switch to my-collections and reset the team collection when user logout
watch(
() => currentUser.value,
Expand All @@ -553,7 +510,7 @@ const hasTeamWriteAccess = computed(() => {
return false
}
const role = collectionsType.value.selectedTeam?.myRole
const role = collectionsType.value.selectedTeam?.role
return role === "OWNER" || role === "EDITOR"
})
Expand Down Expand Up @@ -768,7 +725,7 @@ const addNewRootCollection = (name: string) => {
})
pipe(
createNewRootCollection(name, collectionsType.value.selectedTeam.id),
createNewRootCollection(name, collectionsType.value.selectedTeam.teamID),
TE.match(
(err: GQLError<string>) => {
toast.error(`${getErrorMessage(err)}`)
Expand Down Expand Up @@ -839,7 +796,7 @@ const onAddRequest = (requestName: string) => {
const data = {
request: JSON.stringify(newRequest),
teamID: collectionsType.value.selectedTeam.id,
teamID: collectionsType.value.selectedTeam.teamID,
title: requestName,
}
Expand Down Expand Up @@ -1166,7 +1123,7 @@ const duplicateRequest = (payload: {
const data = {
request: JSON.stringify(newRequest),
teamID: collectionsType.value.selectedTeam.id,
teamID: collectionsType.value.selectedTeam.teamID,
title: `${request.name} - ${t("action.duplicate")}`,
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -364,6 +364,7 @@ const switchToTeamWorkspace = (team: GetMyTeamsQuery["myTeams"][number]) => {
teamID: team.id,
teamName: team.name,
type: "team",
role: team.myRole,
})
}
watch(
Expand Down

0 comments on commit 056f21b

Please sign in to comment.