Skip to content

Commit

Permalink
[DataEntryTable] Remove "en" assumption (#615)
Browse files Browse the repository at this point in the history
* Use project's analysisLang in new entries instead of 'en'
  • Loading branch information
imnasnainaec authored Aug 20, 2020
1 parent e2d1248 commit 27d81b9
Show file tree
Hide file tree
Showing 12 changed files with 78 additions and 44 deletions.
39 changes: 24 additions & 15 deletions src/components/DataEntry/DataEntryTable/DataEntryTable.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ import {
} from "react-localize-redux";

import * as Backend from "../../../backend";
import { getProjectId } from "../../../backend/localStorage";
import { Project } from "../../../types/project";
import DomainTree from "../../../types/SemanticDomain";
import theme from "../../../types/theme";
import {
Expand Down Expand Up @@ -43,6 +45,7 @@ interface DataEntryTableState {
existingWords: Word[];
recentlyAddedWords: WordAccess[];
isReady: boolean;
analysisLang: string;
}

async function addAudiosToBackend(
Expand Down Expand Up @@ -91,22 +94,17 @@ export function addSemanticDomainToSense(
export function addSenseToWord(
semanticDomain: SemanticDomain,
existingWord: Word,
gloss: string
gloss: string,
language: string
): Word {
let updatedWord: Word = { ...existingWord };

let newGloss: Gloss = {
language: "en",
def: gloss,
};

let newSense: Sense = {
const updatedWord: Word = { ...existingWord };
const newGloss: Gloss = { language, def: gloss };
const newSense: Sense = {
glosses: [newGloss],
semanticDomains: [semanticDomain],
accessibility: State.Active,
};

updatedWord.senses.push(newSense); // Fix which sense we are adding to
updatedWord.senses.push(newSense);
return updatedWord;
}

Expand All @@ -124,6 +122,7 @@ export class DataEntryTable extends React.Component<
existingWords: [],
recentlyAddedWords: [],
isReady: false,
analysisLang: "en",
};
this.refNewEntry = React.createRef<NewEntry>();
this.recorder = new Recorder();
Expand All @@ -133,6 +132,15 @@ export class DataEntryTable extends React.Component<

async componentDidMount() {
await this.updateExisting();
await this.getProjectSettings();
}

async getProjectSettings() {
const proj: Project = await Backend.getProject(getProjectId());
let analysisLang: string = "en";
if (proj.analysisWritingSystems.length > 0)
analysisLang = proj.analysisWritingSystems[0].bcp47;
this.setState({ analysisLang });
}

/** Finished with this page of words, select new semantic domain */
Expand All @@ -157,9 +165,7 @@ export class DataEntryTable extends React.Component<
} else {
recentlyAddedWords.push(newWordAccess);
}
this.setState({
recentlyAddedWords,
});
this.setState({ recentlyAddedWords });
}

/** Update the word in the backend and the frontend */
Expand Down Expand Up @@ -241,7 +247,8 @@ export class DataEntryTable extends React.Component<
const updatedWord = addSenseToWord(
this.props.semanticDomain,
existingWord,
gloss
gloss,
this.state.analysisLang
);
await this.updateWordForNewEntry(
updatedWord,
Expand Down Expand Up @@ -508,6 +515,7 @@ export class DataEntryTable extends React.Component<
if (this.refNewEntry.current)
this.refNewEntry.current.focusVernInput();
}}
analysisLang={this.state.analysisLang}
/>
</Grid>
))}
Expand All @@ -530,6 +538,7 @@ export class DataEntryTable extends React.Component<
this.setState({ isReady: isReady })
}
recorder={this.recorder}
analysisLang={this.state.analysisLang}
/>
</Grid>
</Grid>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ interface NewEntryProps {
semanticDomain: SemanticDomain;
setIsReadyState: (isReady: boolean) => void;
recorder?: Recorder;
analysisLang: string;
}

