1- import { React , useState , useEffect } from "react" ;
2- import PropTypes from "prop-types" ;
1+ import { useState } from "react" ;
32import { useTranslation } from "react-i18next" ;
43import { SelectPlaylistColumns } from "../playlist/playlists-columns" ;
54import PlaylistsDropdown from "../util/forms/multiselect-dropdown/playlists/playlists-dropdown" ;
@@ -8,87 +7,48 @@ import FormCheckbox from "../util/forms/form-checkbox";
87import {
98 useGetV2PlaylistsByIdSlidesQuery ,
109 useGetV2PlaylistsQuery ,
11- useGetV2ScreensByIdRegionsAndRegionIdPlaylistsQuery ,
1210} from "../../redux/api/api.generated.ts" ;
1311import ScreenGanttChart from "../screen/util/screen-gantt-chart" ;
1412
1513/**
1614 * A drag and drop component for playlists.
1715 *
1816 * @param {string } props The props.
19- * @param {Function } props.handleChange - The callback when something changed
20- * @param {string } props.name - The id of the form element
21- * @param {string } props.screenId - The screen id for get request
17+ * @param {Array } props.selectedPlaylists - The selected playlists
18+ * @param {string } props.name - The name
19+ * @param {Function } props.handleChange - The callback when something is added
2220 * @param {string } props.regionId - The region id for get request
2321 * @param {string } props.regionIdForInitializeCallback - The region id to add
2422 * regions to formstateobject.
2523 * @returns {object } A drag and drop component
2624 */
2725function PlaylistDragAndDrop ( {
28- handleChange ,
26+ selectedPlaylists ,
2927 name,
30- screenId,
28+ handleChange,
29+ removeFromList,
3130 regionId,
32- regionIdForInitializeCallback,
3331} ) {
3432 const { t } = useTranslation ( "common" , {
3533 keyPrefix : "playlist-drag-and-drop" ,
3634 } ) ;
37-
3835 const [ searchText , setSearchText ] = useState ( ) ;
39- const [ selectedData , setSelectedData ] = useState ( [ ] ) ;
40- const [ totalItems , setTotalItems ] = useState ( 0 ) ;
4136 const [ page , setPage ] = useState ( 1 ) ;
4237 const [ onlySharedPlaylists , setOnlySharedPlaylists ] = useState ( false ) ;
43- const { data : selectedPlaylistsByRegion } =
44- useGetV2ScreensByIdRegionsAndRegionIdPlaylistsQuery ( {
45- id : screenId ,
46- regionId,
47- page,
48- itemsPerPage : 10 ,
49- } ) ;
5038
51- // Get method
52- const { data } = useGetV2PlaylistsQuery ( {
39+ const {
40+ data : {
41+ "hydra:member" : playlists = null ,
42+ "hydra:totalItems" : totalItems = 0 ,
43+ } = { } ,
44+ } = useGetV2PlaylistsQuery ( {
5345 isCampaign : false ,
5446 title : searchText ,
5547 itemsPerPage : 30 ,
5648 order : { createdAt : "desc" } ,
5749 sharedWithMe : onlySharedPlaylists ,
5850 } ) ;
5951
60- /**
61- * @param {object } regionsAndPlaylists This method initializes playlists, so
62- * the initial formstate object in screen manager is not empty
63- */
64- function callbackToinitializePlaylists ( regionsAndPlaylists ) {
65- handleChange ( {
66- target : {
67- id : regionIdForInitializeCallback ,
68- value : regionsAndPlaylists [ "hydra:member" ] . map (
69- ( { playlist } ) => playlist
70- ) ,
71- } ,
72- } ) ;
73- }
74-
75- /** Set loaded data into form state. */
76- useEffect ( ( ) => {
77- if ( selectedPlaylistsByRegion ) {
78- setTotalItems ( selectedPlaylistsByRegion [ "hydra:totalItems" ] ) ;
79- const newPlaylists = selectedPlaylistsByRegion [ "hydra:member" ] . map (
80- ( { playlist, weight } ) => ( { ...playlist , weight } )
81- ) ;
82-
83- const selected = [ ...selectedData , ...newPlaylists ] . sort (
84- ( a , b ) => a . weight - b . weight
85- ) ;
86-
87- setSelectedData ( selected ) ;
88- callbackToinitializePlaylists ( selectedPlaylistsByRegion ) ;
89- }
90- } , [ selectedPlaylistsByRegion ] ) ;
91-
9252 /**
9353 * Fetches data for the multi component
9454 *
@@ -98,39 +58,6 @@ function PlaylistDragAndDrop({
9858 setSearchText ( filter ) ;
9959 } ;
10060
101- /**
102- * Removes playlist from list of playlists, and closes modal.
103- *
104- * @param {object } removeItem - Item to remove
105- */
106- const removeFromList = ( removeItem ) => {
107- const indexOfItemToRemove = selectedData
108- . map ( ( item ) => {
109- return item [ "@id" ] ;
110- } )
111- . indexOf ( removeItem ) ;
112- const selectedDataCopy = [ ...selectedData ] ;
113- selectedDataCopy . splice ( indexOfItemToRemove , 1 ) ;
114- setSelectedData ( selectedDataCopy ) ;
115-
116- const target = { value : selectedDataCopy , id : name } ;
117- handleChange ( { target } ) ;
118- } ;
119-
120- /**
121- * Adds group to list of groups.
122- *
123- * @param {object } props - The props.
124- * @param {object } props.target - The target.
125- */
126- const handleAdd = ( { target } ) => {
127- const { value, id } = target ;
128- setSelectedData ( value ) ;
129- handleChange ( {
130- target : { id, value } ,
131- } ) ;
132- } ;
133-
13461 const columns = SelectPlaylistColumns ( {
13562 handleDelete : removeFromList ,
13663 apiCall : useGetV2PlaylistsByIdSlidesQuery ,
@@ -140,53 +67,43 @@ function PlaylistDragAndDrop({
14067 infoModalTitle : t ( "select-playlists-table.info-modal.slides" ) ,
14168 } ) ;
14269
70+ if ( ! playlists ) return null ;
71+
14372 return (
14473 < >
145- { data && data [ "hydra:member" ] && (
146- < >
147- < FormCheckbox
148- label = { t ( "show-only-shared" ) }
149- onChange = { ( ) => {
150- setOnlySharedPlaylists ( ! onlySharedPlaylists ) ;
151- } }
152- value = { onlySharedPlaylists }
153- name = "show-only-shared"
154- />
155- < div className = "mb-3" >
156- < PlaylistsDropdown
157- filterCallback = { onFilter }
158- name = { name }
159- handlePlaylistSelection = { handleAdd }
160- selected = { selectedData }
161- data = { data [ "hydra:member" ] }
162- />
163- </ div >
164- { selectedData . length > 0 && (
165- < DragAndDropTable
166- columns = { columns }
167- onDropped = { handleChange }
168- name = { name }
169- data = { selectedData }
170- callback = { ( ) => setPage ( page + 1 ) }
171- label = { t ( "more-playlists" ) }
172- totalItems = { totalItems }
173- />
174- ) }
175- { selectedData ?. length > 0 && (
176- < ScreenGanttChart playlists = { selectedData } id = { regionId } />
177- ) }
178- </ >
74+ < FormCheckbox
75+ label = { t ( "show-only-shared" ) }
76+ onChange = { ( ) => {
77+ setOnlySharedPlaylists ( ! onlySharedPlaylists ) ;
78+ } }
79+ value = { onlySharedPlaylists }
80+ name = "show-only-shared"
81+ />
82+ < div className = "mb-3" >
83+ < PlaylistsDropdown
84+ filterCallback = { onFilter }
85+ name = { name }
86+ handlePlaylistSelection = { handleChange }
87+ selected = { selectedPlaylists }
88+ data = { playlists }
89+ />
90+ </ div >
91+ { selectedPlaylists . length > 0 && (
92+ < DragAndDropTable
93+ columns = { columns }
94+ onDropped = { handleChange }
95+ name = { name }
96+ data = { selectedPlaylists }
97+ callback = { ( ) => setPage ( page + 1 ) }
98+ label = { t ( "more-playlists" ) }
99+ totalItems = { totalItems }
100+ />
101+ ) }
102+ { selectedPlaylists ?. length > 0 && (
103+ < ScreenGanttChart playlists = { selectedPlaylists } id = { regionId } />
179104 ) }
180105 </ >
181106 ) ;
182107}
183108
184- PlaylistDragAndDrop . propTypes = {
185- name : PropTypes . string . isRequired ,
186- screenId : PropTypes . string . isRequired ,
187- regionIdForInitializeCallback : PropTypes . string . isRequired ,
188- regionId : PropTypes . string . isRequired ,
189- handleChange : PropTypes . func . isRequired ,
190- } ;
191-
192109export default PlaylistDragAndDrop ;
0 commit comments