Skip to content

Commit

Permalink
handle filters when deleting kind/categories
Browse files Browse the repository at this point in the history
  • Loading branch information
Andrea-Papaleo committed Apr 16, 2024
1 parent cc91c83 commit 06317ac
Show file tree
Hide file tree
Showing 3 changed files with 107 additions and 2 deletions.
27 changes: 27 additions & 0 deletions src/store/imageViewer/imageViewerListeners.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import {
DecodedAnnotationObject,
ImageObject,
} from "store/data/types";
import { difference, intersection } from "lodash";

export const imageViewerMiddleware = createListenerMiddleware();
const startAppListening =
Expand Down Expand Up @@ -124,3 +125,29 @@ startAppListening({
listenerAPI.subscribe();
},
});

startAppListening({
predicate: (action, currentState, previousState) => {
return (
currentState.newData.categories.ids.length <
previousState.newData.categories.ids.length
);
},
effect: async (action, listenerApi) => {
const { imageViewer, newData } = listenerApi.getState();
const { newData: oldData } = listenerApi.getOriginalState();
const deletedCategories = difference(
oldData.categories.ids,
newData.categories.ids
) as string[];
const filteredCats = imageViewer.filters.categoryId;
const deletedFilters = intersection(filteredCats, deletedCategories);
if (deletedFilters.length > 0) {
listenerApi.dispatch(
imageViewerSlice.actions.removeFilters({
categoryIds: deletedFilters,
})
);
}
},
});
68 changes: 68 additions & 0 deletions src/store/project/projectListeners.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import { classifierSlice } from "store/classifier";
import { segmenterSlice } from "store/segmenter";
import { imageViewerSlice } from "store/imageViewer";
import { newDataSlice } from "store/data";
import { difference, intersection } from "lodash";
import { dataSlice } from "store/data/dataSlice";

export const projectMiddleware = createListenerMiddleware();
const startAppListening =
Expand Down Expand Up @@ -43,3 +45,69 @@ startAppListening({
listenerApi.subscribe();
},
});

startAppListening({
actionCreator: projectSlice.actions.resetProject,
effect: (action, listenerAPI) => {
listenerAPI.dispatch(newDataSlice.actions.resetData());
listenerAPI.dispatch(annotatorSlice.actions.resetAnnotator());
listenerAPI.dispatch(classifierSlice.actions.resetClassifier());
listenerAPI.dispatch(segmenterSlice.actions.resetSegmenter());
listenerAPI.dispatch(imageViewerSlice.actions.resetImageViewer());
},
});

startAppListening({
predicate: (action, currentState, previousState) => {
return (
currentState.newData.categories.ids.length <
previousState.newData.categories.ids.length
);
},
effect: async (action, listenerApi) => {
const { project, newData } = listenerApi.getState();
const { newData: oldData } = listenerApi.getOriginalState();
const deletedCategories = difference(
oldData.categories.ids,
newData.categories.ids
) as string[];
const filters = project.thingFilters;
for (let kind in filters) {
const filteredCats = filters[kind].categoryId;
const deletedFilters = intersection(filteredCats, deletedCategories);
if (deletedFilters.length > 0) {
listenerApi.dispatch(
projectSlice.actions.removeThingCategoryFilters({
categoryIds: deletedFilters,
kinds: [kind],
})
);
}
}
},
});

startAppListening({
actionCreator: dataSlice.actions.deleteKind,
effect: async (action, listenerApi) => {
const { newData } = listenerApi.getState();
const { newData: oldData } = listenerApi.getOriginalState();
const deletedKinds = difference(
oldData.kinds.ids,
newData.kinds.ids
) as string[];

listenerApi.dispatch(
projectSlice.actions.removeThingCategoryFilters({
categoryIds: "all",
kinds: deletedKinds,
})
);
listenerApi.dispatch(
projectSlice.actions.removeThingPartitionFilters({
partitions: "all",
kinds: deletedKinds,
})
);
},
});
14 changes: 12 additions & 2 deletions src/store/project/projectSlice.ts
Original file line number Diff line number Diff line change
Expand Up @@ -137,13 +137,18 @@ export const projectSlice = createSlice({
if (!(kind in state.thingFilters)) continue;
if (categoryIds === "all") {
state.thingFilters[kind].categoryId = [];
return;
} else {
mutatingFilter(
state.thingFilters[kind].categoryId,
(id) => !categoryIds!.includes(id)
);
}
if (
state.thingFilters[kind].categoryId.length === 0 &&
state.thingFilters[kind].partition.length === 0
) {
delete state.thingFilters[kind];
}
}
},
addThingPartitionFilters(
Expand Down Expand Up @@ -183,13 +188,18 @@ export const projectSlice = createSlice({
if (!(kind in state.thingFilters)) continue;
if (partitions === "all") {
state.thingFilters[kind].partition = [];
return;
} else {
mutatingFilter(
state.thingFilters[kind].partition,
(id) => !partitions.includes(id)
);
}
if (
state.thingFilters[kind].partition.length === 0 &&
state.thingFilters[kind].categoryId.length === 0
) {
delete state.thingFilters[kind];
}
}
},
},
Expand Down

0 comments on commit 06317ac

Please sign in to comment.