Guest User

Untitled

a guest
Jan 17th, 2019
127
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.74 KB | None | 0 0
  1. import React from 'react'
  2. import Picklist from 'core/common/Picklist'
  3. import createCRUDComponents from 'core/helpers/createCRUDComponents'
  4. import Loader from 'core/common/Loader'
  5. import { withAppContext } from 'core/AppContext'
  6. import { loadInfrastructure } from '../infrastructure/actions'
  7. import { deleteNamespace } from './actions'
  8.  
  9. const ListPage = ({ ListContainer }) => {
  10. class ListPage extends React.Component {
  11. state = {
  12. activeCluster: '__all__',
  13. namespaces: null,
  14. clusterOptions: [
  15. { label: 'all', value: '__all__' },
  16. ],
  17. }
  18.  
  19. async componentDidMount () {
  20. const { context, setContext } = this.props
  21. await loadInfrastructure({ context, setContext })
  22.  
  23. // Make sure to use a new reference to props.context since it has now changed
  24. const clusters = this.props.context.clusters.filter(x => x.hasMasterNode)
  25. const clusterOptions = clusters.map(cluster => ({
  26. label: cluster.name,
  27. value: cluster.uuid
  28. }))
  29. this.setState({
  30. clusterOptions: [
  31. { label: 'all', value: '__all__' },
  32. ...clusterOptions
  33. ]
  34. })
  35. }
  36.  
  37. handleChangeCluster = clusterId => {
  38. this.setState({ activeCluster: clusterId })
  39. }
  40.  
  41. findClusterName = clusterId => {
  42. const cluster = this.props.context.clusters.find(x => x.uuid === clusterId)
  43. return (cluster && cluster.name) || ''
  44. }
  45.  
  46. render () {
  47. const { activeCluster, clusterOptions } = this.state
  48. const { namespaces } = this.props.context
  49. if (!namespaces) { return <Loader /> }
  50. const filteredNamespaces = (activeCluster === '__all__' && namespaces) ||
  51. namespaces.filter(namespace => namespace.clusterId === activeCluster)
  52. const withClusterNames = filteredNamespaces.map(ns => ({
  53. ...ns,
  54. clusterName: this.findClusterName(ns.clusterId)
  55. }))
  56.  
  57. return (
  58. <div>
  59. <Picklist
  60. name="currentCluster"
  61. label="Current Cluster"
  62. options={clusterOptions}
  63. value={activeCluster}
  64. onChange={this.handleChangeCluster}
  65. />
  66.  
  67. <ListContainer data={withClusterNames} />
  68. </div>
  69. )
  70. }
  71. }
  72. const NamespacesListPage = withAppContext(ListPage)
  73. return (<NamespacesListPage />)
  74. }
  75.  
  76. export const options = {
  77. baseUrl: '/ui/kubernetes/namespaces',
  78. columns: [
  79. { id: 'name', label: 'Name' },
  80. { id: 'clusterName', label: 'Cluster' },
  81. { id: 'created', label: 'Created' },
  82. ],
  83. dataKey: 'namespaces',
  84. deleteFn: deleteNamespace,
  85. name: 'Namespaces',
  86. title: 'Namespaces',
  87. ListPage,
  88. }
  89.  
  90. const components = createCRUDComponents(options)
  91. ListContainer = components.ListContainer
  92. export const NodesList = components.List
  93.  
  94. export default components.ListPage
Add Comment
Please, Sign In to add comment