Skip to content

Commit

Permalink
refactor: add generateGetReducers util MAASENG-3132 WIP (#5428)
Browse files Browse the repository at this point in the history
- remove workarounds for error return type https://bugs.launchpad.net/maas/+bug/1931654
- refactor controller, device, iprange, pod, subnet, vlan, domain, iprange, space reducers
  • Loading branch information
petermakowski authored May 9, 2024
1 parent 5f66184 commit f2b18aa
Show file tree
Hide file tree
Showing 11 changed files with 124 additions and 438 deletions.
50 changes: 7 additions & 43 deletions src/app/store/controller/slice.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import { NodeActions } from "@/app/store/types/node";
import type { GenericItemMeta, StatusHandlers } from "@/app/store/utils/slice";
import {
generateCommonReducers,
generateGetReducers,
generateStatusHandlers,
genericInitialState,
updateErrors,
Expand Down Expand Up @@ -289,49 +290,12 @@ const controllerSlice = createSlice({
state.loading = false;
state.loaded = true;
},
get: {
prepare: (id: Controller[ControllerMeta.PK]) => ({
meta: {
model: ControllerMeta.MODEL,
method: "get",
},
payload: {
params: { [ControllerMeta.PK]: id },
},
}),
reducer: () => {
// No state changes need to be handled for this action.
},
},
getError: (
state: ControllerState,
action: PayloadAction<ControllerState["errors"]>
) => {
state.errors = action.payload;
state = setErrors(state, action, "get");
state.loading = false;
state.saving = false;
},
getStart: (state: ControllerState) => {
state.loading = true;
},
getSuccess: (state: ControllerState, action: PayloadAction<Controller>) => {
const controller = action.payload;
// If the item already exists, update it, otherwise
// add it to the store.
const i = state.items.findIndex(
(draftItem: Controller) =>
draftItem[ControllerMeta.PK] === controller[ControllerMeta.PK]
);
if (i !== -1) {
state.items[i] = controller;
} else {
state.items.push(controller);
// Set up the statuses for this controller.
state.statuses[controller[ControllerMeta.PK]] = DEFAULT_STATUSES;
}
state.loading = false;
},
...generateGetReducers<ControllerState, Controller, ControllerMeta.PK>({
modelName: ControllerMeta.MODEL,
primaryKey: ControllerMeta.PK,
defaultStatuses: DEFAULT_STATUSES,
setErrors,
}),
getSummaryXml: {
prepare: (params: GetSummaryXmlParams) => ({
meta: {
Expand Down
50 changes: 7 additions & 43 deletions src/app/store/device/slice.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import type {
import { NodeActions } from "@/app/store/types/node";
import {
generateCommonReducers,
generateGetReducers,
generateStatusHandlers,
genericInitialState,
updateErrors,
Expand Down Expand Up @@ -225,49 +226,12 @@ const deviceSlice = createSlice({
deleteInterfaceError: statusHandlers.deleteInterface.error,
deleteInterfaceStart: statusHandlers.deleteInterface.start,
deleteInterfaceSuccess: statusHandlers.deleteInterface.success,
get: {
prepare: (id: Device[DeviceMeta.PK]) => ({
meta: {
model: DeviceMeta.MODEL,
method: "get",
},
payload: {
params: { [DeviceMeta.PK]: id },
},
}),
reducer: () => {
// No state changes need to be handled for this action.
},
},
getError: (
state: DeviceState,
action: PayloadAction<DeviceState["errors"]>
) => {
state.errors = action.payload;
state = setErrors(state, action, "get");
state.loading = false;
state.saving = false;
},
getStart: (state: DeviceState) => {
state.loading = true;
},
getSuccess: (state: DeviceState, action: PayloadAction<Device>) => {
const device = action.payload;
// If the item already exists, update it, otherwise
// add it to the store.
const i = state.items.findIndex(
(draftItem: Device) =>
draftItem[DeviceMeta.PK] === device[DeviceMeta.PK]
);
if (i !== -1) {
state.items[i] = device;
} else {
state.items.push(device);
// Set up the statuses for this device.
state.statuses[device[DeviceMeta.PK]] = DEFAULT_STATUSES;
}
state.loading = false;
},
...generateGetReducers({
modelName: DeviceMeta.MODEL,
primaryKey: DeviceMeta.PK,
defaultStatuses: DEFAULT_STATUSES,
setErrors,
}),
linkSubnet: {
prepare: (params: LinkSubnetParams) => ({
meta: {
Expand Down
44 changes: 0 additions & 44 deletions src/app/store/domain/reducers.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -199,21 +199,6 @@ describe("domain reducer", () => {
);
});

// Related to: https://bugs.launchpad.net/maas/+bug/1931654.
it("reduces getError when the error when a domain can't be found", () => {
const domainState = factory.domainState({
errors: null,
saving: true,
});

expect(reducers(domainState, actions.getError("9"))).toEqual(
factory.domainState({
errors: "There was an error getting the domain.",
saving: false,
})
);
});

it("reduces setDefaultError", () => {
const domainState = factory.domainState({
errors: null,
Expand All @@ -230,21 +215,6 @@ describe("domain reducer", () => {
);
});

// Related to: https://bugs.launchpad.net/maas/+bug/1931654.
it("reduces setDefaultError when the error when a domain can't be found", () => {
const domainState = factory.domainState({
errors: null,
saving: true,
});

expect(reducers(domainState, actions.setDefaultError("9"))).toEqual(
factory.domainState({
errors: "There was an error when setting default domain.",
saving: false,
})
);
});

it("reduces setDefaultSuccess", () => {
const domain1 = factory.domain({ id: 1, is_default: true });
const domain2 = factory.domain({ id: 2, is_default: false });
Expand Down Expand Up @@ -291,20 +261,6 @@ describe("domain reducer", () => {
);
});

// Related to: https://bugs.launchpad.net/maas/+bug/1931654.
it("reduces setActiveError when the error when a domain can't be found", () => {
const domainState = factory.domainState({
errors: null,
});

expect(reducers(domainState, actions.setActiveError("9"))).toEqual(
factory.domainState({
errors: "There was an error when setting active domain.",
saving: false,
})
);
});

it("reduces setActiveSuccess", () => {
const podState = factory.domainState({
active: null,
Expand Down
76 changes: 7 additions & 69 deletions src/app/store/domain/slice.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import type {
import type { APIError } from "@/app/base/types";
import {
generateCommonReducers,
generateGetReducers,
genericInitialState,
} from "@/app/store/utils/slice";

Expand All @@ -39,55 +40,10 @@ const domainSlice = createSlice({
CreateParams,
UpdateParams
>(DomainMeta.MODEL, DomainMeta.PK),
get: {
prepare: (id: Domain[DomainMeta.PK]) => ({
meta: {
model: DomainMeta.MODEL,
method: "get",
},
payload: {
params: { id },
},
}),
reducer: () => {
// No state changes need to be handled for this action.
},
},
getStart: (state: DomainState) => {
state.loading = true;
},
getError: (
state: DomainState,
action: PayloadAction<DomainState["errors"]>
) => {
// API seems to return the domain id in payload.error not an error message
// when the domain can't be found. This override can be removed when the
// bug is fixed: https://bugs.launchpad.net/maas/+bug/1931654.
if (!isNaN(Number(action.payload))) {
// returned error string is a number (id of the domain)
state.errors = "There was an error getting the domain.";
} else {
// returned error string is an error message
state.errors = action.payload;
}

state.loading = false;
state.saving = false;
},
getSuccess: (state: DomainState, action: PayloadAction<Domain>) => {
const domain = action.payload;
// If the item already exists, update it, otherwise
// add it to the store.
const i = state.items.findIndex(
(draftItem: Domain) => draftItem.id === domain.id
);
if (i !== -1) {
state.items[i] = domain;
} else {
state.items.push(domain);
}
state.loading = false;
},
...generateGetReducers<DomainState, Domain, DomainMeta.PK>({
modelName: DomainMeta.MODEL,
primaryKey: DomainMeta.PK,
}),
setDefault: {
prepare: (id: Domain[DomainMeta.PK]) => ({
meta: {
Expand All @@ -111,16 +67,7 @@ const domainSlice = createSlice({
action: PayloadAction<SetDefaultErrors>
) => {
state.saving = false;
// API seems to return the domain id in payload.error not an error message
// when the domain can't be found. This override can be removed when the
// bug is fixed: https://bugs.launchpad.net/maas/+bug/1931654.
if (!isNaN(Number(action.payload))) {
// returned error string is a number (id of the domain)
state.errors = "There was an error when setting default domain.";
} else {
// returned error string is an error message
state.errors = action.payload;
}
state.errors = action.payload;
},
setDefaultSuccess: (state: DomainState, action: PayloadAction<Domain>) => {
state.saving = false;
Expand Down Expand Up @@ -156,16 +103,7 @@ const domainSlice = createSlice({
action: PayloadAction<DomainState["errors"]>
) => {
state.active = null;
// API seems to return the domain id in payload.error not an error message
// when the domain can't be found. This override can be removed when the
// bug is fixed: https://bugs.launchpad.net/maas/+bug/1931654.
if (!isNaN(Number(action.payload))) {
// returned error string is a number (id of the domain)
state.errors = "There was an error when setting active domain.";
} else {
// returned error string is an error message
state.errors = action.payload;
}
state.errors = action.payload;
},
setActiveSuccess: (
state: DomainState,
Expand Down
45 changes: 5 additions & 40 deletions src/app/store/fabric/slice.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import type { CreateParams, Fabric, FabricState, UpdateParams } from "./types";

import {
generateCommonReducers,
generateGetReducers,
genericInitialState,
} from "@/app/store/utils/slice";

Expand All @@ -22,46 +23,10 @@ const fabricSlice = createSlice({
CreateParams,
UpdateParams
>(FabricMeta.MODEL, FabricMeta.PK),
get: {
prepare: (id: Fabric[FabricMeta.PK]) => ({
meta: {
model: FabricMeta.MODEL,
method: "get",
},
payload: {
params: { [FabricMeta.PK]: id },
},
}),
reducer: () => {
// No state changes need to be handled for this action.
},
},
getError: (
state: FabricState,
action: PayloadAction<FabricState["errors"]>
) => {
state.errors = action.payload;
state.loading = false;
state.saving = false;
},
getStart: (state: FabricState) => {
state.loading = true;
},
getSuccess: (state: FabricState, action: PayloadAction<Fabric>) => {
const fabric = action.payload;
// If the item already exists, update it, otherwise
// add it to the store.
const i = state.items.findIndex(
(draftItem: Fabric) =>
draftItem[FabricMeta.PK] === fabric[FabricMeta.PK]
);
if (i !== -1) {
state.items[i] = fabric;
} else {
state.items.push(fabric);
}
state.loading = false;
},
...generateGetReducers<FabricState, Fabric, FabricMeta.PK>({
modelName: FabricMeta.MODEL,
primaryKey: FabricMeta.PK,
}),
setActive: {
prepare: (id: Fabric[FabricMeta.PK] | null) => ({
meta: {
Expand Down
Loading

0 comments on commit f2b18aa

Please sign in to comment.