interface NewEntryState {
Expand Down Expand Up @@ -82,7 +83,7 @@ export default class NewEntry extends React.Component<
...this.state.newEntry,
senses: [
{
glosses: [{ language: "en", def: newValue }],
glosses: [{ language: this.props.analysisLang, def: newValue }],
semanticDomains: [this.props.semanticDomain],
},
],
Expand Down Expand Up @@ -215,6 +216,7 @@ export default class NewEntry extends React.Component<
setActiveGloss={(newGloss: string) =>
this.setState({ activeGloss: newGloss })
}
analysisLang={this.props.analysisLang}
/>
</Grid>
<Grid item xs={12}>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,11 @@ describe("Tests NewEntry", () => {
<NewEntry
allVerns={[]}
allWords={[]}
updateWord={() => null}
addNewWord={() => null}
updateWordWithNewGloss={jest.fn()}
addNewWord={jest.fn()}
semanticDomain={{ name: "", id: "" }}
setIsReadyState={() => null}
setIsReadyState={jest.fn()}
analysisLang={""}
/>
</Provider>
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ interface RecentEntryProps {
semanticDomain: SemanticDomain;
recorder: Recorder;
focusNewEntry: () => void;
analysisLang: string;
}

interface RecentEntryState {
Expand All @@ -33,7 +34,7 @@ interface RecentEntryState {
}

/**
* Displays a word a user can still make edits to
* Displays a recently entered word that a user can still edit
*/
export default class RecentEntry extends React.Component<
RecentEntryProps,
Expand All @@ -43,7 +44,8 @@ export default class RecentEntry extends React.Component<
super(props);

let sense: Sense = { ...props.entry.senses[props.senseIndex] };
if (sense.glosses.length < 1) sense.glosses.push({ def: "", language: "" });
if (sense.glosses.length < 1)
sense.glosses.push({ def: "", language: this.props.analysisLang });

this.state = {
vernacular: props.entry.vernacular,
Expand Down Expand Up @@ -132,6 +134,7 @@ export default class RecentEntry extends React.Component<
if (this.state.vernacular) this.focusOnNewEntry();
}}
setActiveGloss={() => {}}
analysisLang={this.props.analysisLang}
/>
</Grid>
<Grid
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,14 +32,15 @@ function renderWithWord(word: Word) {
allWords={[]}
entry={word}
senseIndex={0}
updateGloss={() => null}
updateVern={() => null}
removeEntry={() => null}
addAudioToWord={() => null}
deleteAudioFromWord={() => null}
updateGloss={jest.fn()}
updateVern={jest.fn()}
removeEntry={jest.fn()}
addAudioToWord={jest.fn()}
deleteAudioFromWord={jest.fn()}
semanticDomain={{ name: "", id: "" }}
recorder={new Recorder()}
focusNewEntry={() => null}
focusNewEntry={jest.fn()}
analysisLang={"en"}
/>
</Provider>
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ function SenseDialog(
selectedWord: Word;
open: boolean;
handleClose: (senseIndex: number) => void;
analysisLang: string;
} & LocalizeContextProps
) {
return (
Expand All @@ -31,6 +32,7 @@ function SenseDialog(
<SenseList
selectedWord={props.selectedWord}
closeDialog={props.handleClose}
analysisLang={props.analysisLang}
/>
</DialogContent>
</Dialog>
Expand All @@ -40,6 +42,7 @@ function SenseDialog(
interface SenseListProps {
selectedWord: Word;
closeDialog: (senseIndex: number) => void;
analysisLang: string;
}

// Copied from customized menus at https://material-ui.com/components/menus/
Expand Down Expand Up @@ -76,8 +79,11 @@ export function SenseList(props: SenseListProps) {
<div style={{ margin: theme.spacing(4) }}>
<DomainCell
rowData={parseWord(
{ ...props.selectedWord, senses: [sense] } as Word,
"en"
{
...props.selectedWord,
senses: [sense],
} as Word,
props.analysisLang
)}
sortingByDomains={false}
/>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ export function VernDialog(
vernacularWords: Word[];
open: boolean;
handleClose: (selectedWordId?: string) => void;
analysisLang: string;
} & LocalizeContextProps
) {
return (
Expand All @@ -37,6 +38,7 @@ export function VernDialog(
<VernList
vernacularWords={props.vernacularWords}
closeDialog={props.handleClose}
analysisLang={props.analysisLang}
/>
</DialogContent>
</Dialog>
Expand All @@ -46,6 +48,7 @@ export function VernDialog(
interface VernListProps {
vernacularWords: Word[];
closeDialog: (selectedWordId: string) => void;
analysisLang: string;
}

// Copied from customized menus at https://material-ui.com/components/menus/
Expand Down Expand Up @@ -91,13 +94,13 @@ export function VernList(props: VernListProps) {
<SenseCell
editable={false}
sortingByGloss={false}
value={parseWord(filteredWord, "en").senses}
rowData={parseWord(filteredWord, "en")}
value={parseWord(word, props.analysisLang).senses}
rowData={parseWord(word, props.analysisLang)}
/>
</div>
<div style={{ margin: theme.spacing(4) }}>
<DomainCell
rowData={parseWord(filteredWord, "en")}
rowData={parseWord(word, props.analysisLang)}
sortingByDomains={false}
/>
</div>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@ describe("Tests VernList ", () => {
renderer.create(
<VernList
vernacularWords={[simpleWord("", "")]}
closeDialog={() => null}
closeDialog={jest.fn()}
analysisLang={"en"}
/>
);
});
Expand Down Expand Up @@ -54,6 +55,7 @@ function createVernListInstance(
<VernList
vernacularWords={_vernacularWords}
closeDialog={_mockCallback}
analysisLang={"en"}
/>
</Provider>
).root;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ interface VernWithSuggestionsProps {
updateWordId: (wordId?: string) => void;
selectedWordId?: string;
onBlur?: () => void;
analysisLang: string;
}

interface VernWithSuggestionsState {
Expand Down Expand Up @@ -187,6 +188,7 @@ export class VernWithSuggestions extends React.Component<
}
}}
vernacularWords={this.state.dupVernWords}
analysisLang={this.props.analysisLang}
/>
)}
{this.props.isNew && (
Expand All @@ -201,6 +203,7 @@ export class VernWithSuggestions extends React.Component<
}
this.setState({ senseOpen: false });
}}
analysisLang={this.props.analysisLang}
/>
)}
</React.Fragment>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,12 @@ describe("Tests VernWithSuggestions", () => {
<LocalizedVernWithSuggestions
vernacular={""}
vernInput={React.createRef<HTMLDivElement>()}
updateVernField={() => []}
updateWordId={() => null}
updateVernField={jest.fn()}
setActiveGloss={jest.fn()}
updateWordId={jest.fn()}
allVerns={[]}
handleEnter={() => null}
handleEnterAndTab={jest.fn()}
analysisLang={"en"}
/>
);
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,13 +58,11 @@ beforeEach(() => {
<DataEntryTable
domain={baseDomain}
semanticDomain={mockSemanticDomain}
displaySemanticDomainView={(_isGettingSemanticDomain: boolean) => {}}
displaySemanticDomainView={jest.fn()}
isSmallScreen={false}
hideQuestions={hideQuestionsMock}
getWordsFromBackend={() => {
return new Promise(() => []);
}}
showExistingData={() => {}}
getWordsFromBackend={jest.fn()}
showExistingData={jest.fn()}
/>
</Provider>
);
Expand Down Expand Up @@ -131,7 +129,9 @@ describe("Tests DataEntryTable", () => {
...word,
senses: [...word.senses, newSense],
};
expect(addSenseToWord(semanticDomain, word, gloss)).toEqual(expectedWord);
expect(addSenseToWord(semanticDomain, word, gloss, "en")).toEqual(
expectedWord
);
});

it("adds a sense to a word that already has a sense", () => {
Expand All @@ -154,7 +154,9 @@ describe("Tests DataEntryTable", () => {
...word,
senses: [...word.senses, expectedSense],
};
expect(addSenseToWord(semanticDomain, word, gloss)).toEqual(expectedWord);
expect(addSenseToWord(semanticDomain, word, gloss, "en")).toEqual(
expectedWord
);
});

it("adds a semantic domain to an existing sense", () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ export interface ReviewEntriesSense {

export function parseWord(
word: Word,
analysisLang: string,
analysisLang: string, // bcp47 code
commonRecorder?: Recorder
) {
let currentWord: ReviewEntriesWord = {
Expand Down

0 comments on commit 27d81b9

Please sign in to comment.