parent
4d8173fc1f
commit
86796ddf2d
ui/src
buckets/selectors
notebooks/context
shared/components/DeleteDataForm
|
@ -1,12 +1,12 @@
|
||||||
// Funcs
|
// Funcs
|
||||||
import {
|
import {
|
||||||
isSystemBucket,
|
isSystemBucket,
|
||||||
getSortedBucketNames,
|
getSortedBuckets,
|
||||||
SYSTEM,
|
SYSTEM,
|
||||||
} from 'src/buckets/selectors/index'
|
} from 'src/buckets/selectors/index'
|
||||||
|
|
||||||
// Types
|
// Types
|
||||||
import {Bucket} from 'src/types'
|
import {AppState, Bucket} from 'src/types'
|
||||||
|
|
||||||
describe('Bucket Selector', () => {
|
describe('Bucket Selector', () => {
|
||||||
it('should return true when a default bucket is passed', () => {
|
it('should return true when a default bucket is passed', () => {
|
||||||
|
@ -164,7 +164,18 @@ describe('Bucket Selector', () => {
|
||||||
},
|
},
|
||||||
]
|
]
|
||||||
|
|
||||||
const results = getSortedBucketNames(buckets)
|
const results = getSortedBuckets({
|
||||||
|
resources: {
|
||||||
|
buckets: {
|
||||||
|
status: 'Done',
|
||||||
|
byID: buckets.reduce((prev, curr) => {
|
||||||
|
prev[curr.id] = curr
|
||||||
|
return prev
|
||||||
|
}, {}),
|
||||||
|
allIDs: buckets.map(bucket => bucket.id),
|
||||||
|
},
|
||||||
|
},
|
||||||
|
} as AppState).map(bucket => bucket.name)
|
||||||
const expectedResult = [
|
const expectedResult = [
|
||||||
'alpha',
|
'alpha',
|
||||||
'buck2',
|
'buck2',
|
||||||
|
|
|
@ -32,9 +32,11 @@ const sortFunc = (a: Bucket, b: Bucket) => {
|
||||||
return 0
|
return 0
|
||||||
}
|
}
|
||||||
|
|
||||||
export const getSortedBucketNames = (buckets: Bucket[]) => {
|
export const getSortedBuckets = (state: AppState) => {
|
||||||
const systemBuckets = []
|
const systemBuckets = []
|
||||||
const otherBuckets = []
|
const otherBuckets = []
|
||||||
|
const buckets = getAll<Bucket>(state, ResourceType.Buckets)
|
||||||
|
|
||||||
buckets.forEach(bucket => {
|
buckets.forEach(bucket => {
|
||||||
// separate system buckets from the rest
|
// separate system buckets from the rest
|
||||||
if (isSystemBucket(bucket.type)) {
|
if (isSystemBucket(bucket.type)) {
|
||||||
|
@ -47,6 +49,6 @@ export const getSortedBucketNames = (buckets: Bucket[]) => {
|
||||||
systemBuckets.sort(sortFunc)
|
systemBuckets.sort(sortFunc)
|
||||||
// alphabetize other buckets
|
// alphabetize other buckets
|
||||||
otherBuckets.sort(sortFunc)
|
otherBuckets.sort(sortFunc)
|
||||||
// concat the system buckets to the end of the other buckets and map results
|
// concat the system buckets to the end of the other buckets
|
||||||
return otherBuckets.concat(systemBuckets).map(bucket => bucket.name)
|
return otherBuckets.concat(systemBuckets)
|
||||||
}
|
}
|
||||||
|
|
|
@ -0,0 +1,88 @@
|
||||||
|
import React, {FC, useEffect} from 'react'
|
||||||
|
import {connect} from 'react-redux'
|
||||||
|
|
||||||
|
// Actions
|
||||||
|
import {getBuckets} from 'src/buckets/actions/thunks'
|
||||||
|
|
||||||
|
// Selectors
|
||||||
|
import {getSortedBuckets} from 'src/buckets/selectors'
|
||||||
|
import {getStatus} from 'src/resources/selectors'
|
||||||
|
|
||||||
|
// Types
|
||||||
|
import {AppState, Bucket, ResourceType, RemoteDataState} from 'src/types'
|
||||||
|
|
||||||
|
export interface StateProps {
|
||||||
|
loading: RemoteDataState
|
||||||
|
buckets: Bucket[]
|
||||||
|
}
|
||||||
|
|
||||||
|
export interface DispatchProps {
|
||||||
|
getBuckets: typeof getBuckets
|
||||||
|
}
|
||||||
|
|
||||||
|
export type Props = StateProps & DispatchProps
|
||||||
|
|
||||||
|
export interface BucketContextType {
|
||||||
|
loading: RemoteDataState
|
||||||
|
buckets: Bucket[]
|
||||||
|
}
|
||||||
|
|
||||||
|
export const DEFAULT_CONTEXT: BucketContextType = {
|
||||||
|
loading: RemoteDataState.NotStarted,
|
||||||
|
buckets: [],
|
||||||
|
}
|
||||||
|
|
||||||
|
export const BucketContext = React.createContext<BucketContextType>(
|
||||||
|
DEFAULT_CONTEXT
|
||||||
|
)
|
||||||
|
|
||||||
|
let GLOBAL_LOADING = false
|
||||||
|
|
||||||
|
const lockAndLoad = async grabber => {
|
||||||
|
GLOBAL_LOADING = true
|
||||||
|
await grabber()
|
||||||
|
GLOBAL_LOADING = false
|
||||||
|
}
|
||||||
|
|
||||||
|
export const BucketProvider: FC<Props> = React.memo(
|
||||||
|
({loading, getBuckets, buckets, children}) => {
|
||||||
|
useEffect(() => {
|
||||||
|
if (loading !== RemoteDataState.NotStarted) {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
if (GLOBAL_LOADING) {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
lockAndLoad(getBuckets)
|
||||||
|
}, [loading])
|
||||||
|
|
||||||
|
return (
|
||||||
|
<BucketContext.Provider
|
||||||
|
value={{
|
||||||
|
loading,
|
||||||
|
buckets,
|
||||||
|
}}
|
||||||
|
>
|
||||||
|
{children}
|
||||||
|
</BucketContext.Provider>
|
||||||
|
)
|
||||||
|
}
|
||||||
|
)
|
||||||
|
|
||||||
|
const mstp = (state: AppState): StateProps => {
|
||||||
|
const buckets = getSortedBuckets(state)
|
||||||
|
const loading = getStatus(state, ResourceType.Buckets)
|
||||||
|
|
||||||
|
return {
|
||||||
|
loading,
|
||||||
|
buckets,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
const mdtp: DispatchProps = {
|
||||||
|
getBuckets: getBuckets,
|
||||||
|
}
|
||||||
|
|
||||||
|
export default connect<StateProps, DispatchProps>(mstp, mdtp)(BucketProvider)
|
|
@ -4,11 +4,10 @@ import {connect} from 'react-redux'
|
||||||
import {SelectDropdown} from '@influxdata/clockface'
|
import {SelectDropdown} from '@influxdata/clockface'
|
||||||
|
|
||||||
// Types
|
// Types
|
||||||
import {AppState, Bucket, ResourceType} from 'src/types'
|
import {AppState} from 'src/types'
|
||||||
|
|
||||||
// Selectors
|
// Selectors
|
||||||
import {getSortedBucketNames} from 'src/buckets/selectors'
|
import {getSortedBuckets} from 'src/buckets/selectors'
|
||||||
import {getAll} from 'src/resources/selectors'
|
|
||||||
|
|
||||||
interface StateProps {
|
interface StateProps {
|
||||||
bucketNames: string[]
|
bucketNames: string[]
|
||||||
|
@ -37,12 +36,10 @@ const BucketsDropdown: FunctionComponent<Props> = ({
|
||||||
|
|
||||||
const mstp = (state: AppState): StateProps => {
|
const mstp = (state: AppState): StateProps => {
|
||||||
// map names and sort via a selector
|
// map names and sort via a selector
|
||||||
const buckets = getSortedBucketNames(
|
const buckets = getSortedBuckets(state)
|
||||||
getAll<Bucket>(state, ResourceType.Buckets)
|
|
||||||
)
|
|
||||||
|
|
||||||
return {
|
return {
|
||||||
bucketNames: buckets,
|
bucketNames: buckets.map(bucket => bucket.name),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue