Skip to content

Commit a2ff44b

Browse files
authored
Merge pull request #1121 from Arnei/reduce-api-requests
Reduce api requests
2 parents 751bdd9 + fc13497 commit a2ff44b

28 files changed

Lines changed: 491 additions & 528 deletions

src/components/About.tsx

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,13 +47,11 @@ const About = () => {
4747
{
4848
path: "/about/imprint",
4949
accessRole: "ROLE_UI_USERS_VIEW",
50-
loadFn: () => { },
5150
text: "ABOUT.IMPRINT"
5251
},
5352
{
5453
path: "/about/privacy",
5554
accessRole: "ROLE_UI_GROUPS_VIEW",
56-
loadFn: () => { },
5755
text: "ABOUT.PRIVACY"
5856
},
5957
]}

src/components/Header.tsx

Lines changed: 14 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,10 @@
11
import React, { useEffect, useRef, useState } from "react";
22
import { useTranslation } from "react-i18next";
3-
import { Link } from "react-router";
3+
import { useNavigate } from "react-router";
44
import i18n from "../i18n/i18n";
55
import languages from "../i18n/languages";
66
import opencastLogo from "../img/opencast-white.svg?url";
77
import { setSpecificServiceFilter } from "../slices/tableFilterSlice";
8-
import { loadServicesIntoTable } from "../thunks/tableThunks";
98
import { getErrorCount, getHealthStatus } from "../selectors/healthSelectors";
109
import {
1110
getOrgProperties,
@@ -73,14 +72,6 @@ const Header = () => {
7372
registrationModalRef.current?.open()
7473
};
7574

76-
const redirectToServices = async () => {
77-
// Load services into table
78-
await dispatch(loadServicesIntoTable());
79-
80-
// set the action filter value of services to true
81-
await dispatch(setSpecificServiceFilter({ filter: "actions", filterValue: "true" }));
82-
};
83-
8475
const showHotKeyCheatSheet = () => {
8576
hotKeyCheatSheetModalRef.current?.open()
8677
};
@@ -216,7 +207,6 @@ const Header = () => {
216207
{displayMenuNotify && (
217208
<MenuNotify
218209
healthStatus={healthStatus}
219-
redirectToServices={redirectToServices}
220210
/>
221211
)}
222212
</div>
@@ -302,20 +292,27 @@ const MenuLang = () => {
302292

303293
const MenuNotify = ({
304294
healthStatus,
305-
redirectToServices
306295
}: {
307296
healthStatus: HealthStatus[],
308-
redirectToServices: () => Promise<void>,
309297
}) => {
298+
const dispatch = useAppDispatch();
299+
const navigate = useNavigate();
300+
301+
const redirectToServices = async () => {
302+
// set the action filter value of services to true
303+
await dispatch(setSpecificServiceFilter({ filter: "actions", filterValue: "true" }));
304+
navigate("/systems/services");
305+
};
306+
310307
return (
311308
<ul className="dropdown-ul">
312309
{/* For each service in the serviceList (Background Services) one list item */}
313310
{healthStatus.map((service, key) => (
314311
<li key={key}>
315312
{!!service.status && (
316-
<Link
317-
to="/systems/services"
318-
onClick={async () => await redirectToServices()}
313+
<button
314+
className="button-like-anchor"
315+
onClick={() => redirectToServices()}
319316
>
320317
<span> {service.name} </span>
321318
{service.error ? (
@@ -327,7 +324,7 @@ const MenuNotify = ({
327324
{service.status}
328325
</span>
329326
)}
330-
</Link>
327+
</button>
331328
)}
332329
</li>
333330
))}

src/components/NavBar.tsx

Lines changed: 6 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,11 @@
11
import React, { useRef } from "react";
22
import { Link, useLocation } from "react-router";
33
import { hasAccess } from "../utils/utils";
4-
import { AppDispatch, useAppDispatch, useAppSelector } from "../store";
4+
import { useAppSelector } from "../store";
55
import { getUserInformation } from "../selectors/userInfoSelectors";
66
import cn from "classnames";
77
import { useTranslation } from "react-i18next";
88
import MainNav from "./shared/MainNav";
9-
import { setOffset } from "../slices/tableSlice";
109
import NewResourceModal, { NewResource } from "./shared/NewResourceModal";
1110
import { useHotkeys } from "react-hotkeys-hook";
1211
import { ModalHandle } from "./shared/modals/Modal";
@@ -15,13 +14,6 @@ import { ParseKeys } from "i18next";
1514
/**
1615
* Component that renders the nav bar
1716
*/
18-
type LinkType = {
19-
path: string
20-
accessRole: string
21-
loadFn: (dispatch: AppDispatch) => void
22-
text: ParseKeys
23-
}
24-
2517
type CreateType = {
2618
accessRole: string
2719
onShowModal?: () => Promise<void>
@@ -45,12 +37,14 @@ const NavBar = ({
4537
navAriaLabel?: ParseKeys
4638
displayNavigation: boolean
4739
setNavigation: React.Dispatch<React.SetStateAction<boolean>>
48-
links: LinkType[]
40+
links: {
41+
path: string
42+
accessRole: string
43+
text: ParseKeys
44+
}[]
4945
create?: CreateType
5046
}) => {
51-
5247
const { t } = useTranslation();
53-
const dispatch = useAppDispatch();
5448
const location = useLocation();
5549

5650
const user = useAppSelector(state => getUserInformation(state));
@@ -99,13 +93,6 @@ const NavBar = ({
9993
key={index}
10094
to={link.path}
10195
className={cn({ active: location.pathname === link.path || (location.pathname === "/" && link.path === "/events/events") })}
102-
onClick={() => {
103-
if (location.pathname !== link.path) {
104-
// Reset the current page to first page
105-
dispatch(setOffset(0));
106-
}
107-
link.loadFn(dispatch)
108-
}}
10996
>
11097
{t(link.text)}
11198
</Link>

src/components/configuration/Themes.tsx

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import MainView from "../MainView";
1313
import Footer from "../Footer";
1414
import { useAppDispatch, useAppSelector } from "../../store";
1515
import { fetchThemes } from "../../slices/themeSlice";
16+
import { resetTableProperties } from "../../slices/tableSlice";
1617

1718
/**
1819
* This component renders the table view of events
@@ -25,27 +26,37 @@ const Themes = () => {
2526

2627
const themes = useAppSelector(state => getTotalThemes(state));
2728

28-
const loadThemes = async () => {
29-
// Fetching themes from server
30-
await dispatch(fetchThemes());
29+
useEffect(() => {
30+
// State variable for interrupting the load function
31+
let allowLoadIntoTable = true;
3132

32-
// Load users into table
33-
dispatch(loadThemesIntoTable());
34-
};
33+
// Clear table of previous data
34+
dispatch(resetTableProperties());
3535

36-
useEffect(() => {
3736
dispatch(fetchFilters("themes"));
3837

3938
// Reset text filter
4039
dispatch(editTextFilter(""));
4140

4241
// Load themes on mount
42+
const loadThemes = async () => {
43+
// Fetching themes from server
44+
await dispatch(fetchThemes());
45+
46+
// Load users into table
47+
if (allowLoadIntoTable) {
48+
dispatch(loadThemesIntoTable());
49+
}
50+
};
4351
loadThemes();
4452

4553
// Fetch themes every minute
4654
let fetchThemesInterval = setInterval(loadThemes, 5000);
4755

48-
return () => clearInterval(fetchThemesInterval);
56+
return () => {
57+
allowLoadIntoTable = false;
58+
clearInterval(fetchThemesInterval);
59+
}
4960
// eslint-disable-next-line react-hooks/exhaustive-deps
5061
}, []);
5162

@@ -59,7 +70,6 @@ const Themes = () => {
5970
{
6071
path: "/configuration/themes",
6172
accessRole: "ROLE_UI_THEMES_VIEW",
62-
loadFn: loadThemes,
6373
text: "CONFIGURATION.NAVIGATION.THEMES"
6474
},
6575
]}

src/components/events/Events.tsx

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,9 @@ import {
3636
} from "../../slices/eventSlice";
3737
import EventDetailsModal from "./partials/modals/EventDetailsModal";
3838
import { showModal } from "../../selectors/eventDetailsSelectors";
39-
import { eventsLinks, loadEvents } from "./partials/EventsNavigation";
39+
import { eventsLinks } from "./partials/EventsNavigation";
4040
import { Modal, ModalHandle } from "../shared/modals/Modal";
41+
import { resetTableProperties } from "../../slices/tableSlice";
4142

4243
// References for detecting a click outside of the container of the dropdown menu
4344
const containerAction = React.createRef<HTMLDivElement>();
@@ -67,6 +68,12 @@ const Events = () => {
6768
let location = useLocation();
6869

6970
useEffect(() => {
71+
// State variable for interrupting the load function
72+
let allowLoadIntoTable = true;
73+
74+
// Clear redux of previous table data
75+
dispatch(resetTableProperties());
76+
7077
dispatch(fetchFilters("events"))
7178

7279
// Reset text filter
@@ -76,7 +83,17 @@ const Events = () => {
7683
dispatch(setShowActions(false));
7784

7885
// Load events on mount
79-
loadEvents(dispatch);
86+
const loadEvents = async () => {
87+
// Fetching events from server
88+
await dispatch(fetchEvents());
89+
90+
// Load events into table
91+
if (allowLoadIntoTable) {
92+
dispatch(loadEventsIntoTable());
93+
}
94+
}
95+
// call the function
96+
loadEvents();
8097

8198
// Function for handling clicks outside of an open dropdown menu
8299
const handleClickOutside = (e: MouseEvent) => {
@@ -89,12 +106,13 @@ const Events = () => {
89106
};
90107

91108
// Fetch events every five seconds
92-
let fetchEventsInterval = setInterval(() => loadEvents(dispatch), 5000);
109+
let fetchEventsInterval = setInterval(() => loadEvents(), 5000);
93110

94111
// Event listener for handle a click outside of dropdown menu
95112
window.addEventListener("mousedown", handleClickOutside);
96113

97114
return () => {
115+
allowLoadIntoTable = false;
98116
window.removeEventListener("mousedown", handleClickOutside);
99117
clearInterval(fetchEventsInterval);
100118
};

src/components/events/Series.tsx

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,10 @@ import {
2626
showActionsSeries,
2727
} from "../../slices/seriesSlice";
2828
import { fetchSeriesDetailsTobiraNew } from "../../slices/seriesSlice";
29-
import { eventsLinks, loadSeries } from "./partials/EventsNavigation";
29+
import { eventsLinks } from "./partials/EventsNavigation";
3030
import { Modal, ModalHandle } from "../shared/modals/Modal";
3131
import { availableHotkeys } from "../../configs/hotkeysConfig";
32+
import { resetTableProperties } from "../../slices/tableSlice";
3233

3334
// References for detecting a click outside of the container of the dropdown menu
3435
const containerAction = React.createRef<HTMLDivElement>();
@@ -52,6 +53,12 @@ const Series = () => {
5253
const showActions = useAppSelector(state => isShowActions(state));
5354

5455
useEffect(() => {
56+
// State variable for interrupting the load function
57+
let allowLoadIntoTable = true;
58+
59+
// Clear table of previous data
60+
dispatch(resetTableProperties());
61+
5562
dispatch(fetchFilters("series"))
5663

5764
// Reset text filer
@@ -61,7 +68,16 @@ const Series = () => {
6168
dispatch(showActionsSeries(false));
6269

6370
// Load events on mount
64-
loadSeries(dispatch);
71+
const loadSeries = async() => {
72+
// fetching series from server
73+
await dispatch(fetchSeries());
74+
75+
// load series into table
76+
if (allowLoadIntoTable) {
77+
dispatch(loadSeriesIntoTable());
78+
}
79+
};
80+
loadSeries();
6581

6682
// Function for handling clicks outside of an dropdown menu
6783
const handleClickOutside = (e: MouseEvent) => {
@@ -74,12 +90,13 @@ const Series = () => {
7490
};
7591

7692
// Fetch series every minute
77-
let fetchSeriesInterval = setInterval(() => loadSeries(dispatch), 5000);
93+
let fetchSeriesInterval = setInterval(() => loadSeries(), 5000);
7894

7995
// Event listener for handle a click outside of dropdown menu
8096
window.addEventListener("mousedown", handleClickOutside);
8197

8298
return () => {
99+
allowLoadIntoTable = false;
83100
window.removeEventListener("mousedown", handleClickOutside);
84101
clearInterval(fetchSeriesInterval);
85102
};
Lines changed: 0 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,49 +1,21 @@
11
import { ParseKeys } from "i18next";
2-
import { fetchEvents } from "../../../slices/eventSlice";
3-
import { fetchSeries } from "../../../slices/seriesSlice";
4-
import { fetchStats } from "../../../slices/tableFilterSlice";
5-
import { AppDispatch } from "../../../store";
6-
import { loadEventsIntoTable, loadSeriesIntoTable } from "../../../thunks/tableThunks";
72

83
/**
94
* Utility file for the navigation bar
105
*/
11-
12-
export const loadEvents = (dispatch: AppDispatch) => {
13-
// Fetching stats from server
14-
dispatch(fetchStats());
15-
16-
// Fetching events from server
17-
dispatch(fetchEvents());
18-
19-
// Load events into table
20-
dispatch(loadEventsIntoTable());
21-
};
22-
23-
export const loadSeries = (dispatch: AppDispatch) => {
24-
// fetching series from server
25-
dispatch(fetchSeries());
26-
27-
// load series into table
28-
dispatch(loadSeriesIntoTable());
29-
};
30-
316
export const eventsLinks: {
327
path: string,
338
accessRole: string,
34-
loadFn: (dispatch: AppDispatch) => void,
359
text: ParseKeys
3610
}[] = [
3711
{
3812
path: "/events/events",
3913
accessRole: "ROLE_UI_EVENTS_VIEW",
40-
loadFn: loadEvents,
4114
text: "EVENTS.EVENTS.NAVIGATION.EVENTS"
4215
},
4316
{
4417
path: "/events/series",
4518
accessRole: "ROLE_UI_SERIES_VIEW",
46-
loadFn: loadSeries,
4719
text: "EVENTS.EVENTS.NAVIGATION.SERIES"
4820
}
4921
];

0 commit comments

Comments
 (0)