Skip to content

Commit cadac76

Browse files
author
Caleb Ellis
committed
fix(ui): only fetch scripts if they have not been fetched before (#2210)
1 parent 184753f commit cadac76

File tree

5 files changed

+44
-15
lines changed

5 files changed

+44
-15
lines changed

ui/src/app/machines/views/MachineList/MachineListHeader/ActionFormWrapper/CommissionForm/CommissionForm.tsx

+6-2
Original file line numberDiff line numberDiff line change
@@ -95,8 +95,12 @@ export const CommissionForm = ({ setSelectedAction }: Props): JSX.Element => {
9595
);
9696

9797
useEffect(() => {
98-
dispatch(scriptActions.fetch());
99-
}, [dispatch]);
98+
if (!scriptsLoaded) {
99+
// scripts are fetched via http, so we explicitly check if they're already
100+
// loaded here.
101+
dispatch(scriptActions.fetch());
102+
}
103+
}, [dispatch, scriptsLoaded]);
100104

101105
return (
102106
<ActionForm

ui/src/app/machines/views/MachineList/MachineListHeader/ActionFormWrapper/TestForm/TestForm.js

+6-2
Original file line numberDiff line numberDiff line change
@@ -54,8 +54,12 @@ export const TestForm = ({ setSelectedAction }) => {
5454
}, {});
5555

5656
useEffect(() => {
57-
dispatch(scriptActions.fetch());
58-
}, [dispatch]);
57+
if (!scriptsLoaded) {
58+
// scripts are fetched via http, so we explicitly check if they're already
59+
// loaded here.
60+
dispatch(scriptActions.fetch());
61+
}
62+
}, [dispatch, scriptsLoaded]);
5963

6064
return (
6165
<ActionForm

ui/src/app/machines/views/MachineList/MachineListTable/MachineListTable.js

-2
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@ import ZoneColumn from "./ZoneColumn";
2121
import {
2222
general as generalActions,
2323
machine as machineActions,
24-
scripts as scriptActions,
2524
} from "app/base/actions";
2625
import TableHeader from "app/base/components/TableHeader";
2726
import { nodeStatus } from "app/base/enum";
@@ -497,7 +496,6 @@ export const MachineListTable = ({
497496
dispatch(generalActions.fetchPowerTypes());
498497
dispatch(generalActions.fetchVersion());
499498
dispatch(resourcePoolActions.fetch());
500-
dispatch(scriptActions.fetch());
501499
dispatch(tagActions.fetch());
502500
dispatch(userActions.fetch());
503501
dispatch(zoneActions.fetch());

ui/src/app/settings/views/Scripts/ScriptsList/ScriptsList.test.tsx

+26-7
Original file line numberDiff line numberDiff line change
@@ -68,8 +68,9 @@ describe("ScriptsList", () => {
6868
});
6969
});
7070

71-
it("dispatches action to fetch scripts load", () => {
71+
it("fetches scripts if they haven't been loaded yet", () => {
7272
const state = { ...initialState };
73+
state.scripts.loaded = false;
7374
const store = mockStore(state);
7475

7576
mount(
@@ -80,11 +81,27 @@ describe("ScriptsList", () => {
8081
</Provider>
8182
);
8283

83-
expect(store.getActions()).toEqual([
84-
{
85-
type: "FETCH_SCRIPTS",
86-
},
87-
]);
84+
expect(
85+
store.getActions().some((action) => action.type === "FETCH_SCRIPTS")
86+
).toBe(true);
87+
});
88+
89+
it("does not fetch scripts if they've already been loaded", () => {
90+
const state = { ...initialState };
91+
state.scripts.loaded = true;
92+
const store = mockStore(state);
93+
94+
mount(
95+
<Provider store={store}>
96+
<MemoryRouter initialEntries={[{ pathname: "/" }]}>
97+
<ScriptsList />
98+
</MemoryRouter>
99+
</Provider>
100+
);
101+
102+
expect(
103+
store.getActions().some((action) => action.type === "FETCH_SCRIPTS")
104+
).toBe(false);
88105
});
89106

90107
it("Displays commissioning scripts by default", () => {
@@ -194,7 +211,9 @@ describe("ScriptsList", () => {
194211
wrapper.find("TableRow").at(1).find("Button").at(1).simulate("click");
195212
// Click on the delete confirm button
196213
wrapper.find("TableRow").at(1).find("Button").at(3).simulate("click");
197-
expect(store.getActions()[1]).toEqual({
214+
expect(
215+
store.getActions().find((action) => action.type === "DELETE_SCRIPT")
216+
).toEqual({
198217
type: "DELETE_SCRIPT",
199218
payload: {
200219
id: 1,

ui/src/app/settings/views/Scripts/ScriptsList/ScriptsList.tsx

+6-2
Original file line numberDiff line numberDiff line change
@@ -171,8 +171,12 @@ const ScriptsList = ({ type = "commissioning" }: Props): JSX.Element => {
171171
};
172172

173173
useEffect(() => {
174-
dispatch(scriptActions.fetch());
175-
}, [dispatch, type]);
174+
if (!scriptsLoaded) {
175+
// scripts are fetched via http, so we explicitly check if they're already
176+
// loaded here.
177+
dispatch(scriptActions.fetch());
178+
}
179+
}, [dispatch, scriptsLoaded, type]);
176180

177181
return (
178182
<SettingsTable

0 commit comments

Comments
 (0)