Skip to content

Commit

Permalink
fix: update VLANs on subnet/createNotify (#5181) lp#2037530
Browse files Browse the repository at this point in the history
  • Loading branch information
petermakowski authored Sep 27, 2023
1 parent bf430fd commit 8dc9b0c
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 0 deletions.
22 changes: 22 additions & 0 deletions src/app/store/vlan/reducers.test.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
import reducers, { actions } from "./slice";

import { actions as subnetActions } from "app/store/subnet/slice";
import {
vlan as vlanFactory,
vlanEventError as vlanEventErrorFactory,
vlanState as vlanStateFactory,
subnet as subnetFactory,
vlanStatus as vlanStatusFactory,
vlanStatuses as vlanStatusesFactory,
} from "testing/factories";
Expand Down Expand Up @@ -399,4 +401,24 @@ describe("vlan reducer", () => {
);
});
});

describe("subnet/createNotify", () => {
it("updates VLAN subnet_ids when a subnet is created", () => {
const vlan1 = vlanFactory({ id: 1, subnet_ids: [] });
const vlan2 = vlanFactory({ id: 2, subnet_ids: [] });
const initialState = vlanStateFactory({
items: [vlan1, vlan2],
});
const subnet = subnetFactory({ id: 3, vlan: 1 });
const expectedVlans = [{ ...vlan1, subnet_ids: [subnet.id] }, vlan2];

expect(
reducers(initialState, subnetActions.createNotify(subnet))
).toEqual(
vlanStateFactory({
items: expectedVlans,
})
);
});
});
});
18 changes: 18 additions & 0 deletions src/app/store/vlan/slice.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import type { PayloadAction } from "@reduxjs/toolkit";
import { createSlice } from "@reduxjs/toolkit";

import type { Subnet } from "../subnet/types";

import { VLANMeta } from "./types";
import type {
ConfigureDHCPParams,
Expand Down Expand Up @@ -169,6 +171,22 @@ const vlanSlice = createSlice({
state.active = action.payload ? action.payload[VLANMeta.PK] : null;
},
},
extraReducers: (builder) => {
// Add the newly created subnet's ID to the corresponding VLAN's subnet_ids array
builder.addCase(
"subnet/createNotify",
(state, action: PayloadAction<Subnet, "subnet/createNotify">) => {
const { id: subnetId, vlan: vlanId } = action.payload;

const vlanIndex = state.items.findIndex((item) => item.id === vlanId);

const vlanExists = vlanIndex !== -1;
if (vlanExists) {
state.items[vlanIndex].subnet_ids.push(subnetId);
}
}
);
},
});

export const { actions } = vlanSlice;
Expand Down

0 comments on commit 8dc9b0c

Please sign in to comment.