All files / framework/GitRepoStore GitSyncStoreContext.tsx

90.91% Statements 30/33
72.22% Branches 13/18
72.73% Functions 8/11
92.59% Lines 25/27

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118              296x 296x   296x 296x 296x                       296x                 296x 568x     296x 610x             601x         601x       601x               600x 134x 134x                 600x 134x 134x                 600x 134x 68x 68x               600x                     600x                      
/*
 * Copyright 2021 Harness Inc. All rights reserved.
 * Use of this source code is governed by the PolyForm Shield 1.0.0 license
 * that can be found in the licenses directory at the root of this repository, also available at
 * https://polyformproject.org/wp-content/uploads/2020/06/PolyForm-Shield-1.0.0.txt.
 */
 
import React, { useEffect, useCallback } from 'react'
import { useParams } from 'react-router-dom'
 
import { noop } from 'lodash-es'
import { PageSpinner } from '@wings-software/uicore'
import { GitSyncConfig, SourceCodeManagerDTO, useGetSourceCodeManagers, useListGitSync } from 'services/cd-ng'
import type { ProjectPathProps } from '@common/interfaces/RouteInterfaces'
 
export interface GitSyncStoreProps {
  readonly gitSyncRepos: GitSyncConfig[]
  readonly codeManagers: SourceCodeManagerDTO[]
  readonly loadingRepos: boolean
  readonly loadingCodeManagers: boolean
  updateStore(data: Partial<Pick<GitSyncStoreProps, 'gitSyncRepos'>>): void
  refreshStore(): void
}
 
export const GitSyncStoreContext = React.createContext<GitSyncStoreProps>({
  gitSyncRepos: [],
  codeManagers: [],
  loadingRepos: false,
  loadingCodeManagers: false,
  updateStore: noop,
  refreshStore: noop
})
 
export const useGitSyncStore = (): GitSyncStoreProps => {
  return React.useContext(GitSyncStoreContext)
}
 
export const GitSyncStoreProvider: React.FC = props => {
  const { accountId, projectIdentifier, orgIdentifier } = useParams<ProjectPathProps>()
 
  //Note: right now we support git-sync only at project level
  const {
    data: dataAllGitSync,
    loading: loadingRepos,
    refetch
  } = useListGitSync({
    queryParams: { accountIdentifier: accountId, orgIdentifier, projectIdentifier },
    lazy: true
  })
 
  const { data: codeManagers, loading: loadingCodeManagers } = useGetSourceCodeManagers({
    queryParams: { accountIdentifier: accountId }
  })
 
  const [storeData, setStoreData] = React.useState<Omit<GitSyncStoreProps, 'updateStore' | 'strings'>>({
    gitSyncRepos: [],
    codeManagers: [],
    loadingRepos,
    loadingCodeManagers,
    refreshStore: refetch
  })
 
  useEffect(() => {
    Eif (!loadingCodeManagers) {
      setStoreData(prevStateData => ({
        ...prevStateData,
        loadingCodeManagers: false,
        codeManagers: codeManagers?.data || []
      }))
    }
    // eslint-disable-next-line react-hooks/exhaustive-deps
  }, [loadingCodeManagers])
 
  useEffect(() => {
    Eif (!loadingRepos) {
      setStoreData(prevStateData => ({
        ...prevStateData,
        loadingRepos: false,
        gitSyncRepos: dataAllGitSync || []
      }))
    }
    // eslint-disable-next-line react-hooks/exhaustive-deps
  }, [loadingRepos])
 
  useEffect(() => {
    if (projectIdentifier) {
      refetch()
      setStoreData(prevStateData => ({
        ...prevStateData,
        loadingRepos: true
      }))
    }
    // eslint-disable-next-line react-hooks/exhaustive-deps
  }, [projectIdentifier])
 
  const updateStore = useCallback(
    () =>
      (data: Partial<Pick<GitSyncStoreProps, 'gitSyncRepos'>>): void => {
        setStoreData(prevState => ({
          ...prevState,
          gitSyncRepos: data.gitSyncRepos || prevState.gitSyncRepos
        }))
      },
    []
  )
 
  return (
    <GitSyncStoreContext.Provider
      value={{
        ...storeData,
        updateStore
      }}
    >
      {loadingRepos ? <PageSpinner /> : props.children}
    </GitSyncStoreContext.Provider>
  )
}