From 8e99a5f67d81f31782276be31d8b46566a4806a0 Mon Sep 17 00:00:00 2001 From: Surat Das Date: Wed, 20 Jan 2021 10:06:23 -0800 Subject: [PATCH 1/2] Fixes issue #186 : Build details not cleared on deleting a build. --- src/components/BuildList.tsx | 11 +++++++++++ src/contexts/build.context.tsx | 17 ++++++++++++++++- 2 files changed, 27 insertions(+), 1 deletion(-) diff --git a/src/components/BuildList.tsx b/src/components/BuildList.tsx index a236362a..1a58fe1b 100644 --- a/src/components/BuildList.tsx +++ b/src/components/BuildList.tsx @@ -23,6 +23,7 @@ import { useBuildDispatch, deleteBuild, selectBuild, + clearBuild, stopBuild, } from "../contexts"; import { BuildStatusChip } from "./BuildStatusChip"; @@ -187,8 +188,18 @@ const BuildList: FunctionComponent = () => { }?`} } onSubmit={() => { + let indexOfBuildDeleted = buildList.findIndex((e) => e.id === menuBuild.id); deleteBuild(buildDispatch, menuBuild.id) .then((b) => { + if (buildList.length > 1) { + if (indexOfBuildDeleted === 0) { + selectBuild(buildDispatch, buildList[1].id); + } else { + selectBuild(buildDispatch, buildList[indexOfBuildDeleted - 1].id); + } + } else { + clearBuild(buildDispatch); + } toggleDeleteDialogOpen(); enqueueSnackbar( `Build #${menuBuild.number || menuBuild.id} deleted`, diff --git a/src/contexts/build.context.tsx b/src/contexts/build.context.tsx index da11ed7f..8e15657a 100644 --- a/src/contexts/build.context.tsx +++ b/src/contexts/build.context.tsx @@ -32,13 +32,18 @@ interface IUpdateAction { payload: Build; } +interface IEmptyAction { + type: "empty"; +} + type IAction = | IRequestAction | IGetAction | IDeleteAction | IAddAction | IUpdateAction - | ISelectAction; + | ISelectAction + | IEmptyAction; type Dispatch = (action: IAction) => void; type State = { @@ -82,6 +87,11 @@ function buildReducer(state: State, action: IAction): State { buildList: [], loading: true, }; + case "empty": + return { + ...state, + selectedBuild: null + }; case "get": const { data, take, skip, total } = action.payload; return { @@ -179,6 +189,10 @@ async function selectBuild(dispatch: Dispatch, id: string) { }); } +async function clearBuild(dispatch: Dispatch) { + dispatch({ type: "empty" }); +} + async function addBuild(dispatch: Dispatch, build: Build) { dispatch({ type: "add", payload: build }); } @@ -194,6 +208,7 @@ export { getBuildList, deleteBuild, selectBuild, + clearBuild, addBuild, updateBuild, stopBuild, From 0d61ee6bca2a9f654db81e9275072f969641cf6b Mon Sep 17 00:00:00 2001 From: Surat Das Date: Thu, 21 Jan 2021 15:22:14 -0800 Subject: [PATCH 2/2] Fixed review comments. --- src/components/BuildList.tsx | 16 +++++++----- src/contexts/build.context.tsx | 48 ++++++++++++++++------------------ 2 files changed, 31 insertions(+), 33 deletions(-) diff --git a/src/components/BuildList.tsx b/src/components/BuildList.tsx index 1a58fe1b..39181a7c 100644 --- a/src/components/BuildList.tsx +++ b/src/components/BuildList.tsx @@ -23,7 +23,6 @@ import { useBuildDispatch, deleteBuild, selectBuild, - clearBuild, stopBuild, } from "../contexts"; import { BuildStatusChip } from "./BuildStatusChip"; @@ -189,16 +188,19 @@ 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 (buildList.length > 1) { - if (indexOfBuildDeleted === 0) { - selectBuild(buildDispatch, buildList[1].id); + 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, buildList[indexOfBuildDeleted - 1].id); + selectBuild(buildDispatch, null); } - } else { - clearBuild(buildDispatch); } toggleDeleteDialogOpen(); enqueueSnackbar( diff --git a/src/contexts/build.context.tsx b/src/contexts/build.context.tsx index 8e15657a..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 { @@ -32,18 +32,13 @@ interface IUpdateAction { payload: Build; } -interface IEmptyAction { - type: "empty"; -} - type IAction = | IRequestAction | IGetAction | IDeleteAction | IAddAction | IUpdateAction - | ISelectAction - | IEmptyAction; + | ISelectAction; type Dispatch = (action: IAction) => void; type State = { @@ -76,22 +71,24 @@ 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, buildList: [], loading: true, }; - case "empty": - return { - ...state, - selectedBuild: null - }; case "get": const { data, take, skip, total } = action.payload; return { @@ -183,14 +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 clearBuild(dispatch: Dispatch) { - dispatch({ type: "empty" }); +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) { @@ -208,7 +205,6 @@ export { getBuildList, deleteBuild, selectBuild, - clearBuild, addBuild, updateBuild, stopBuild,