123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563 |
- import { BehaviorSubject } from "rxjs"
- import { gql } from "graphql-tag"
- import pull from "lodash/pull"
- import remove from "lodash/remove"
- import { translateToNewRequest } from "../types/HoppRESTRequest"
- import { TeamCollection } from "./TeamCollection"
- import { TeamRequest } from "./TeamRequest"
- import {
- rootCollectionsOfTeam,
- getCollectionChildren,
- getCollectionRequests,
- } from "./utils"
- import { apolloClient } from "~/helpers/apollo"
- function findParentOfColl(
- tree: TeamCollection[],
- collID: string,
- currentParent?: TeamCollection
- ): TeamCollection | null {
- for (const coll of tree) {
-
- if (coll.id === collID) return currentParent || null
-
- if (coll.children) {
- const result = findParentOfColl(coll.children, collID, coll)
- if (result) return result
- }
- }
- return null
- }
- function findCollInTree(
- tree: TeamCollection[],
- targetID: string
- ): TeamCollection | null {
- for (const coll of tree) {
-
- if (coll.id === targetID) return coll
-
- if (coll.children) {
- const result = findCollInTree(coll.children, targetID)
- if (result) return result
- }
- }
-
- return null
- }
- function findCollWithReqIDInTree(
- tree: TeamCollection[],
- reqID: string
- ): TeamCollection | null {
- for (const coll of tree) {
-
- if (coll.requests) {
- if (coll.requests.find((req) => req.id === reqID)) return coll
- }
-
- if (coll.children) {
- const result = findCollWithReqIDInTree(coll.children, reqID)
- if (result) return result
- }
- }
-
- return null
- }
- function findReqInTree(
- tree: TeamCollection[],
- reqID: string
- ): TeamRequest | null {
- for (const coll of tree) {
-
- if (coll.requests) {
- const match = coll.requests.find((req) => req.id === reqID)
- if (match) return match
- }
-
- if (coll.children) {
- const match = findReqInTree(coll.children, reqID)
- if (match) return match
- }
- }
-
- return null
- }
- function updateCollInTree(
- tree: TeamCollection[],
- updateColl: Partial<TeamCollection> & Pick<TeamCollection, "id">
- ) {
- const el = findCollInTree(tree, updateColl.id)
-
- if (!el) return
-
- Object.assign(el, updateColl)
- }
- function deleteCollInTree(tree: TeamCollection[], targetID: string) {
-
- const parent = findParentOfColl(tree, targetID)
-
- if (parent && parent.children) {
- parent.children = parent.children.filter((coll) => coll.id !== targetID)
- }
-
-
-
-
- const el = findCollInTree(tree, targetID)
- if (!el) return
-
- pull(tree, el)
- }
- export default class TeamCollectionAdapter {
-
- collections$: BehaviorSubject<TeamCollection[]>
-
- private teamCollectionAdded$: ZenObservable.Subscription | null
- private teamCollectionUpdated$: ZenObservable.Subscription | null
- private teamCollectionRemoved$: ZenObservable.Subscription | null
- private teamRequestAdded$: ZenObservable.Subscription | null
- private teamRequestUpdated$: ZenObservable.Subscription | null
- private teamRequestDeleted$: ZenObservable.Subscription | null
-
- constructor(private teamID: string | null) {
- this.collections$ = new BehaviorSubject<TeamCollection[]>([])
- this.teamCollectionAdded$ = null
- this.teamCollectionUpdated$ = null
- this.teamCollectionRemoved$ = null
- this.teamRequestAdded$ = null
- this.teamRequestDeleted$ = null
- this.teamRequestUpdated$ = null
- if (this.teamID) this.initialize()
- }
-
- changeTeamID(newTeamID: string | null) {
- this.collections$.next([])
- this.teamID = newTeamID
- if (this.teamID) this.initialize()
- }
-
- unsubscribeSubscriptions() {
- this.teamCollectionAdded$?.unsubscribe()
- this.teamCollectionUpdated$?.unsubscribe()
- this.teamCollectionRemoved$?.unsubscribe()
- this.teamRequestAdded$?.unsubscribe()
- this.teamRequestDeleted$?.unsubscribe()
- this.teamRequestUpdated$?.unsubscribe()
- }
-
- private async initialize() {
- await this.loadRootCollections()
- this.registerSubscriptions()
- }
-
- private async loadRootCollections(): Promise<void> {
- const colls = await rootCollectionsOfTeam(apolloClient, this.teamID)
- this.collections$.next(colls)
- }
-
- private addCollection(
- collection: TeamCollection,
- parentCollectionID: string | null
- ) {
- const tree = this.collections$.value
- if (!parentCollectionID) {
- tree.push(collection)
- } else {
- const parentCollection = findCollInTree(tree, parentCollectionID)
- if (!parentCollection) return
- if (parentCollection.children != null) {
- parentCollection.children.push(collection)
- } else {
- parentCollection.children = [collection]
- }
- }
- this.collections$.next(tree)
- }
-
- private updateCollection(
- collectionUpdate: Partial<TeamCollection> & Pick<TeamCollection, "id">
- ) {
- const tree = this.collections$.value
- updateCollInTree(tree, collectionUpdate)
- this.collections$.next(tree)
- }
-
- private removeCollection(collectionID: string) {
- const tree = this.collections$.value
- deleteCollInTree(tree, collectionID)
- this.collections$.next(tree)
- }
-
- private addRequest(request: TeamRequest) {
- const tree = this.collections$.value
-
- const coll = findCollInTree(tree, request.collectionID)
- if (!coll) return
-
- if (!coll.requests) return
-
- coll.requests.push(request)
- this.collections$.next(tree)
- }
-
- private removeRequest(requestID: string) {
- const tree = this.collections$.value
-
- const coll = findCollWithReqIDInTree(tree, requestID)
- if (!coll || !coll.requests) return
-
- remove(coll.requests, (req) => req.id === requestID)
-
- this.collections$.next(tree)
- }
-
- private updateRequest(
- requestUpdate: Partial<TeamRequest> & Pick<TeamRequest, "id">
- ) {
- const tree = this.collections$.value
-
- const req = findReqInTree(tree, requestUpdate.id)
- if (!req) return
- Object.assign(req, requestUpdate)
- this.collections$.next(tree)
- }
-
- registerSubscriptions() {
- this.teamCollectionAdded$ = apolloClient
- .subscribe({
- query: gql`
- subscription TeamCollectionAdded($teamID: String!) {
- teamCollectionAdded(teamID: $teamID) {
- id
- title
- parent {
- id
- }
- }
- }
- `,
- variables: {
- teamID: this.teamID,
- },
- })
- .subscribe(({ data }) => {
- this.addCollection(
- {
- id: data.teamCollectionAdded.id,
- children: null,
- requests: null,
- title: data.teamCollectionAdded.title,
- },
- data.teamCollectionAdded.parent?.id
- )
- })
- this.teamCollectionUpdated$ = apolloClient
- .subscribe({
- query: gql`
- subscription TeamCollectionUpdated($teamID: String!) {
- teamCollectionUpdated(teamID: $teamID) {
- id
- title
- parent {
- id
- }
- }
- }
- `,
- variables: {
- teamID: this.teamID,
- },
- })
- .subscribe(({ data }) => {
- this.updateCollection({
- id: data.teamCollectionUpdated.id,
- title: data.teamCollectionUpdated.title,
- })
- })
- this.teamCollectionRemoved$ = apolloClient
- .subscribe({
- query: gql`
- subscription TeamCollectionRemoved($teamID: String!) {
- teamCollectionRemoved(teamID: $teamID)
- }
- `,
- variables: {
- teamID: this.teamID,
- },
- })
- .subscribe(({ data }) => {
- this.removeCollection(data.teamCollectionRemoved)
- })
- this.teamRequestAdded$ = apolloClient
- .subscribe({
- query: gql`
- subscription TeamRequestAdded($teamID: String!) {
- teamRequestAdded(teamID: $teamID) {
- id
- collectionID
- request
- title
- }
- }
- `,
- variables: {
- teamID: this.teamID,
- },
- })
- .subscribe(({ data }) => {
- this.addRequest({
- id: data.teamRequestAdded.id,
- collectionID: data.teamRequestAdded.collectionID,
- request: translateToNewRequest(
- JSON.parse(data.teamRequestAdded.request)
- ),
- title: data.teamRequestAdded.title,
- })
- })
- this.teamRequestUpdated$ = apolloClient
- .subscribe({
- query: gql`
- subscription TeamRequestUpdated($teamID: String!) {
- teamRequestUpdated(teamID: $teamID) {
- id
- collectionID
- request
- title
- }
- }
- `,
- variables: {
- teamID: this.teamID,
- },
- })
- .subscribe(({ data }) => {
- this.updateRequest({
- id: data.teamRequestUpdated.id,
- collectionID: data.teamRequestUpdated.collectionID,
- request: JSON.parse(data.teamRequestUpdated.request),
- title: data.teamRequestUpdated.title,
- })
- })
- this.teamRequestDeleted$ = apolloClient
- .subscribe({
- query: gql`
- subscription TeamRequestDeleted($teamID: String!) {
- teamRequestDeleted(teamID: $teamID)
- }
- `,
- variables: {
- teamID: this.teamID,
- },
- })
- .subscribe(({ data }) => {
- this.removeRequest(data.teamRequestDeleted)
- })
- }
-
- async expandCollection(collectionID: string): Promise<void> {
-
- const tree = this.collections$.value
- const collection = findCollInTree(tree, collectionID)
- if (!collection) return
- if (collection.children != null) return
- const collections: TeamCollection[] = (
- await getCollectionChildren(apolloClient, collectionID)
- ).map<TeamCollection>((el) => {
- return {
- id: el.id,
- title: el.title,
- children: null,
- requests: null,
- }
- })
- const requests: TeamRequest[] = (
- await getCollectionRequests(apolloClient, collectionID)
- ).map<TeamRequest>((el) => {
- return {
- id: el.id,
- collectionID,
- title: el.title,
- request: translateToNewRequest(JSON.parse(el.request)),
- }
- })
- collection.children = collections
- collection.requests = requests
- this.collections$.next(tree)
- }
- }
|