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] Remove "en" assumption #615

Merged
merged 10 commits into from
Aug 20, 2020
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 @@ -247,7 +253,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 @@ -514,6 +521,7 @@ export class DataEntryTable extends React.Component<
if (this.refNewEntry.current)
this.refNewEntry.current.focusVernInput();
}}
analysisLang={this.state.analysisLang}
/>
</Grid>
))}
Expand All @@ -536,6 +544,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
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={() => new Promise(() => {})}
addNewWord={() => new Promise(() => {})}
semanticDomain={{ name: "", id: "" }}
setIsReadyState={() => null}
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
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ function renderWithWord(word: Word) {
semanticDomain={{ name: "", id: "" }}
recorder={new Recorder()}
focusNewEntry={() => null}
analysisLang={""}
/>
</Provider>
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,10 +65,10 @@ export function SenseList(props: SenseListProps) {
</div>
<div style={{ margin: theme.spacing(4) }}>
<DomainCell
rowData={parseWord(
{ ...props.selectedWord, senses: [sense] } as Word,
"en"
)}
rowData={parseWord({
...props.selectedWord,
senses: [sense],
} as Word)}
sortingByDomains={false}
/>
</div>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,15 +71,12 @@ export function VernList(props: VernListProps) {
<SenseCell
editable={false}
sortingByGloss={false}
value={parseWord(word, "en").senses}
rowData={parseWord(word, "en")}
value={parseWord(word).senses}
rowData={parseWord(word)}
/>
</div>
<div style={{ margin: theme.spacing(4) }}>
<DomainCell
rowData={parseWord(word, "en")}
sortingByDomains={false}
/>
<DomainCell rowData={parseWord(word)} sortingByDomains={false} />
</div>
</StyledMenuItem>
))}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,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 +156,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,
commonRecorder?: Recorder
) {
let currentWord: ReviewEntriesWord = {
Expand All @@ -39,7 +39,7 @@ export function parseWord(
return currentWord;
}
// Convert a Sense into a ReviewEntriesSense
function parseSense(sense: Sense, analysisLang: string) {
function parseSense(sense: Sense, analysisLang?: string) {
let hasGloss: boolean;
let currentSense: ReviewEntriesSense = {
glosses: "",
Expand All @@ -61,7 +61,7 @@ function parseSense(sense: Sense, analysisLang: string) {
hasGloss = false;
if (sense.glosses)
for (let gloss of sense.glosses)
if (gloss.language === analysisLang) {
if (analysisLang === undefined || gloss.language === analysisLang) {
hasGloss = true;
currentSense.glosses += gloss.def + SEPARATOR;
}
Expand Down