Skip to content

Commit 025510a

Browse files
authored
fix(settings): Only update the image source if bootResource.fetch succeeds (#5837)
- Moved `saveUbuntu` and `saveOther` to the `onSuccess` callback of the image source change form - (driveby) use accessible attributes for testing the form Fixes [maas-ui#5836](#5836)
1 parent 018c58c commit 025510a

File tree

2 files changed

+15
-12
lines changed

2 files changed

+15
-12
lines changed

src/app/settings/views/Images/ChangeSource/ChangeSource.test.tsx

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,9 @@ describe("ChangeSource", () => {
2929
});
3030

3131
await userEvent
32-
.click(screen.getByTestId("auto-sync-switch"))
32+
.click(
33+
screen.getByRole("checkbox", { name: /Automatically sync images/i })
34+
)
3335
.then(async () => {
3436
await userEvent.click(screen.getByText("Save"));
3537
});
@@ -38,6 +40,7 @@ describe("ChangeSource", () => {
3840
const expectedAction = configActions.update({
3941
boot_images_auto_import: false,
4042
});
43+
4144
expect(
4245
actualActions.find(
4346
(actualAction) => actualAction.type === expectedAction.type

src/app/settings/views/Images/ChangeSource/ChangeSource.tsx

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -63,10 +63,19 @@ const ChangeSource = (): ReactElement => {
6363
const saved = !saving && previousSaving && !errors;
6464

6565
useWindowTitle("Source");
66+
useEffect(() => {
67+
dispatch(bootResourceActions.poll({ continuous: false }));
68+
dispatch(configActions.fetch());
69+
return () => {
70+
dispatch(bootResourceActions.pollStop());
71+
dispatch(bootResourceActions.cleanup());
72+
};
73+
}, [dispatch]);
6674

6775
const canChangeSource = resources.every((resource) => !resource.downloading);
6876
const source: BootResourceUbuntuSource =
6977
sources !== null &&
78+
!!sources.sources[0].source_type &&
7079
sources.sources[0].source_type === BootResourceSourceType.CUSTOM
7180
? sources.sources[0]
7281
: {
@@ -140,15 +149,6 @@ const ChangeSource = (): ReactElement => {
140149
}
141150
}, [sources, resources, otherImages]);
142151

143-
useEffect(() => {
144-
dispatch(bootResourceActions.poll({ continuous: false }));
145-
dispatch(configActions.fetch());
146-
return () => {
147-
dispatch(bootResourceActions.pollStop());
148-
dispatch(bootResourceActions.cleanup());
149-
};
150-
}, [dispatch]);
151-
152152
return (
153153
<PageContent sidePanelContent={null} sidePanelTitle={null}>
154154
<ContentSection variant="narrow">
@@ -182,6 +182,8 @@ const ChangeSource = (): ReactElement => {
182182
boot_images_auto_import: values.autoSync,
183183
})
184184
);
185+
}}
186+
onSuccess={(values) => {
185187
dispatch(
186188
bootResourceActions.saveUbuntu({
187189
keyring_data: values.keyring_data,
@@ -191,7 +193,6 @@ const ChangeSource = (): ReactElement => {
191193
osystems: ubuntuSystems,
192194
})
193195
);
194-
dispatch(bootResourceActions.saveUbuntuSuccess());
195196
dispatch(
196197
bootResourceActions.saveOther({
197198
images: otherSystems.map(
@@ -200,7 +201,6 @@ const ChangeSource = (): ReactElement => {
200201
),
201202
})
202203
);
203-
dispatch(bootResourceActions.saveOtherSuccess());
204204
}}
205205
saved={saved}
206206
saving={saving}

0 commit comments

Comments
 (0)