Global.tsx 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. /*
  2. * <<
  3. * Davinci
  4. * ==
  5. * Copyright (C) 2016 - 2017 EDP
  6. * ==
  7. * Licensed under the Apache License, Version 2.0 (the "License");
  8. * you may not use this file except in compliance with the License.
  9. * You may obtain a copy of the License at
  10. *
  11. * http://www.apache.org/licenses/LICENSE-2.0
  12. *
  13. * Unless required by applicable law or agreed to in writing, software
  14. * distributed under the License is distributed on an "AS IS" BASIS,
  15. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  16. * See the License for the specific language governing permissions and
  17. * limitations under the License.
  18. * >>
  19. */
  20. import React, { Component, GetDerivedStateFromProps } from 'react'
  21. import { connect } from 'react-redux'
  22. import { createStructuredSelector } from 'reselect'
  23. import ControlPanelComponent from 'app/components/Control/Panel'
  24. import ControlActions from './actions'
  25. const { setGlobalControlPanelFormValues } = ControlActions
  26. import {
  27. makeSelectGlobalControlPanelFormValues,
  28. makeSelectGlobalControlPanelSelectOptions
  29. } from './selectors'
  30. import { getFormValuesRelatedItems } from '../Dashboard/util'
  31. import { OnGetControlOptions } from 'app/components/Control/types'
  32. import {
  33. ControlPanelTypes,
  34. ControlPanelLayoutTypes
  35. } from 'app/components/Control/constants'
  36. import { IDashboard, IDashboardItem } from '../Dashboard/types'
  37. import { IFormedViews, IShareFormedViews } from '../View/types'
  38. interface IGlobalControlPanelBaseProps {
  39. currentDashboard: IDashboard
  40. currentItems: IDashboardItem[]
  41. formedViews: IFormedViews | IShareFormedViews
  42. layoutType: ControlPanelLayoutTypes
  43. onGetOptions: OnGetControlOptions
  44. onSearch: (
  45. type: ControlPanelTypes,
  46. relatedItems: number[],
  47. formValues?: object,
  48. itemId?: number
  49. ) => void
  50. onMonitoredSearchDataAction?: () => void
  51. }
  52. type MappedStates = ReturnType<typeof mapStateToProps>
  53. type MappedDispatches = ReturnType<typeof mapDispatchToProps>
  54. type IGlobalControlPanelProps = IGlobalControlPanelBaseProps &
  55. MappedStates &
  56. MappedDispatches
  57. interface IGlobalControlPanelStates {
  58. prevCurrentDashboard: IDashboard
  59. isCurrentDashboardUpdated: boolean
  60. }
  61. class GlobalControlPanel extends Component<
  62. IGlobalControlPanelProps,
  63. IGlobalControlPanelStates
  64. > {
  65. public state: IGlobalControlPanelStates = {
  66. prevCurrentDashboard: null,
  67. isCurrentDashboardUpdated: false
  68. }
  69. public static getDerivedStateFromProps: GetDerivedStateFromProps<
  70. IGlobalControlPanelProps,
  71. IGlobalControlPanelStates
  72. > = (props, state) => {
  73. return {
  74. prevCurrentDashboard: props.currentDashboard,
  75. isCurrentDashboardUpdated:
  76. !!props.currentDashboard &&
  77. !!state.prevCurrentDashboard &&
  78. props.currentDashboard !== state.prevCurrentDashboard &&
  79. props.currentDashboard.id === state.prevCurrentDashboard.id
  80. }
  81. }
  82. private search = (formValues?: object) => {
  83. const {
  84. currentDashboard,
  85. currentItems,
  86. onSearch,
  87. onMonitoredSearchDataAction
  88. } = this.props
  89. const controls = currentDashboard.config.filters
  90. const relatedItems = formValues
  91. ? getFormValuesRelatedItems(controls, currentItems, formValues)
  92. : currentItems.map((item) => item.id)
  93. onSearch(ControlPanelTypes.Global, relatedItems, formValues)
  94. if (onMonitoredSearchDataAction) {
  95. onMonitoredSearchDataAction()
  96. }
  97. }
  98. public render() {
  99. const {
  100. layoutType,
  101. currentDashboard,
  102. currentItems,
  103. formedViews,
  104. selectOptions,
  105. globalControlPanelFormValues,
  106. onGetOptions,
  107. onSetGlobalControlPanelFormValues
  108. } = this.props
  109. const { isCurrentDashboardUpdated } = this.state
  110. const items = currentItems
  111. ? currentItems
  112. .map((item) => item.id)
  113. .sort()
  114. .join(',')
  115. : ''
  116. return (
  117. currentDashboard && (
  118. <ControlPanelComponent
  119. controls={currentDashboard.config.filters}
  120. formedViews={formedViews}
  121. items={items}
  122. type={ControlPanelTypes.Global}
  123. layoutType={layoutType}
  124. reload={isCurrentDashboardUpdated}
  125. queryMode={currentDashboard.config.queryMode}
  126. formValues={globalControlPanelFormValues}
  127. mapOptions={selectOptions}
  128. onGetOptions={onGetOptions}
  129. onChange={onSetGlobalControlPanelFormValues}
  130. onSearch={this.search}
  131. />
  132. )
  133. )
  134. }
  135. }
  136. const mapStateToProps = createStructuredSelector({
  137. selectOptions: makeSelectGlobalControlPanelSelectOptions(),
  138. globalControlPanelFormValues: makeSelectGlobalControlPanelFormValues()
  139. })
  140. function mapDispatchToProps(dispatch) {
  141. return {
  142. onSetGlobalControlPanelFormValues: (values: object) =>
  143. dispatch(setGlobalControlPanelFormValues(values))
  144. }
  145. }
  146. export default connect<
  147. MappedStates,
  148. MappedDispatches,
  149. IGlobalControlPanelBaseProps
  150. >(
  151. mapStateToProps,
  152. mapDispatchToProps
  153. )(GlobalControlPanel)