Skip to content

Commit

Permalink
fix: static routes edit and delete lp#2060176 lp#2060177
Browse files Browse the repository at this point in the history
  • Loading branch information
petermakowski committed Apr 4, 2024
1 parent 7deb99c commit 826030b
Show file tree
Hide file tree
Showing 9 changed files with 45 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ const mockStore = configureStore<RootState>();

const subnet = factory.subnet({ id: 1, cidr: "172.16.1.0/24" });
const destinationSubnet = factory.subnet({ id: 2, cidr: "223.16.1.0/24" });
const staticroute = factory.staticRoute({ id: 1, subnet: subnet.id });

Check failure on line 15 in src/app/subnets/views/SubnetDetails/StaticRoutes/DeleteStaticRouteform/DeleteStaticRouteForm.test.tsx

View workflow job for this annotation

GitHub Actions / Lint

Object literal may only specify known properties, and 'subnet' does not exist in type 'Partial<Config<StaticRoute>>'.
state = factory.rootState({
user: factory.userState({
auth: factory.authState({
Expand All @@ -21,7 +22,7 @@ state = factory.rootState({
}),
staticroute: factory.staticRouteState({
loaded: true,
items: [],
items: [staticroute],
}),
subnet: factory.subnetState({
loaded: true,
Expand All @@ -31,7 +32,10 @@ state = factory.rootState({

it("renders", () => {
renderWithBrowserRouter(
<DeleteStaticRouteForm id={subnet.id} setActiveForm={vi.fn()} />,
<DeleteStaticRouteForm
setActiveForm={vi.fn()}
staticRouteId={staticroute.id}
/>,
{ state }
);

Expand All @@ -41,7 +45,10 @@ it("renders", () => {
it("dispatches the correct action to delete a static route", async () => {
const store = mockStore(state);
renderWithBrowserRouter(
<DeleteStaticRouteForm id={subnet.id} setActiveForm={vi.fn()} />,
<DeleteStaticRouteForm
setActiveForm={vi.fn()}
staticRouteId={staticroute.id}
/>,
{ store }
);

Expand All @@ -51,5 +58,5 @@ it("dispatches the correct action to delete a static route", async () => {
.getActions()
.find((action) => action.type === staticRouteActions.delete.type);

expect(action).toStrictEqual(staticRouteActions.delete(subnet.id));
expect(action).toStrictEqual(staticRouteActions.delete(staticroute.id));
});
Original file line number Diff line number Diff line change
Expand Up @@ -4,25 +4,35 @@ import ModelActionForm from "@/app/base/components/ModelActionForm";
import type { SetSidePanelContent } from "@/app/base/side-panel-context";
import { staticRouteActions } from "@/app/store/staticroute";
import staticRouteSelectors from "@/app/store/staticroute/selectors";
import type { Subnet, SubnetMeta } from "@/app/store/subnet/types";
import type {
StaticRoute,
StaticRouteMeta,
} from "@/app/store/staticroute/types";

type Props = {
id: Subnet[SubnetMeta.PK];
staticRouteId?: StaticRoute[StaticRouteMeta.PK];
setActiveForm: SetSidePanelContent;
};

const DeleteStaticRouteForm = ({ id, setActiveForm }: Props) => {
const DeleteStaticRouteForm = ({ staticRouteId, setActiveForm }: Props) => {
const dispatch = useDispatch();
const saved = useSelector(staticRouteSelectors.saved);
const saving = useSelector(staticRouteSelectors.saving);

if (!staticRouteId) {
return null;
}
return (
<ModelActionForm
aria-label="Confirm static route deletion"
initialValues={{}}
modelType="static route"
onCancel={() => setActiveForm(null)}
onSubmit={() => {
dispatch(staticRouteActions.delete(id));
dispatch(staticRouteActions.delete(staticRouteId));
}}
onSuccess={() => {
setActiveForm(null);
}}
saved={saved}
saving={saving}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ it("displays loading text on load", async () => {
render(
<Provider store={store}>
<MemoryRouter initialEntries={[{ pathname: "/" }]}>
<EditStaticRouteForm id={1} setActiveForm={vi.fn()} />
<EditStaticRouteForm setActiveForm={vi.fn()} staticRouteId={1} />
</MemoryRouter>
</Provider>
);
Expand Down Expand Up @@ -69,7 +69,10 @@ it("dispatches a correct action on edit static route form submit", async () => {
render(
<Provider store={store}>
<MemoryRouter initialEntries={[{ pathname: "/" }]}>
<EditStaticRouteForm id={staticRoute.id} setActiveForm={vi.fn()} />
<EditStaticRouteForm
setActiveForm={vi.fn()}
staticRouteId={staticRoute.id}
/>
</MemoryRouter>
</Provider>
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,11 +39,11 @@ const editStaticRouteSchema = Yup.object().shape({
});

export type Props = {
id: StaticRoute[StaticRouteMeta.PK];
staticRouteId?: StaticRoute[StaticRouteMeta.PK];
setActiveForm: SetSidePanelContent;
};
const EditStaticRouteForm = ({
id,
staticRouteId,
setActiveForm,
}: Props): JSX.Element | null => {
const staticRouteErrors = useSelector(staticRouteSelectors.errors);
Expand All @@ -55,7 +55,7 @@ const EditStaticRouteForm = ({
const subnetsLoading = useSelector(subnetSelectors.loading);
const loading = staticRoutesLoading || subnetsLoading;
const staticRoute = useSelector((state: RootState) =>
staticRouteSelectors.getById(state, id)
staticRouteSelectors.getById(state, staticRouteId)
);
const source = useSelector((state: RootState) =>
subnetSelectors.getById(state, staticRoute?.source)
Expand Down Expand Up @@ -90,7 +90,7 @@ const EditStaticRouteForm = ({
dispatch(staticRouteActions.cleanup());
dispatch(
staticRouteActions.update({
id: id,
id: staticRouteId,

Check failure on line 93 in src/app/subnets/views/SubnetDetails/StaticRoutes/EditStaticRouteForm/EditStaticRouteForm.tsx

View workflow job for this annotation

GitHub Actions / Lint

Type 'number | undefined' is not assignable to type 'number'.
source: staticRoute.source,
gateway_ip,
destination: toFormikNumber(destination) as number,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,13 +67,15 @@ const generateRows = (
view: SubnetDetailsSidePanelViews[
SubnetActionTypes.DeleteStaticRoute
],
extras: { staticRouteId: staticRoute.id },
});
}}
onEdit={() => {
setSidePanelContent({
view: SubnetDetailsSidePanelViews[
SubnetActionTypes.EditStaticRoute
],
extras: { staticRouteId: staticRoute.id },
});
}}
/>
Expand Down
1 change: 1 addition & 0 deletions src/app/subnets/views/SubnetDetails/SubnetDetails.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ const SubnetDetails = (): JSX.Element => {
activeForm={activeForm}
id={subnet.id}
setActiveForm={setSidePanelContent}
{...sidePanelContent?.extras}
/>
) : null
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ const SubnetActionForms = ({
id,
activeForm,
setActiveForm,
staticRouteId,
}: SubnetActionProps): JSX.Element => {
const FormComponent = activeForm ? FormComponents[activeForm] : () => null;

Expand All @@ -40,6 +41,7 @@ const SubnetActionForms = ({
activeForm={activeForm}
id={id}
setActiveForm={setActiveForm}
staticRouteId={staticRouteId}
/>
);
};
Expand Down
2 changes: 1 addition & 1 deletion src/app/subnets/views/SubnetDetails/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,5 +47,5 @@ export const SubnetDetailsSidePanelViews = {

export type SubnetDetailsSidePanelContent = SidePanelContent<
ValueOf<typeof SubnetDetailsSidePanelViews>,
{ createType?: IPRangeType; ipRangeId?: number }
{ createType?: IPRangeType; ipRangeId?: number; staticRouteId?: number }
>;
5 changes: 5 additions & 0 deletions src/app/subnets/views/SubnetDetails/types.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,17 @@
import type { SubnetActionTypes } from "./constants";

import type { SetSidePanelContent } from "@/app/base/side-panel-context";
import type {
StaticRoute,
StaticRouteMeta,
} from "@/app/store/staticroute/types";
import type { Subnet, SubnetMeta } from "@/app/store/subnet/types";

export type SubnetAction = keyof typeof SubnetActionTypes;

export interface SubnetActionProps {
id: Subnet[SubnetMeta.PK];
staticRouteId?: StaticRoute[StaticRouteMeta.PK];
activeForm: SubnetAction;
setActiveForm: SetSidePanelContent;
}

0 comments on commit 826030b

Please sign in to comment.