diff --git a/src/components/BuildList.tsx b/src/components/BuildList.tsx index a236362a..39181a7c 100644 --- a/src/components/BuildList.tsx +++ b/src/components/BuildList.tsx @@ -187,8 +187,21 @@ const BuildList: FunctionComponent = () => { }?`} } onSubmit={() => { + let indexOfBuildDeleted = buildList.findIndex((e) => e.id === menuBuild.id); + let indexOfSelectedBuild = buildList.findIndex((e) => e.id === selectedBuild?.id); deleteBuild(buildDispatch, menuBuild.id) .then((b) => { + if (indexOfBuildDeleted === indexOfSelectedBuild) { + if (buildList.length > 1) { + if (indexOfBuildDeleted === 0) { + selectBuild(buildDispatch, buildList[1].id); + } else { + selectBuild(buildDispatch, buildList[indexOfBuildDeleted - 1].id); + } + } else { + selectBuild(buildDispatch, null); + } + } toggleDeleteDialogOpen(); enqueueSnackbar( `Build #${menuBuild.number || menuBuild.id} deleted`, diff --git a/src/contexts/build.context.tsx b/src/contexts/build.context.tsx index da11ed7f..976ee455 100644 --- a/src/contexts/build.context.tsx +++ b/src/contexts/build.context.tsx @@ -14,7 +14,7 @@ interface IGetAction { interface ISelectAction { type: "select"; - payload: Build; + payload: Build | null; } interface IDeleteAction { @@ -71,11 +71,18 @@ const initialState: State = { function buildReducer(state: State, action: IAction): State { switch (action.type) { case "select": - return { - ...state, - selectedBuildId: action.payload.id, - selectedBuild: action.payload, - }; + if (action.payload === null) { + return { + ...state, + selectedBuild: null + }; + } else { + return { + ...state, + selectedBuildId: action.payload.id, + selectedBuild: action.payload, + }; + } case "request": return { ...state, @@ -173,10 +180,14 @@ async function stopBuild(dispatch: Dispatch, id: string) { }); } -async function selectBuild(dispatch: Dispatch, id: string) { - return buildsService.getDetails(id).then((build) => { - dispatch({ type: "select", payload: build }); - }); +async function selectBuild(dispatch: Dispatch, id: string | null) { + if (id === null) { + dispatch({ type: "select", payload: null }); + } else { + return buildsService.getDetails(id).then((build) => { + dispatch({ type: "select", payload: build }); + }); + } } async function addBuild(dispatch: Dispatch, build: Build) {