Display.tsx 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  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, { useMemo, useEffect } from 'react'
  21. import { useSelector, useDispatch } from 'react-redux'
  22. import { makeSelectCurrentDisplayWidgets } from '../selectors'
  23. import {
  24. makeSelectCurrentSlides,
  25. makeSelectCurrentDisplay
  26. } from 'containers/Viz/selectors'
  27. import { makeSelectFormedViews } from 'containers/View/selectors'
  28. import {
  29. LayerListContextValue,
  30. LayerListContext
  31. } from '../components/Layer'
  32. import Slide from './Slide'
  33. import { ViewActions } from 'containers/View/actions'
  34. import { RenderType } from 'containers/Widget/components/Widget'
  35. import { IQueryConditions } from 'containers/Dashboard/types'
  36. import { IWidgetFormed } from 'app/containers/Widget/types'
  37. import { getRequestParamsByWidgetConfig } from 'containers/Viz/util'
  38. import DisplayActions from '../actions'
  39. interface IDisplayPreviewProps {
  40. slideIndex: number
  41. }
  42. const DisplayPreview: React.FC<IDisplayPreviewProps> = (props) => {
  43. const { slideIndex } = props
  44. const dispatch = useDispatch()
  45. const { id: displayId } = useSelector(makeSelectCurrentDisplay())
  46. const currentSlides = useSelector(makeSelectCurrentSlides())
  47. const currentDisplayWidgets = useSelector(makeSelectCurrentDisplayWidgets())
  48. const formedViews = useSelector(makeSelectFormedViews())
  49. useEffect(() => {
  50. const { id: slideId } = currentSlides[slideIndex]
  51. dispatch(DisplayActions.loadSlideDetail(displayId, slideId))
  52. }, [displayId, slideIndex, currentSlides])
  53. const layerListContextValue = useMemo<LayerListContextValue>(
  54. () => ({
  55. currentDisplayWidgets,
  56. getWidgetViewModel: (viewId: number) => {
  57. const viewModel = formedViews[viewId].model
  58. return viewModel
  59. },
  60. getChartData: (
  61. renderType: RenderType,
  62. slideId: number,
  63. layerId: number,
  64. widget: IWidgetFormed,
  65. prevQueryConditions: Partial<IQueryConditions>,
  66. queryConditions?: Partial<IQueryConditions>
  67. ) => {
  68. const requestParams = getRequestParamsByWidgetConfig(
  69. renderType,
  70. widget.config,
  71. prevQueryConditions,
  72. queryConditions
  73. )
  74. dispatch(
  75. ViewActions.loadViewDataFromVizItem(
  76. renderType,
  77. [slideId, layerId],
  78. widget.viewId,
  79. requestParams,
  80. 'display',
  81. null
  82. )
  83. )
  84. }
  85. }),
  86. [currentDisplayWidgets, formedViews]
  87. )
  88. return (
  89. <LayerListContext.Provider value={layerListContextValue}>
  90. {currentSlides.map((slide) => (
  91. <Slide
  92. key={slide.id}
  93. slideId={slide.id}
  94. slideParams={slide.config.slideParams}
  95. />
  96. ))}
  97. </LayerListContext.Provider>
  98. )
  99. }
  100. export default DisplayPreview