Skip to content

Commit 06e887c

Browse files
authored
Fixes issue #186 : Build details not cleared on deleting a build. (#133)
* Fixes issue #186 : Build details not cleared on deleting a build.
1 parent 1603526 commit 06e887c

File tree

2 files changed

+34
-10
lines changed

2 files changed

+34
-10
lines changed

src/components/BuildList.tsx

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -187,8 +187,21 @@ const BuildList: FunctionComponent = () => {
187187
}?`}</Typography>
188188
}
189189
onSubmit={() => {
190+
let indexOfBuildDeleted = buildList.findIndex((e) => e.id === menuBuild.id);
191+
let indexOfSelectedBuild = buildList.findIndex((e) => e.id === selectedBuild?.id);
190192
deleteBuild(buildDispatch, menuBuild.id)
191193
.then((b) => {
194+
if (indexOfBuildDeleted === indexOfSelectedBuild) {
195+
if (buildList.length > 1) {
196+
if (indexOfBuildDeleted === 0) {
197+
selectBuild(buildDispatch, buildList[1].id);
198+
} else {
199+
selectBuild(buildDispatch, buildList[indexOfBuildDeleted - 1].id);
200+
}
201+
} else {
202+
selectBuild(buildDispatch, null);
203+
}
204+
}
192205
toggleDeleteDialogOpen();
193206
enqueueSnackbar(
194207
`Build #${menuBuild.number || menuBuild.id} deleted`,

src/contexts/build.context.tsx

Lines changed: 21 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ interface IGetAction {
1414

1515
interface ISelectAction {
1616
type: "select";
17-
payload: Build;
17+
payload: Build | null;
1818
}
1919

2020
interface IDeleteAction {
@@ -71,11 +71,18 @@ const initialState: State = {
7171
function buildReducer(state: State, action: IAction): State {
7272
switch (action.type) {
7373
case "select":
74-
return {
75-
...state,
76-
selectedBuildId: action.payload.id,
77-
selectedBuild: action.payload,
78-
};
74+
if (action.payload === null) {
75+
return {
76+
...state,
77+
selectedBuild: null
78+
};
79+
} else {
80+
return {
81+
...state,
82+
selectedBuildId: action.payload.id,
83+
selectedBuild: action.payload,
84+
};
85+
}
7986
case "request":
8087
return {
8188
...state,
@@ -173,10 +180,14 @@ async function stopBuild(dispatch: Dispatch, id: string) {
173180
});
174181
}
175182

176-
async function selectBuild(dispatch: Dispatch, id: string) {
177-
return buildsService.getDetails(id).then((build) => {
178-
dispatch({ type: "select", payload: build });
179-
});
183+
async function selectBuild(dispatch: Dispatch, id: string | null) {
184+
if (id === null) {
185+
dispatch({ type: "select", payload: null });
186+
} else {
187+
return buildsService.getDetails(id).then((build) => {
188+
dispatch({ type: "select", payload: build });
189+
});
190+
}
180191
}
181192

182193
async function addBuild(dispatch: Dispatch, build: Build) {

0 commit comments

Comments
 (0)