Skip to content

Commit

Permalink
Clean up
Browse files Browse the repository at this point in the history
  • Loading branch information
imnasnainaec committed Oct 25, 2023
1 parent 564ae51 commit b720d2e
Show file tree
Hide file tree
Showing 10 changed files with 133 additions and 128 deletions.
3 changes: 3 additions & 0 deletions public/locales/en/translation.json
Original file line number Diff line number Diff line change
Expand Up @@ -321,6 +321,9 @@
"search": "Search"
}
},
"completed": {
"number": "Number of entries edited: "
},
"undo": {
"undo": "Undo Edit",
"undoDialog": "Undo this edit?",
Expand Down
7 changes: 6 additions & 1 deletion src/components/GoalTimeline/GoalList.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ import { CharInvChangesGoalList } from "goals/CharacterInventory/CharInvComplete
import { CharInvChanges } from "goals/CharacterInventory/CharacterInventoryTypes";
import { MergesCount } from "goals/MergeDuplicates/MergeDupsCompleted";
import { MergesCompleted } from "goals/MergeDuplicates/MergeDupsTypes";
import { EditsCount } from "goals/ReviewEntries/ReviewEntriesCompleted";
import { EntriesEdited } from "goals/ReviewEntries/ReviewEntriesTypes";
import { Goal, GoalStatus, GoalType } from "types/goals";

type Orientation = "horizontal" | "vertical";
Expand Down Expand Up @@ -112,7 +114,8 @@ export function makeGoalTile(
(goal.status === GoalStatus.Completed &&
goal.goalType !== GoalType.CreateCharInv &&
goal.goalType !== GoalType.MergeDups &&
goal.goalType !== GoalType.ReviewDeferredDups)
goal.goalType !== GoalType.ReviewDeferredDups &&
goal.goalType !== GoalType.ReviewEntries)
}
data-testid="goal-button"
>
Expand Down Expand Up @@ -152,6 +155,8 @@ function getCompletedGoalInfo(goal: Goal): ReactElement {
case GoalType.MergeDups:
case GoalType.ReviewDeferredDups:
return MergesCount(goal.changes as MergesCompleted);
case GoalType.ReviewEntries:
return EditsCount(goal.changes as EntriesEdited);
default:
return <Fragment />;
}
Expand Down
12 changes: 8 additions & 4 deletions src/goals/ReviewEntries/Redux/ReviewEntriesActions.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
import { Sense } from "api/models";
import * as backend from "backend";
import { addEntryEditToGoal } from "components/GoalTimeline/Redux/GoalActions";
import {
addEntryEditToGoal,
asyncUpdateGoal,
} from "components/GoalTimeline/Redux/GoalActions";
import { uploadFileFromUrl } from "components/Pronunciations/utilities";
import {
ReviewClearReviewEntriesState,
Expand All @@ -23,8 +26,9 @@ export function updateAllWords(words: ReviewEntriesWord[]): ReviewUpdateWords {
}

function updateWord(oldId: string, updatedWord: ReviewEntriesWord) {
return (dispatch: StoreStateDispatch) => {
return async (dispatch: StoreStateDispatch) => {
dispatch(addEntryEditToGoal({ newId: updatedWord.id, oldId }));
await dispatch(asyncUpdateGoal());
const update: ReviewUpdateWord = {
type: ReviewEntriesActionTypes.UpdateWord,
oldId,
Expand Down Expand Up @@ -163,7 +167,7 @@ export function updateFrontierWord(
editSource.audio = (await backend.getWord(editSource.id)).audio;

// Update the review entries word in the state.
dispatch(updateWord(editWord.id, editSource));
await dispatch(updateWord(editWord.id, editSource));
};
}

Expand Down Expand Up @@ -192,7 +196,7 @@ function refreshWord(
return async (dispatch: StoreStateDispatch): Promise<void> => {
const newWordId = await wordUpdater(oldWordId);
const word = await backend.getWord(newWordId);
dispatch(updateWord(oldWordId, new ReviewEntriesWord(word)));
await dispatch(updateWord(oldWordId, new ReviewEntriesWord(word)));
};
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,13 @@ jest.mock("backend", () => ({
getWord: (wordId: string) => mockGetWord(wordId),
updateWord: (word: Word) => mockUpdateWord(word),
}));

jest.mock("backend/localStorage", () => ({
getProjectId: jest.fn(),
}));
jest.mock("components/GoalTimeline/Redux/GoalActions", () => ({
addEntryEditToGoal: () => jest.fn(),
asyncUpdateGoal: () => jest.fn(),
}));

const mockStore = configureMockStore([thunk])();

Expand Down
51 changes: 51 additions & 0 deletions src/goals/ReviewEntries/Redux/tests/ReviewEntriesReducer.test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
import { reviewEntriesReducer } from "goals/ReviewEntries/Redux/ReviewEntriesReducer";
import {
defaultState,
ReviewEntriesActionTypes,
} from "goals/ReviewEntries/Redux/ReviewEntriesReduxTypes";
import {
ReviewEntriesSense,
ReviewEntriesWord,
} from "goals/ReviewEntries/ReviewEntriesTypes";

describe("ReviewEntriesReducer", () => {
it("Returns default state when passed undefined state", () => {
expect(reviewEntriesReducer(undefined, { type: undefined } as any)).toEqual(
defaultState
);
});

it("Adds a set of words to a list when passed an UpdateAllWords action", () => {
const revWords = [new ReviewEntriesWord(), new ReviewEntriesWord()];
const state = reviewEntriesReducer(defaultState, {
type: ReviewEntriesActionTypes.UpdateAllWords,
words: revWords,
});
expect(state).toEqual({ ...defaultState, words: revWords });
});

it("Updates a specified word when passed an UpdateWord action", () => {
const oldId = "id-of-word-to-be-updated";
const oldWords: ReviewEntriesWord[] = [
{ ...new ReviewEntriesWord(), id: "other-id" },
{ ...new ReviewEntriesWord(), id: oldId, vernacular: "old-vern" },
];
const oldState = { ...defaultState, words: oldWords };

const newId = "id-after-update";
const newRevWord: ReviewEntriesWord = {
...new ReviewEntriesWord(),
id: newId,
vernacular: "new-vern",
senses: [{ ...new ReviewEntriesSense(), guid: "new-sense-id" }],
};
const newWords = [oldWords[0], newRevWord];

const newState = reviewEntriesReducer(oldState, {
type: ReviewEntriesActionTypes.UpdateWord,
oldId,
updatedWord: newRevWord,
});
expect(newState).toEqual({ ...oldState, words: newWords });
});
});
24 changes: 16 additions & 8 deletions src/goals/ReviewEntries/ReviewEntriesCompleted.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -24,15 +24,23 @@ export default function ReviewEntriesCompleted(): ReactElement {
<Typography component="h1" variant="h4">
{t("reviewEntries.title")}
</Typography>
<Typography>
{t("reviewEntries.completed.number")}
{changes.entryEdits?.length ?? 0}
</Typography>
{EditsCount(changes)}
{changes.entryEdits?.map((e) => <EditedEntry edit={e} key={e.newId} />)}
</>
);
}

export function EditsCount(changes: EntriesEdited): ReactElement {
const { t } = useTranslation();

return (
<Typography>
{t("reviewEntries.completed.number")}
{changes.entryEdits?.length ?? 0}
</Typography>
);
}

function EditedEntry(props: { edit: EntryEdit }): ReactElement {
return (
<Grid container style={{ flexWrap: "nowrap", overflow: "auto" }}>
Expand Down Expand Up @@ -82,7 +90,7 @@ function UndoButton(props: UndoButtonProps): ReactElement {

return isUndoEnabled ? (
<Grid container direction="column" justifyContent="center">
<>
<div>
<Button
variant="outlined"
id={`edit-undo-${props.edit.newId}`}
Expand All @@ -100,13 +108,13 @@ function UndoButton(props: UndoButtonProps): ReactElement {
buttonIdCancel="edit-undo-cancel"
buttonIdConfirm="edit-undo-confirm"
/>
</>
</div>
</Grid>
) : (
<Grid container direction="column" justifyContent="center">
<>
<div>
<Button disabled>{t(props.disabledId)}</Button>
</>
</div>
</Grid>
);
}
Expand Down
34 changes: 33 additions & 1 deletion src/goals/ReviewEntries/ReviewEntriesTypes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import {
Word,
} from "api/models";
import { Goal, GoalName, GoalType } from "types/goals";
import { newSense, newWord } from "types/word";
import { newNote, newSense, newWord } from "types/word";
import { cleanDefinitions, cleanGlosses } from "utilities/wordUtilities";

export class ReviewEntries extends Goal {
Expand Down Expand Up @@ -101,3 +101,35 @@ export class ReviewEntriesSense {
return sense.glosses.map((g) => g.def).join(ReviewEntriesSense.SEPARATOR);
}
}

/** Reverse map of the ReviewEntriesSense constructor.
* Important: Not everything is preserved! */
function senseFromReviewEntriesSense(revSense: ReviewEntriesSense): Sense {
return {
...newSense(),
accessibility: revSense.protected
? Status.Protected
: revSense.deleted
? Status.Deleted
: Status.Active,
definitions: revSense.definitions.map((d) => ({ ...d })),
glosses: revSense.glosses.map((g) => ({ ...g })),
grammaticalInfo: revSense.partOfSpeech,
guid: revSense.guid,
semanticDomains: revSense.domains.map((dom) => ({ ...dom })),
};
}

/** Reverse map of the ReviewEntriesWord constructor.
* Important: Not everything is preserved! */
export function wordFromReviewEntriesWord(revWord: ReviewEntriesWord): Word {
return {
...newWord(revWord.vernacular),
accessibility: revWord.protected ? Status.Protected : Status.Active,
audio: [...revWord.audio],
id: revWord.id,
flag: { ...revWord.flag },
note: newNote(revWord.noteText),
senses: revWord.senses.map(senseFromReviewEntriesSense),
};
}
76 changes: 0 additions & 76 deletions src/goals/ReviewEntries/tests/ReviewEntriesReducer.test.tsx

This file was deleted.

32 changes: 2 additions & 30 deletions src/goals/ReviewEntries/tests/WordsMock.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,10 @@
import { GramCatGroup, Sense, Word } from "api/models";
import { GramCatGroup } from "api/models";
import {
ReviewEntriesSense,
ReviewEntriesWord,
} from "goals/ReviewEntries/ReviewEntriesTypes";
import { newSemanticDomain } from "types/semanticDomain";
import {
newDefinition,
newFlag,
newGloss,
newNote,
newSense,
newWord,
} from "types/word";
import { newDefinition, newFlag, newGloss } from "types/word";
import { Bcp47Code } from "types/writingSystem";

export default function mockWords(): ReviewEntriesWord[] {
Expand Down Expand Up @@ -57,24 +50,3 @@ export default function mockWords(): ReviewEntriesWord[] {
},
];
}

export function mockCreateWord(word: ReviewEntriesWord): Word {
return {
...newWord(word.vernacular),
id: word.id,
senses: word.senses.map((sense) => createMockSense(sense)),
note: newNote(word.noteText),
flag: word.flag,
};
}

function createMockSense(sense: ReviewEntriesSense): Sense {
return {
...newSense(),
guid: sense.guid,
definitions: [...sense.definitions],
glosses: [...sense.glosses],
grammaticalInfo: sense.partOfSpeech,
semanticDomains: [...sense.domains],
};
}
Loading

0 comments on commit b720d2e

Please sign in to comment.