|
1 | 1 | import { useState, useEffect } from "react"; |
2 | 2 | import { Tabs, Tab, Alert } from "react-bootstrap"; |
3 | | -import { |
4 | | - createGridArea, |
5 | | - createGrid, |
6 | | -} from "../../../../shared/grid-generator/grid-generator"; |
| 3 | +import Grid from "./grid"; |
7 | 4 | import { useTranslation } from "react-i18next"; |
8 | 5 | import { useDispatch } from "react-redux"; |
9 | 6 | import idFromUrl from "../../util/helpers/id-from-url"; |
@@ -31,15 +28,10 @@ function GridGenerationAndSelect({ |
31 | 28 | }) { |
32 | 29 | const { t } = useTranslation("common"); |
33 | 30 | const dispatch = useDispatch(); |
34 | | - const [key, setKey] = useState(regions.length > 0 ? regions[0]["@id"] : ""); |
| 31 | + const [selectedRegion, setSelectedRegion] = useState( |
| 32 | + regions.length > 0 ? regions[0]["@id"] : "", |
| 33 | + ); |
35 | 34 | const [selectedPlaylists, setSelectedPlaylists] = useState([]); |
36 | | - const gridClasses = `grid ${vertical ? "vertical" : "horizontal"}`; |
37 | | - // Rows and columns in grid defaults to 1. |
38 | | - const configColumns = grid?.columns || 1; |
39 | | - const configRows = grid?.rows || 1; |
40 | | - const gridTemplateAreas = { |
41 | | - gridTemplateAreas: createGrid(configColumns, configRows), |
42 | | - }; |
43 | 35 |
|
44 | 36 | /** |
45 | 37 | * @param {object} props The props |
@@ -149,87 +141,67 @@ function GridGenerationAndSelect({ |
149 | 141 | setSelectedPlaylists(playlists); |
150 | 142 | }; |
151 | 143 |
|
152 | | - /** @param {string} id - The id of the selected tab */ |
153 | | - const handleSelect = (id) => { |
154 | | - setKey(id); |
155 | | - }; |
156 | | - |
157 | 144 | /** |
158 | | - * Removes playlist from list of playlists, and closes modal. |
| 145 | + * Removes playlist from list of playlists. |
159 | 146 | * |
160 | | - * @param {object} inputPlaylist - InputPlaylist to remove |
161 | | - * @param {object} inputRegion - InputRegion to remove from |
| 147 | + * @param {object} inputPlaylistId - InputPlaylistId to remove |
| 148 | + * @param {object} inputRegionId - InputRegionId to remove from |
162 | 149 | */ |
163 | | - const removeFromList = (inputPlaylist, inputRegion) => { |
164 | | - const indexOfItemToRemove = selectedPlaylists.findIndex( |
165 | | - ({ "@id": id, region }) => { |
166 | | - return region === inputRegion && id === inputPlaylist; |
167 | | - }, |
| 150 | + const removeFromList = (inputPlaylistId, inputRegionId) => { |
| 151 | + setSelectedPlaylists((prev) => |
| 152 | + prev.filter( |
| 153 | + ({ "@id": id, region: regionId }) => |
| 154 | + !(regionId === inputRegionId && id === inputPlaylistId), |
| 155 | + ), |
168 | 156 | ); |
169 | | - const selectedPlaylistsCopy = [...selectedPlaylists]; |
170 | | - selectedPlaylistsCopy.splice(indexOfItemToRemove, 1); |
171 | | - setSelectedPlaylists(selectedPlaylistsCopy); |
172 | 157 | }; |
173 | 158 |
|
| 159 | + if (regions?.length === 0) return null; |
| 160 | + |
174 | 161 | return ( |
175 | 162 | <> |
176 | 163 | <div className="col-md-4 my-3 my-md-0"> |
177 | 164 | <div className="bg-light border rounded p-1"> |
178 | | - <div className={gridClasses} style={gridTemplateAreas}> |
179 | | - {regions && |
180 | | - regions.map((data) => ( |
181 | | - <div |
182 | | - key={data["@id"]} |
183 | | - className={ |
184 | | - key === data["@id"] ? "grid-item selected" : "grid-item " |
185 | | - } |
186 | | - style={{ gridArea: createGridArea(data.gridArea) }} |
187 | | - > |
188 | | - {data.title} |
189 | | - </div> |
190 | | - ))} |
191 | | - </div> |
| 165 | + <Grid |
| 166 | + grid={grid} |
| 167 | + vertical={vertical} |
| 168 | + regions={regions} |
| 169 | + selected={selectedRegion} |
| 170 | + /> |
192 | 171 | </div> |
193 | 172 | </div> |
194 | 173 | <div className="col-md-12"> |
195 | | - {regions.length > 0 && ( |
196 | | - <> |
197 | | - <h3 className="h5">{t("screen-form.screen-region-playlists")}</h3> |
198 | | - <Tabs |
199 | | - defaultActiveKey={regions[0]["@id"]} |
200 | | - id="tabs" |
201 | | - onSelect={handleSelect} |
202 | | - className="mb-3" |
203 | | - > |
204 | | - {regions && |
205 | | - regions.map((data) => ( |
206 | | - <Tab |
207 | | - eventKey={data["@id"]} |
208 | | - key={data["@id"]} |
209 | | - title={data.title} |
210 | | - > |
211 | | - <PlaylistDragAndDrop |
212 | | - id="playlist_drag_and_drop" |
213 | | - handleChange={handleChange} |
214 | | - removeFromList={removeFromList} |
215 | | - name={data["@id"]} |
216 | | - regionIdForInitializeCallback={data["@id"]} |
217 | | - screenId={screenId} |
218 | | - regionId={idFromUrl(data["@id"])} |
219 | | - selectedPlaylists={selectedPlaylists.filter( |
220 | | - ({ region }) => region === idFromUrl(data["@id"]), |
221 | | - )} |
222 | | - /> |
223 | | - {data?.type === "touch-buttons" && ( |
224 | | - <Alert key="screen-form-touch-buttons" variant="info"> |
225 | | - {t("screen-form.touch-region-helptext")} |
226 | | - </Alert> |
227 | | - )} |
228 | | - </Tab> |
229 | | - ))} |
230 | | - </Tabs> |
231 | | - </> |
232 | | - )} |
| 174 | + <> |
| 175 | + <h3 className="h5">{t("screen-form.screen-region-playlists")}</h3> |
| 176 | + <Tabs |
| 177 | + defaultActiveKey={regions[0]["@id"]} |
| 178 | + id="tabs" |
| 179 | + onSelect={setSelectedRegion} |
| 180 | + className="mb-3" |
| 181 | + > |
| 182 | + {regions.map(({ title, "@id": id, type }) => ( |
| 183 | + <Tab eventKey={id} key={id} title={title}> |
| 184 | + <PlaylistDragAndDrop |
| 185 | + id="playlist_drag_and_drop" |
| 186 | + handleChange={handleChange} |
| 187 | + removeFromList={removeFromList} |
| 188 | + name={id} |
| 189 | + regionIdForInitializeCallback={id} |
| 190 | + screenId={screenId} |
| 191 | + regionId={idFromUrl(id)} |
| 192 | + selectedPlaylists={selectedPlaylists.filter( |
| 193 | + ({ region }) => region === idFromUrl(id), |
| 194 | + )} |
| 195 | + /> |
| 196 | + {type === "touch-buttons" && ( |
| 197 | + <Alert key="screen-form-touch-buttons" variant="info"> |
| 198 | + {t("screen-form.touch-region-helptext")} |
| 199 | + </Alert> |
| 200 | + )} |
| 201 | + </Tab> |
| 202 | + ))} |
| 203 | + </Tabs> |
| 204 | + </> |
233 | 205 | </div> |
234 | 206 | </> |
235 | 207 | ); |
|
0 commit comments