Skip to content

Commit d6d2e28

Browse files
author
Sine Jespersen
committed
rewrite screen save
1 parent 79ffdba commit d6d2e28

File tree

5 files changed

+232
-211
lines changed

5 files changed

+232
-211
lines changed

src/components/playlist-drag-and-drop/playlist-drag-and-drop.jsx

Lines changed: 41 additions & 113 deletions
Original file line numberDiff line numberDiff line change
@@ -26,69 +26,32 @@ import ScreenGanttChart from "../screen/util/screen-gantt-chart";
2626
*/
2727
function PlaylistDragAndDrop({
2828
handleChange,
29+
selectedPlaylists,
2930
name,
30-
screenId,
31+
handleAdd,
32+
removeFromList,
3133
regionId,
32-
regionIdForInitializeCallback,
3334
}) {
3435
const { t } = useTranslation("common", {
3536
keyPrefix: "playlist-drag-and-drop",
3637
});
37-
3838
const [searchText, setSearchText] = useState();
39-
const [selectedData, setSelectedData] = useState([]);
40-
const [totalItems, setTotalItems] = useState(0);
4139
const [page, setPage] = useState(1);
4240
const [onlySharedPlaylists, setOnlySharedPlaylists] = useState(false);
43-
const { data: selectedPlaylistsByRegion } =
44-
useGetV2ScreensByIdRegionsAndRegionIdPlaylistsQuery({
45-
id: screenId,
46-
regionId,
47-
page,
48-
itemsPerPage: 10,
49-
});
5041

51-
// Get method
52-
const { data } = useGetV2PlaylistsQuery({
42+
const {
43+
data: {
44+
"hydra:member": playlists = null,
45+
"hydra:totalItems": totalItems = 0,
46+
} = {},
47+
} = useGetV2PlaylistsQuery({
5348
isCampaign: false,
5449
title: searchText,
5550
itemsPerPage: 30,
5651
order: { createdAt: "desc" },
5752
sharedWithMe: onlySharedPlaylists,
5853
});
5954

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-
9255
/**
9356
* Fetches data for the multi component
9457
*
@@ -98,39 +61,6 @@ function PlaylistDragAndDrop({
9861
setSearchText(filter);
9962
};
10063

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-
13464
const columns = SelectPlaylistColumns({
13565
handleDelete: removeFromList,
13666
apiCall: useGetV2PlaylistsByIdSlidesQuery,
@@ -140,42 +70,40 @@ function PlaylistDragAndDrop({
14070
infoModalTitle: t("select-playlists-table.info-modal.slides"),
14171
});
14272

73+
if (!playlists) return null;
74+
14375
return (
14476
<>
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-
</>
77+
<FormCheckbox
78+
label={t("show-only-shared")}
79+
onChange={() => {
80+
setOnlySharedPlaylists(!onlySharedPlaylists);
81+
}}
82+
value={onlySharedPlaylists}
83+
name="show-only-shared"
84+
/>
85+
<div className="mb-3">
86+
<PlaylistsDropdown
87+
filterCallback={onFilter}
88+
name={name}
89+
handlePlaylistSelection={handleAdd}
90+
selected={selectedPlaylists}
91+
data={playlists}
92+
/>
93+
</div>
94+
{selectedPlaylists.length > 0 && (
95+
<DragAndDropTable
96+
columns={columns}
97+
onDropped={handleChange}
98+
name={name}
99+
data={selectedPlaylists}
100+
callback={() => setPage(page + 1)}
101+
label={t("more-playlists")}
102+
totalItems={totalItems}
103+
/>
104+
)}
105+
{selectedPlaylists?.length > 0 && (
106+
<ScreenGanttChart playlists={selectedPlaylists} id={regionId} />
179107
)}
180108
</>
181109
);

src/components/screen/screen-form.jsx

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -38,11 +38,14 @@ import { displayError } from "../util/list/toast-component/display-toast";
3838
* @param {string} props.loadingMessage The loading message for the spinner
3939
* @param {object} props.orientationOptions The options for the orientation dropdown
4040
* @param {object} props.resolutionOptions The options for the resolution dropdown
41+
* @param props.handleSubmitWithoutRedirect
42+
* @param props.handleSubmitWithRedirect
4143
* @returns {object} The screen form.
4244
*/
4345
function ScreenForm({
46+
handleSubmitWithoutRedirect,
4447
handleInput,
45-
handleSubmit,
48+
handleSubmitWithRedirect,
4649
headerText,
4750
orientationOptions,
4851
resolutionOptions,
@@ -81,8 +84,12 @@ function ScreenForm({
8184
previewOrientationOptions[0].value
8285
);
8386

84-
/** Check if published is set */
85-
const checkInputsHandleSubmit = () => {
87+
/**
88+
* Check if published is set
89+
*
90+
* @param redirect
91+
*/
92+
const checkInputsHandleSubmit = (redirect) => {
8693
setLayoutError(false);
8794
let submit = true;
8895

@@ -93,7 +100,11 @@ function ScreenForm({
93100
}
94101

95102
if (submit) {
96-
handleSubmit();
103+
if (redirect) {
104+
handleSubmitWithRedirect();
105+
} else {
106+
handleSubmitWithoutRedirect();
107+
}
97108
}
98109
};
99110

@@ -252,14 +263,14 @@ function ScreenForm({
252263
)}
253264
{selectedLayout &&
254265
selectedLayout.grid &&
255-
selectedLayout.regions && (
266+
selectedLayout.regions &&
267+
screen.layout && (
256268
<GridGenerationAndSelect
257269
screenId={idFromUrl(screen["@id"])}
258270
grid={selectedLayout?.grid}
259271
vertical={isVertical()}
260272
regions={selectedLayout.regions}
261273
handleInput={handleInput}
262-
selectedData={screen.layout}
263274
/>
264275
)}
265276
</div>
@@ -375,7 +386,7 @@ function ScreenForm({
375386
type="button"
376387
className="margin-right-button"
377388
id="save_screen"
378-
onClick={checkInputsHandleSubmit}
389+
onClick={() => checkInputsHandleSubmit(false)}
379390
>
380391
{t("save-button")}
381392
</Button>

src/components/screen/screen-manager.jsx

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ function ScreenManager({
3535
initialState = null,
3636
}) {
3737
const { t } = useTranslation("common", { keyPrefix: "screen-manager" });
38+
const [saveWithoutClose, setSaveWithoutClose] = useState(false);
3839
const navigate = useNavigate();
3940
const orientationOptions = [
4041
{ title: "Vertikal", "@id": "vertical" },
@@ -58,7 +59,7 @@ function ScreenManager({
5859
// Handler for creating screen.
5960
const [
6061
PostV2Screens,
61-
{ error: saveErrorPost, isSuccess: isSaveSuccessPost },
62+
{ data: postData, error: saveErrorPost, isSuccess: isSaveSuccessPost },
6263
] = usePostV2ScreensMutation();
6364

6465
/** If the screen is saved, display the success message */
@@ -170,7 +171,7 @@ function ScreenManager({
170171
function mapPlaylistsWithRegion() {
171172
const returnArray = [];
172173
const { playlists, regions } = formStateObject;
173-
const regionIds = regions.map((r) => r["@id"]);
174+
const regionIds = regions.map((r) => idFromUrl(r["@id"]));
174175

175176
// The playlists all have a regionId, the following creates a unique list of relevant regions If there are not
176177
// playlists, then an empty playlist is to be saved per region
@@ -191,7 +192,6 @@ function ScreenManager({
191192
regionId: idFromUrl(regionId),
192193
});
193194
});
194-
195195
// The remaining regions are added with empty playlist arrays.
196196
if (regionIds.length > 0) {
197197
regionIds.forEach((regionId) =>
@@ -201,7 +201,6 @@ function ScreenManager({
201201
})
202202
);
203203
}
204-
205204
return returnArray;
206205
}
207206

@@ -267,24 +266,39 @@ function ScreenManager({
267266
}
268267
};
269268

269+
const handleSubmitWithRedirect = () => {
270+
setSaveWithoutClose(true);
271+
handleSubmit();
272+
};
273+
270274
/** Handle submitting is done. */
271275
useEffect(() => {
272276
if (isSaveSuccessPut || isSaveSuccessPost) {
273277
setSavingScreen(false);
274-
navigate("/screen/list");
278+
279+
if (saveWithoutClose) {
280+
setSaveWithoutClose(false);
281+
282+
if (isSaveSuccessPost) {
283+
navigate(`/screen/edit/${idFromUrl(postData["@id"])}`);
284+
}
285+
} else {
286+
navigate("/screen/list");
287+
}
275288
}
276289
}, [isSaveSuccessPut, isSaveSuccessPost]);
277290

278291
return (
279292
<>
280293
{formStateObject && (
281294
<ScreenForm
295+
handleSubmitWithoutRedirect={handleSubmitWithRedirect}
282296
screen={formStateObject}
283297
orientationOptions={orientationOptions}
284298
resolutionOptions={resolutionOptions}
285299
headerText={headerText}
286300
handleInput={handleInput}
287-
handleSubmit={handleSubmit}
301+
handleSubmitWithRedirect={handleSubmit}
288302
isLoading={savingScreen || isLoading}
289303
loadingMessage={loadingMessage}
290304
groupId={groupId}

0 commit comments

Comments
 (0)