diff --git a/assets/admin/components/screen/util/grid-generation-and-select.jsx b/assets/admin/components/screen/util/grid-generation-and-select.jsx index 4c5e2aeaf..eb59c46d2 100644 --- a/assets/admin/components/screen/util/grid-generation-and-select.jsx +++ b/assets/admin/components/screen/util/grid-generation-and-select.jsx @@ -1,9 +1,6 @@ import { useState, useEffect } from "react"; import { Tabs, Tab, Alert } from "react-bootstrap"; -import { - createGridArea, - createGrid, -} from "../../../../shared/grid-generator/grid-generator"; +import Grid from "./grid"; import { useTranslation } from "react-i18next"; import { useDispatch } from "react-redux"; import idFromUrl from "../../util/helpers/id-from-url"; @@ -31,15 +28,10 @@ function GridGenerationAndSelect({ }) { const { t } = useTranslation("common"); const dispatch = useDispatch(); - const [key, setKey] = useState(regions.length > 0 ? regions[0]["@id"] : ""); + const [selectedRegion, setSelectedRegion] = useState( + regions.length > 0 ? regions[0]["@id"] : "", + ); const [selectedPlaylists, setSelectedPlaylists] = useState([]); - const gridClasses = `grid ${vertical ? "vertical" : "horizontal"}`; - // Rows and columns in grid defaults to 1. - const configColumns = grid?.columns || 1; - const configRows = grid?.rows || 1; - const gridTemplateAreas = { - gridTemplateAreas: createGrid(configColumns, configRows), - }; /** * @param {object} props The props @@ -149,87 +141,67 @@ function GridGenerationAndSelect({ setSelectedPlaylists(playlists); }; - /** @param {string} id - The id of the selected tab */ - const handleSelect = (id) => { - setKey(id); - }; - /** - * Removes playlist from list of playlists, and closes modal. + * Removes playlist from list of playlists. * - * @param {object} inputPlaylist - InputPlaylist to remove - * @param {object} inputRegion - InputRegion to remove from + * @param {object} inputPlaylistId - InputPlaylistId to remove + * @param {object} inputRegionId - InputRegionId to remove from */ - const removeFromList = (inputPlaylist, inputRegion) => { - const indexOfItemToRemove = selectedPlaylists.findIndex( - ({ "@id": id, region }) => { - return region === inputRegion && id === inputPlaylist; - }, + const removeFromList = (inputPlaylistId, inputRegionId) => { + setSelectedPlaylists((prev) => + prev.filter( + ({ "@id": id, region: regionId }) => + !(regionId === inputRegionId && id === inputPlaylistId), + ), ); - const selectedPlaylistsCopy = [...selectedPlaylists]; - selectedPlaylistsCopy.splice(indexOfItemToRemove, 1); - setSelectedPlaylists(selectedPlaylistsCopy); }; + if (regions?.length === 0) return null; + return ( <>
-
- {regions && - regions.map((data) => ( -
- {data.title} -
- ))} -
+
- {regions.length > 0 && ( - <> -

{t("screen-form.screen-region-playlists")}

- - {regions && - regions.map((data) => ( - - region === idFromUrl(data["@id"]), - )} - /> - {data?.type === "touch-buttons" && ( - - {t("screen-form.touch-region-helptext")} - - )} - - ))} - - - )} + <> +

{t("screen-form.screen-region-playlists")}

+ + {regions.map(({ title, "@id": id, type }) => ( + + region === idFromUrl(id), + )} + /> + {type === "touch-buttons" && ( + + {t("screen-form.touch-region-helptext")} + + )} + + ))} + +
); diff --git a/assets/admin/components/screen/util/grid.jsx b/assets/admin/components/screen/util/grid.jsx new file mode 100644 index 000000000..0c7df5c25 --- /dev/null +++ b/assets/admin/components/screen/util/grid.jsx @@ -0,0 +1,35 @@ +import { + createGridArea, + createGrid, +} from "../../../../shared/grid-generator/grid-generator"; + +// Rows and columns in grid defaults to 1. +const Grid = ({ + vertical, + regions, + selected, + grid: { columns = 1, rows = 1 }, +}) => { + const gridClasses = `grid ${vertical ? "vertical" : "horizontal"}`; + const gridTemplateAreas = { + gridTemplateAreas: createGrid(columns, rows), + }; + + return ( +
+ {regions.map((region) => ( +
+ {region.title} +
+ ))} +
+ ); +}; + +export default Grid;