Skip to content

Commit

Permalink
Unify sense-reference info
Browse files Browse the repository at this point in the history
  • Loading branch information
imnasnainaec committed Dec 16, 2024
1 parent 5251099 commit 57125ec
Show file tree
Hide file tree
Showing 8 changed files with 40 additions and 48 deletions.
35 changes: 14 additions & 21 deletions src/goals/MergeDuplicates/MergeDupsStep/MergeDragDrop/DragSense.tsx
Original file line number Diff line number Diff line change
@@ -1,22 +1,23 @@
import { Card } from "@mui/material";
import { ReactElement, useCallback, useEffect, useState } from "react";
import { type ReactElement, useCallback, useEffect, useState } from "react";
import { Draggable } from "react-beautiful-dnd";

import { trashId } from "goals/MergeDuplicates/MergeDupsStep/MergeDragDrop";
import SenseCardContent from "goals/MergeDuplicates/MergeDupsStep/SenseCardContent";
import { MergeTreeSense } from "goals/MergeDuplicates/MergeDupsTreeTypes";
import {
type MergeTreeReference,
type MergeTreeSense,
} from "goals/MergeDuplicates/MergeDupsTreeTypes";
import { setSidebar } from "goals/MergeDuplicates/Redux/MergeDupsActions";
import { useAppDispatch, useAppSelector } from "rootRedux/hooks";
import { type StoreState } from "rootRedux/types";
import theme from "types/theme";

interface DragSenseProps {
index: number;
wordId: string;
mergeSenseId: string;
mergeSenses: MergeTreeSense[];
isOnlySenseInProtectedWord: boolean;
isProtectedSense: boolean;
mergeSenses: MergeTreeSense[];
senseRef: MergeTreeReference;
}

