Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[DataEntryTable] Prevent RecentEntry re-rendering when typing in NewEntry #2752

Merged
merged 2 commits into from
Nov 2, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 16 additions & 10 deletions src/components/DataEntry/DataEntryTable/RecentEntry.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Grid } from "@mui/material";
import { ReactElement, useState } from "react";
import { ReactElement, memo, useState } from "react";

import { Word, WritingSystem } from "api/models";
import {
Expand All @@ -19,10 +19,10 @@ export interface RecentEntryProps {
rowIndex: number;
entry: Word;
senseGuid: string;
updateGloss: (gloss: string) => void;
updateNote: (newText: string) => Promise<void>;
updateVern: (newVernacular: string, targetWordId?: string) => void;
removeEntry: () => void;
updateGloss: (index: number, gloss: string) => void;
updateNote: (index: number, newText: string) => Promise<void>;
updateVern: (index: number, newVern: string, targetWordId?: string) => void;
removeEntry: (index: number) => void;
addAudioToWord: (wordId: string, audioFile: File) => void;
deleteAudioFromWord: (wordId: string, fileName: string) => void;
focusNewEntry: () => void;
Expand All @@ -34,7 +34,7 @@ export interface RecentEntryProps {
/**
* Displays a recently entered word that a user can still edit
*/
export default function RecentEntry(props: RecentEntryProps): ReactElement {
export function RecentEntry(props: RecentEntryProps): ReactElement {
const sense = props.entry.senses.find((s) => s.guid === props.senseGuid)!;
if (sense.glosses.length < 1) {
sense.glosses.push(newGloss("", props.analysisLang.bcp47));
Expand All @@ -44,20 +44,24 @@ export default function RecentEntry(props: RecentEntryProps): ReactElement {

function conditionallyUpdateGloss(): void {
if (firstGlossText(sense) !== gloss) {
props.updateGloss(gloss);
props.updateGloss(props.rowIndex, gloss);
}
}

function conditionallyUpdateVern(): void {
if (vernacular.trim()) {
if (props.entry.vernacular !== vernacular) {
props.updateVern(vernacular);
props.updateVern(props.rowIndex, vernacular);
}
} else {
setVernacular(props.entry.vernacular);
}
}

const handleRemoveEntry = (): void => props.removeEntry(props.rowIndex);
const handleUpdateNote = (noteText: string): Promise<void> =>
props.updateNote(props.rowIndex, noteText);

return (
<Grid alignItems="center" container id={`${idAffix}-${props.rowIndex}`}>
<Grid
Expand Down Expand Up @@ -114,7 +118,7 @@ export default function RecentEntry(props: RecentEntryProps): ReactElement {
{!props.disabled && (
<EntryNote
noteText={props.entry.note.text}
updateNote={props.updateNote}
updateNote={handleUpdateNote}
buttonId={`${idAffix}-${props.rowIndex}-note`}
/>
)}
Expand Down Expand Up @@ -152,7 +156,7 @@ export default function RecentEntry(props: RecentEntryProps): ReactElement {
>
{!props.disabled && (
<DeleteEntry
removeEntry={() => props.removeEntry()}
removeEntry={handleRemoveEntry}
buttonId={`${idAffix}-${props.rowIndex}-delete`}
confirmId={"addWords.deleteRowWarning"}
wordId={props.entry.id}
Expand All @@ -162,3 +166,5 @@ export default function RecentEntry(props: RecentEntryProps): ReactElement {
</Grid>
);
}

export default memo(RecentEntry);
Loading