Skip to content

Commit

Permalink
[MergeDups] Track deleted senses (#2953)
Browse files Browse the repository at this point in the history
  • Loading branch information
imnasnainaec authored Mar 26, 2024
1 parent 3312b57 commit 87e1ac9
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 15 deletions.
7 changes: 6 additions & 1 deletion src/goals/MergeDuplicates/MergeDupsTreeTypes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -94,8 +94,13 @@ export const defaultSidebar: Sidebar = {
};

export interface MergeTree {
deletedSenseGuids: string[];
sidebar: Sidebar;
words: Hash<MergeTreeWord>;
}

export const defaultTree: MergeTree = { sidebar: defaultSidebar, words: {} };
export const defaultTree: MergeTree = {
deletedSenseGuids: [],
sidebar: defaultSidebar,
words: {},
};
24 changes: 12 additions & 12 deletions src/goals/MergeDuplicates/Redux/MergeDupsReducer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import {
type Sidebar,
convertSenseToMergeTreeSense,
convertWordToMergeTreeWord,
defaultData,
defaultSidebar,
defaultTree,
newMergeTreeWord,
Expand Down Expand Up @@ -74,12 +75,13 @@ const mergeDuplicatesSlice = createSlice({
deleteSenseAction: (state, action) => {
const srcRef: MergeTreeReference = action.payload;
const srcWordId = srcRef.wordId;
const words = state.tree.words;
const { deletedSenseGuids, words } = state.tree;

const sensesGuids = words[srcWordId].sensesGuids;
const srcGuids = sensesGuids[srcRef.mergeSenseId];
if (srcRef.order === undefined || srcGuids.length === 1) {
// A sense deleted from a word.
deletedSenseGuids.push(...srcGuids);
delete sensesGuids[srcRef.mergeSenseId];
if (!Object.keys(sensesGuids).length) {
delete words[srcWordId];
Expand All @@ -92,6 +94,7 @@ const mergeDuplicatesSlice = createSlice({
}
} else {
// A sense deleted from the sidebar.
deletedSenseGuids.push(srcGuids[srcRef.order]);
srcGuids.splice(srcRef.order, 1);
if (srcGuids.length < 2) {
// If not multiple senses in the sidebar, reset the sidebar.
Expand All @@ -107,12 +110,9 @@ const mergeDuplicatesSlice = createSlice({
getMergeWordsAction: (state) => {
// Handle words with all senses deleted.
const possibleWords = Object.values(state.data.words);
// List of all non-deleted senses.
const nonDeletedSenses = Object.values(state.tree.words).flatMap((w) =>
Object.values(w.sensesGuids).flatMap((s) => s)
);
const deletedSenseGuids = state.tree.deletedSenseGuids;
const deletedWords = possibleWords.filter((w) =>
w.senses.every((s) => !nonDeletedSenses.includes(s.guid))
w.senses.every((s) => deletedSenseGuids.includes(s.guid))
);
state.mergeWords = deletedWords.map((w) =>
newMergeWords(w, [{ srcWordId: w.id, getAudio: false }], true)
Expand All @@ -123,7 +123,7 @@ const mergeDuplicatesSlice = createSlice({
const mergeSenses = buildSenses(
mergeWord.sensesGuids,
state.data,
nonDeletedSenses
deletedSenseGuids
);
const mergeWords = createMergeWords(
wordId,
Expand Down Expand Up @@ -265,7 +265,7 @@ const mergeDuplicatesSlice = createSlice({
});
wordsTree[word.id] = convertWordToMergeTreeWord(word);
});
state.data = { senses, words };
state.data = { ...defaultData, senses, words };
state.tree = { ...defaultTree, words: wordsTree };
state.mergeWords = [];
}
Expand All @@ -285,7 +285,7 @@ const mergeDuplicatesSlice = createSlice({
function buildSenses(
sensesGuids: Hash<string[]>,
data: MergeData,
nonDeletedSenses: string[]
deletedSenseGuids: string[]
): Hash<MergeTreeSense[]> {
const senses: Hash<MergeTreeSense[]> = {};
for (const senseGuids of Object.values(sensesGuids)) {
Expand All @@ -305,9 +305,9 @@ function buildSenses(
srcWordId: wordId,
sense: {
...sense,
accessibility: nonDeletedSenses.includes(sense.guid)
? Status.Separate
: Status.Deleted,
accessibility: deletedSenseGuids.includes(sense.guid)
? Status.Deleted
: Status.Separate,
},
});
}
Expand Down
12 changes: 10 additions & 2 deletions src/goals/MergeDuplicates/Redux/tests/MergeDupsActions.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -214,7 +214,11 @@ describe("MergeDupActions", () => {
it("delete one sense from word with multiple senses", async () => {
const WA = newMergeTreeWord(vernA, { ID1: [S1] });
const WB = newMergeTreeWord(vernB, { ID1: [S3], ID2: [S4] });
const tree: MergeTree = { ...defaultTree, words: { WA, WB } };
const tree: MergeTree = {
...defaultTree,
deletedSenseGuids: [S2],
words: { WA, WB },
};
const store = setupStore({
...preloadedState,
mergeDuplicateGoal: { ...defaultMergeState, data, tree },
Expand All @@ -237,7 +241,11 @@ describe("MergeDupActions", () => {
// Delete both senses from B
it("delete all senses from a word", async () => {
const WA = newMergeTreeWord(vernA, { ID1: [S1], ID2: [S2] });
const tree: MergeTree = { ...defaultTree, words: { WA } };
const tree: MergeTree = {
...defaultTree,
deletedSenseGuids: [S3, S4],
words: { WA },
};
const store = setupStore({
...preloadedState,
mergeDuplicateGoal: { ...defaultMergeState, data, tree },
Expand Down

0 comments on commit 87e1ac9

Please sign in to comment.