Skip to content

Commit 47e3684

Browse files
committed
4905: rewrite mapdata, now it removes when deselect in the select
1 parent d02ab41 commit 47e3684

File tree

1 file changed

+39
-34
lines changed

1 file changed

+39
-34
lines changed

assets/admin/components/screen/util/grid-generation-and-select.jsx

Lines changed: 39 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -48,49 +48,54 @@ function GridGenerationAndSelect({
4848
* @returns {Array} Mapped data
4949
*/
5050
function mapData({ value: inputPlaylists, id }) {
51-
// Map to add region id to incoming data.
52-
const localTarget = inputPlaylists.map((playlist) => {
53-
return {
54-
region: idFromUrl(id),
55-
...playlist,
56-
};
57-
});
58-
// A copy, to be able to remove items.
59-
let selectedPlaylistsCopy = [...selectedPlaylists];
60-
61-
// The following is used to determine if something has been removed from a list.
62-
const regionPlaylists = selectedPlaylists
63-
.filter(({ region }) => region === id)
64-
.map(({ region }) => region);
65-
66-
const selectedWithoutRegion = [];
67-
68-
// Checks if an element has been removed from the list
69-
if (inputPlaylists.length < regionPlaylists.length) {
70-
selectedPlaylists.forEach((playlist) => {
71-
if (!regionPlaylists.includes(playlist.region)) {
72-
selectedWithoutRegion.push(playlist);
73-
}
74-
});
75-
// If a playlist is removed from a list, all the playlists in that region will be removed.
76-
selectedPlaylistsCopy = selectedWithoutRegion;
51+
// Region id form id url
52+
const region = idFromUrl(id);
53+
54+
// Add the region id to each inputted playlist
55+
const playlistsWithRegion = inputPlaylists.map((playlist) => ({
56+
region,
57+
...playlist,
58+
}));
59+
60+
// Get the playlists that belong the same region from the selected playlists
61+
const existingRegionPlaylists = selectedPlaylists.filter(
62+
(playlist) => playlist.region === region,
63+
);
64+
65+
// Check if any playlists from the existing region playlists are missing from
66+
// The inputted playlists if so, they are removed from the list
67+
const removedPlaylists = existingRegionPlaylists.some(
68+
({ "@id": existingId }) =>
69+
!inputPlaylists.find(
70+
({ "@id": incomingId }) => incomingId === existingId,
71+
),
72+
);
73+
74+
// Start with the existing selected playlists
75+
let updatedRegionPlaylists = [...selectedPlaylists];
76+
77+
// If any playlists were removed, filter out all playlists for this region
78+
if (removedPlaylists) {
79+
updatedRegionPlaylists = selectedPlaylists.filter(
80+
(playlist) => playlist.region !== region,
81+
);
7782
}
7883

79-
// Removes duplicates.
80-
const localSelectedPlaylists = [
81-
...localTarget,
82-
...selectedPlaylistsCopy,
84+
// Merge the updated region playlists with the input playlists,
85+
// and remove any duplicate region and id combinations
86+
const mappedData = [
87+
...playlistsWithRegion,
88+
...updatedRegionPlaylists,
8389
].filter(
8490
(playlist, index, self) =>
8591
index ===
8692
self.findIndex(
87-
(secondPlaylist) =>
88-
secondPlaylist["@id"] === playlist["@id"] &&
89-
secondPlaylist.region === playlist.region,
93+
({ region, "@id": playlistId }) =>
94+
playlistId === playlist["@id"] && region === playlist.region,
9095
),
9196
);
9297

93-
return localSelectedPlaylists;
98+
return mappedData;
9499
}
95100

96101
// On received data, map to fit the components

0 commit comments

Comments
 (0)