Local.tsx 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  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, { PureComponent } from 'react'
  21. import { connect } from 'react-redux'
  22. import ControlPanelComponent from 'app/components/Control/Panel'
  23. import ControlActions from './actions'
  24. const { setLocalControlPanelFormValues } = ControlActions
  25. import {
  26. makeSelectLocalControlPanelFormValues,
  27. makeSelectLocalControlPanelSelectOptions
  28. } from './selectors'
  29. import { OnGetControlOptions } from 'app/components/Control/types'
  30. import {
  31. ControlPanelTypes,
  32. ControlPanelLayoutTypes
  33. } from 'app/components/Control/constants'
  34. import { IWidgetFormed } from 'app/containers/Widget/types'
  35. import { IFormedViews, IShareFormedViews } from '../View/types'
  36. interface ILocalControlPanelBaseProps {
  37. formedViews: IFormedViews | IShareFormedViews
  38. itemId: number
  39. widget: IWidgetFormed
  40. layoutType: ControlPanelLayoutTypes
  41. onGetOptions: OnGetControlOptions
  42. onSearch: (
  43. type: ControlPanelTypes,
  44. relatedItems: number[],
  45. formValues?: object,
  46. itemId?: number
  47. ) => void
  48. onMonitoredSearchDataAction?: () => void
  49. }
  50. type MappedStates = ReturnType<ReturnType<typeof makeMapStateToProps>>
  51. type MappedDispatches = ReturnType<typeof mapDispatchToProps>
  52. type ILocalControlPanelProps = ILocalControlPanelBaseProps &
  53. MappedStates &
  54. MappedDispatches
  55. class LocalControlPanel extends PureComponent<ILocalControlPanelProps, {}> {
  56. private search = (formValues: object) => {
  57. const { itemId, onSearch, onMonitoredSearchDataAction } = this.props
  58. onSearch(ControlPanelTypes.Local, [itemId], formValues, itemId)
  59. if (onMonitoredSearchDataAction) {
  60. onMonitoredSearchDataAction()
  61. }
  62. }
  63. private change = (formValues: object) => {
  64. const { itemId, onSetLocalControlPanelFormValues } = this.props
  65. onSetLocalControlPanelFormValues(formValues, itemId)
  66. }
  67. public render() {
  68. const {
  69. formedViews,
  70. itemId,
  71. widget,
  72. layoutType,
  73. formValues,
  74. selectOptions,
  75. onGetOptions
  76. } = this.props
  77. return (
  78. !!widget.config.controls.length && (
  79. <ControlPanelComponent
  80. controls={widget.config.controls}
  81. formedViews={formedViews}
  82. items={itemId.toString()}
  83. type={ControlPanelTypes.Local}
  84. layoutType={layoutType}
  85. reload={false}
  86. viewId={widget.viewId}
  87. queryMode={widget.config.queryMode}
  88. formValues={formValues}
  89. mapOptions={selectOptions}
  90. onGetOptions={onGetOptions}
  91. onChange={this.change}
  92. onSearch={this.search}
  93. />
  94. )
  95. )
  96. }
  97. }
  98. function makeMapStateToProps() {
  99. const localControlPanelFormValuesSelector = makeSelectLocalControlPanelFormValues()
  100. const localControlPanelSelectOptionsSelector = makeSelectLocalControlPanelSelectOptions()
  101. const mapStateToProps = (state, props) => {
  102. return {
  103. formValues: localControlPanelFormValuesSelector(state, props.itemId),
  104. selectOptions: localControlPanelSelectOptionsSelector(state, props.itemId)
  105. }
  106. }
  107. return mapStateToProps
  108. }
  109. function mapDispatchToProps(dispatch) {
  110. return {
  111. onSetLocalControlPanelFormValues: (values: object, itemId: number) =>
  112. dispatch(setLocalControlPanelFormValues(values, itemId))
  113. }
  114. }
  115. export default connect<
  116. MappedStates,
  117. MappedDispatches,
  118. ILocalControlPanelBaseProps
  119. >(
  120. makeMapStateToProps,
  121. mapDispatchToProps
  122. )(LocalControlPanel)