Skip to content

Commit

Permalink
#2328 follow-up (#2545)
Browse files Browse the repository at this point in the history
  • Loading branch information
imnasnainaec authored Sep 5, 2023
1 parent 55f4416 commit 6d85b1f
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 43 deletions.
75 changes: 41 additions & 34 deletions src/components/ProjectSettings/ProjectLanguages.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,9 @@ import { getAnalysisLangsFromWords } from "utilities/wordUtilities";
const addAnalysisLangButtonId = "analysis-language-new";
const addAnalysisLangCleanButtonId = "analysis-language-new-clear";
const addAnalysisLangConfirmButtonId = "analysis-language-new-confirm";
export const editVernacularNameButtonId = "vernacular-language-edit";
export const editVernacularNameFieldId = "vernacular-language-name";
export const editVernacularNameSaveButtonId = "vernacular-language-save";
const getProjAnalysisLangsButtonId = "analysis-language-get";
const semDomLangSelectId = "semantic-domains-language";

Expand All @@ -54,12 +57,11 @@ export default function ProjectLanguages(
.map((ws) => ws.bcp47)
.includes(newLang.bcp47)
);
}, [newLang.bcp47, props.project.analysisWritingSystems]);

useEffect(() => {
setNewVernName(props.project.vernacularWritingSystem.name);
}, [
newLang.bcp47,
props.project.analysisWritingSystems,
props.project.vernacularWritingSystem.name,
]);
}, [props.project.vernacularWritingSystem.name]);

const setNewAnalysisDefault = async (index: number): Promise<void> => {
const analysisWritingSystems = [...props.project.analysisWritingSystems];
Expand Down Expand Up @@ -156,22 +158,26 @@ export default function ProjectLanguages(
};

const updateVernacularName = async (): Promise<void> => {
if (newVernName != props.project.vernacularWritingSystem.name) {
var vernacularWritingSystem = props.project.vernacularWritingSystem;
vernacularWritingSystem.name = newVernName;
await props
.updateProject({
...props.project,
vernacularWritingSystem,
})
.then(() => resetState())
.catch((err) => {
console.error(err);
toast.error(
t("projectSettings.language.updateVernacularLanguageNameFailed")
);
});
if (
!newVernName ||
newVernName === props.project.vernacularWritingSystem.name
) {
return;
}

const vernacularWritingSystem: WritingSystem = {
...props.project.vernacularWritingSystem,
name: newVernName,
};
await props
.updateProject({ ...props.project, vernacularWritingSystem })
.then(() => resetState())
.catch((err) => {
console.error(err);
toast.error(
t("projectSettings.language.updateVernacularLanguageNameFailed")
);
});
};

const addAnalysisLangButtons = (): ReactElement => (
Expand Down Expand Up @@ -271,15 +277,17 @@ export default function ProjectLanguages(
<ImmutableWritingSystem
ws={props.project.vernacularWritingSystem}
buttons={
<IconButtonWithTooltip
icon={<BorderColor fontSize="inherit" />}
textId={t("projectSettings.language.changeName")}
small
onClick={() => {
setChangeVernName(true);
}}
buttonId={`vernacular-language-edit`}
/>
props.readOnly ? (
<Fragment />
) : (
<IconButtonWithTooltip
icon={<BorderColor fontSize="inherit" />}
textId={"projectSettings.language.changeName"}
small
onClick={() => setChangeVernName(true)}
buttonId={editVernacularNameButtonId}
/>
)
}
/>
);
Expand All @@ -289,7 +297,7 @@ export default function ProjectLanguages(
<Grid item xs={12}>
<TextField
variant="standard"
id="vernacular-name"
id={editVernacularNameFieldId}
value={newVernName}
onChange={(e) => setNewVernName(e.target.value)}
onBlur={() => {
Expand All @@ -303,10 +311,9 @@ export default function ProjectLanguages(
<Button
variant="contained"
color="primary"
id="vernacular-name-save"
onMouseDown={() => {
updateVernacularName();
}}
id={editVernacularNameSaveButtonId}
onClick={() => updateVernacularName()}
onMouseDown={(e) => e.preventDefault()}
>
{t("buttons.save")}
</Button>
Expand Down
9 changes: 2 additions & 7 deletions src/components/ProjectSettings/ProjectName.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -38,16 +38,11 @@ export default function ProjectName(
</Grid>
<Grid item>
<Button
// No onClick necessary, as name updates on blur away from TextField.
variant="contained"
color="primary"
id="project-name-save"
onMouseDown={(e) => {
e.preventDefault();
}}
onClick={() => {
updateProjectName();
}}
onClick={() => updateProjectName()}
onMouseDown={(e) => e.preventDefault()}
>
{t("buttons.save")}
</Button>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
import { Button, Select } from "@mui/material";
import { Button, IconButton, Select } from "@mui/material";
import { LanguagePicker } from "mui-language-picker";
import renderer from "react-test-renderer";

import "tests/reactI18nextMock";

import { Project, WritingSystem } from "api/models";
import ProjectLanguages from "components/ProjectSettings/ProjectLanguages";
import ProjectLanguages, {
editVernacularNameButtonId,
editVernacularNameFieldId,
editVernacularNameSaveButtonId,
} from "components/ProjectSettings/ProjectLanguages";
import { newProject } from "types/project";
import { newWritingSystem } from "types/writingSystem";

Expand Down Expand Up @@ -54,9 +58,33 @@ describe("ProjectLanguages", () => {
it("renders readOnly", async () => {
await renderProjLangs(mockProject([...mockAnalysisWritingSystems]), true);
expect(projectMaster.root.findAllByType(Button)).toHaveLength(0);
expect(projectMaster.root.findAllByType(IconButton)).toHaveLength(0);
expect(projectMaster.root.findAllByType(Select)).toHaveLength(0);
});

it("can change vernacular language name", async () => {
await renderProjLangs(mockProject([...mockAnalysisWritingSystems]));
const newName = "Vern Lang";
await renderer.act(async () => {
projectMaster.root
.findByProps({ id: editVernacularNameButtonId })
.props.onClick();
});
await renderer.act(async () => {
projectMaster.root
.findByProps({ id: editVernacularNameFieldId })
.props.onChange({ target: { value: newName } });
});
await renderer.act(async () => {
projectMaster.root
.findByProps({ id: editVernacularNameSaveButtonId })
.props.onClick();
});
expect(
mockUpdateProject.mock.calls[0][0].vernacularWritingSystem.name
).toEqual(newName);
});

it("can add language to project", async () => {
await renderAndClickAdd();
pickerHandle = projectMaster.root.findByType(LanguagePicker);
Expand Down

0 comments on commit 6d85b1f

Please sign in to comment.