Skip to content

Commit

Permalink
fix: edit species (Greenstand#128)
Browse files Browse the repository at this point in the history
* fix: edit species

* fix: simplify the edit species to make it clearer
  • Loading branch information
tranquanghuy0801 authored Jul 11, 2021
1 parent 59de3b9 commit 0127c13
Showing 1 changed file with 9 additions and 2 deletions.
11 changes: 9 additions & 2 deletions src/components/SpeciesTable.js
Original file line number Diff line number Diff line change
Expand Up @@ -375,7 +375,6 @@ const EditModal = ({
data,
}) => {
const [error, setError] = React.useState(undefined);
const nameSpecies = data.map((species) => species.name.toLowerCase());

const onNameChange = (e) => {
setError(undefined);
Expand All @@ -387,12 +386,20 @@ const EditModal = ({
};

const handleEditDetailClose = () => {
setError(undefined);
setIsEdit(false);
setSpeciesEdit(undefined);
};

const handleSave = async () => {
if (nameSpecies.includes(speciesEdit.name.toLowerCase().trim())) {
const editName = speciesEdit.name.toLowerCase().trim();
const otherSpeciesList = isEdit
? data.filter((species) => Number(species.id) !== speciesEdit.id)
: data;
const nameSpecies = otherSpeciesList.map((species) =>
species.name.toLowerCase(),
);
if (nameSpecies.includes(editName)) {
setError('Species already exists');
} else {
setError(undefined);
Expand Down

0 comments on commit 0127c13

Please sign in to comment.