function arraysEqual<T>(arr1: T[], arr2: T[]): boolean {
Expand Down Expand Up @@ -45,17 +46,13 @@ export default function DragSense(props: DragSenseProps): ReactElement {
(state: StoreState) => state.mergeDuplicateGoal.tree.sidebar
);
const isInSidebar =
sidebar.wordId === props.wordId &&
sidebar.mergeSenseId === props.mergeSenseId &&
sidebar.senseRef.wordId === props.senseRef.wordId &&
sidebar.senseRef.mergeSenseId === props.senseRef.mergeSenseId &&
sidebar.mergeSenses.length > 1;

const updateSidebar = useCallback(() => {
dispatch(
setSidebar({
mergeSenses: props.mergeSenses,
wordId: props.wordId,
mergeSenseId: props.mergeSenseId,
})
setSidebar({ mergeSenses: props.mergeSenses, senseRef: props.senseRef })
);
}, [dispatch, props]);

Expand Down Expand Up @@ -88,12 +85,8 @@ export default function DragSense(props: DragSenseProps): ReactElement {

return (
<Draggable
key={props.mergeSenseId}
draggableId={JSON.stringify({
wordId: props.wordId,
mergeSenseId: props.mergeSenseId,
isSenseProtected: props.isProtectedSense,
})}
key={props.senseRef.mergeSenseId}
draggableId={JSON.stringify(props.senseRef)}
index={props.index}
isDragDisabled={props.isOnlySenseInProtectedWord}
>
Expand All @@ -109,13 +102,13 @@ export default function DragSense(props: DragSenseProps): ReactElement {
minWidth: 150,
maxWidth: 300,
opacity:
!props.isProtectedSense &&
!props.senseRef.isSenseProtected &&
(snapshot.draggingOver === trashId || snapshot.combineWith)
? 0.7
: 1,
background: isInSidebar
? "lightblue"
: props.isProtectedSense
: props.senseRef.isSenseProtected
? "lightyellow"
: snapshot.draggingOver === trashId
? "red"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,11 +75,13 @@ export default function DropWord(props: DropWordProps): ReactElement {
<DragSense
key={id}
index={index}
wordId={props.wordId}
mergeSenseId={id}
mergeSenses={senses}
senseRef={{
isSenseProtected: senses[0].protected,
mergeSenseId: id,
wordId: props.wordId,
}}
isOnlySenseInProtectedWord={protectedWithOneChild}
isProtectedSense={senses[0].protected}
/>
);
})}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,7 @@ import { Draggable } from "react-beautiful-dnd";

import { trashId } from "goals/MergeDuplicates/MergeDupsStep/MergeDragDrop";
import SenseCardContent from "goals/MergeDuplicates/MergeDupsStep/SenseCardContent";
import {
MergeTreeReference,
MergeTreeSense,
} from "goals/MergeDuplicates/MergeDupsTreeTypes";
import { MergeTreeSense } from "goals/MergeDuplicates/MergeDupsTreeTypes";
import { useAppSelector } from "rootRedux/hooks";
import { type StoreState } from "rootRedux/types";
import theme from "types/theme";
Expand All @@ -21,10 +18,8 @@ export default function SidebarDragSense(
props: SidebarDragSenseProps
): ReactElement {
const draggableId = useAppSelector((state: StoreState) => {
const { mergeSenseId, wordId } = state.mergeDuplicateGoal.tree.sidebar;
const order = props.index;
const ref: MergeTreeReference = { wordId, mergeSenseId, order };
return JSON.stringify(ref);
const ref = state.mergeDuplicateGoal.tree.sidebar.senseRef;
return JSON.stringify({ ...ref, order: props.index });
});

return (
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { ArrowForwardIos, HelpOutline } from "@mui/icons-material";
import { Grid, IconButton, Typography } from "@mui/material";
import { ReactElement } from "react";
import { type ReactElement } from "react";
import { Droppable } from "react-beautiful-dnd";

import SidebarDragSense from "goals/MergeDuplicates/MergeDupsStep/MergeDragDrop/SidebarDragSense";
import { MergeTreeSense } from "goals/MergeDuplicates/MergeDupsTreeTypes";
import { type MergeTreeSense } from "goals/MergeDuplicates/MergeDupsTreeTypes";
import { setSidebar } from "goals/MergeDuplicates/Redux/MergeDupsActions";
import { useAppDispatch, useAppSelector } from "rootRedux/hooks";
import { type StoreState } from "rootRedux/types";
Expand All @@ -15,16 +15,14 @@ export default function SidebarDrop(): ReactElement {
const sidebar = useAppSelector(
(state: StoreState) => state.mergeDuplicateGoal.tree.sidebar
);
const { mergeSenseId, wordId } = sidebar.senseRef;
const vernacular = useAppSelector((state: StoreState) => {
const tree = state.mergeDuplicateGoal.tree;
return tree.words[tree.sidebar.wordId]?.vern;
return tree.words[tree.sidebar.senseRef.wordId]?.vern;
});

return (
<Droppable
droppableId={`${sidebar.wordId} ${sidebar.mergeSenseId}`}
key={sidebar.mergeSenseId}
>
<Droppable droppableId={`${wordId} ${mergeSenseId}`} key={mergeSenseId}>
{(providedDroppable): ReactElement => (
<div
ref={providedDroppable.innerRef}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,6 @@ describe("MergeDragDrop", () => {
});
expect(mockSetSidebar).toHaveBeenCalledTimes(1);
const callArg = mockSetSidebar.mock.calls[0][0];
expect(callArg.mergeSenseId).toEqual("word1_senseA");
expect(callArg.senseRef.mergeSenseId).toEqual("word1_senseA");
});
});
9 changes: 5 additions & 4 deletions src/goals/MergeDuplicates/MergeDupsTreeTypes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -95,14 +95,15 @@ export function convertWordToMergeTreeWord(word: Word): MergeTreeWord {

export interface Sidebar {
mergeSenses: MergeTreeSense[];
wordId: string;
mergeSenseId: string;
senseRef: MergeTreeReference;
}

export const defaultSidebar: Sidebar = {
mergeSenses: [],
wordId: "",
mergeSenseId: "",
senseRef: {
wordId: "",
mergeSenseId: "",
},
};

export interface MergeTree {
Expand Down
2 changes: 1 addition & 1 deletion src/goals/MergeDuplicates/Redux/MergeDupsReducer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ const mergeDuplicatesSlice = createSlice({
}

// If the deleted sense was open in the sidebar, reset the sidebar.
const { mergeSenseId, wordId } = state.tree.sidebar;
const { mergeSenseId, wordId } = state.tree.sidebar.senseRef;
if (mergeSenseId === srcRef.mergeSenseId && wordId === srcRef.wordId) {
state.tree.sidebar = defaultSidebar;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -100,8 +100,11 @@ describe("MergeDupsReducer", () => {
...defaultTree,
sidebar: {
...defaultSidebar,
mergeSenseId: "word2_senseA",
wordId: "word2",
senseRef: {
...defaultSidebar.senseRef,
mergeSenseId: "word2_senseA",
wordId: "word2",
},
},
words: testTreeWords(),
},
Expand All @@ -114,7 +117,7 @@ describe("MergeDupsReducer", () => {
sidebarClosed = false
): void {
const { sidebar, words } = mergeDupStepReducer(mockState, action).tree;
expect(!sidebar.wordId).toEqual(sidebarClosed);
expect(!sidebar.senseRef.wordId).toEqual(sidebarClosed);
// Stringify for this test, because order within `.sensesGuids` matters.
expect(JSON.stringify(words)).toEqual(JSON.stringify(expectedWords));
}
Expand Down

0 comments on commit 57125ec

Please sign in to comment.