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

BugFix: Overwriting animal sex when editing an animal #1331

Merged
merged 2 commits into from
Jul 30, 2024
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
34 changes: 24 additions & 10 deletions app/src/components/species/components/SpeciesAutocompleteField.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ const SpeciesAutocompleteField = (props: ISpeciesAutocompleteFieldProps) => {
const isMounted = useIsMounted();

// A default species has been provided and it is not a promise
const isDefaultSpecies = defaultSpecies && !('then' in defaultSpecies);
const isDefaultSpecies = defaultSpecies && !(defaultSpecies instanceof Promise);

const [hasLoadedDefaultSpecies, setHasLoadedDefaultSpecies] = useState(false);
// The input field value
Expand All @@ -137,7 +137,7 @@ const SpeciesAutocompleteField = (props: ISpeciesAutocompleteFieldProps) => {
const [isLoading, setIsLoading] = useState(false);

useEffect(() => {
if (defaultSpecies && 'then' in defaultSpecies) {
if (defaultSpecies instanceof Promise) {
// A default species has been provided and it is a promise
defaultSpecies.then((taxonomy) => {
if (hasLoadedDefaultSpecies) {
Expand Down Expand Up @@ -186,34 +186,48 @@ const SpeciesAutocompleteField = (props: ISpeciesAutocompleteFieldProps) => {
id={formikFieldName}
disabled={disabled}
data-testid={formikFieldName}
filterSelectedOptions
noOptionsText="No matching options"
options={options}
getOptionLabel={(option) => option.scientificName}
isOptionEqualToValue={(option, value) => {
return option.tsn === value.tsn;
}}
filterOptions={(item) => item}
inputValue={inputValue}
// Text field value changed
onInputChange={(_, value, reason) => {
if (reason === 'reset') {
if (clearOnSelect) {
setInputValue('');
setOptions([]);
handleClear?.();
if (!clearOnSelect) {
return;
}

if (inputValue === '' && options.length === 0) {
// Nothing to clear
return;
}

setInputValue('');
setOptions([]);
handleClear?.();

return;
}

if (reason === 'clear') {
if (inputValue === '' && options.length === 0) {
// Nothing to clear
return;
}

setInputValue('');
setOptions([]);
handleClear?.();
return;
}

if (!value) {
if (inputValue === '' && options.length === 0) {
// Nothing to clear
return;
}

setInputValue('');
setOptions([]);
return;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ export interface IAnimalFormProps {
}

const AnimalFormYupSchema = yup.object({
nickname: yup.string().trim().min(3, 'Nickname must be at least 3 letters').required('Nickname is required'),
nickname: yup.string().trim().min(1, 'Nickname is required').required('Nickname is required'),
species: yup
.object()
.shape({
Expand All @@ -53,6 +53,7 @@ const AnimalFormYupSchema = yup.object({
.nullable()
.required('Species is required'),
critter_comment: yup.string().nullable(),
sex: yup.string().nullable(),
ecological_units: yup.array(
yup
.object()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import { Link as RouterLink } from 'react-router-dom';
export const defaultAnimalDataFormValues: ICreateEditAnimalRequest = {
nickname: '',
species: null,
sex: AnimalSex.UNKNOWN,
NickPhura marked this conversation as resolved.
Show resolved Hide resolved
ecological_units: [],
wildlife_health_id: '',
critter_comment: ''
Expand Down Expand Up @@ -91,7 +92,7 @@ export const CreateAnimalPage = () => {
itis_tsn: values.species.tsn,
wlh_id: undefined,
animal_id: values.nickname,
sex: AnimalSex.UNKNOWN,
sex: values.sex,
critter_comment: values.critter_comment
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ export const EditAnimalPage = () => {
itis_tsn: values.species.tsn,
wlh_id: values.wildlife_health_id,
animal_id: values.nickname,
sex: AnimalSex.UNKNOWN,
sex: values.sex,
critter_comment: values.critter_comment
});

Expand Down Expand Up @@ -218,6 +218,7 @@ export const EditAnimalPage = () => {
initialAnimalData={{
critter_id: critter.critter_id,
nickname: critter.animal_id || '',
sex: critter.sex as AnimalSex,
species: {
commonNames: [],
rank: undefined,
Expand Down
5 changes: 3 additions & 2 deletions app/src/interfaces/useCritterApi.interface.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import { ICreateCritterCollectionUnit } from 'features/surveys/view/survey-animals/animal';
import { AnimalSex, ICreateCritterCollectionUnit } from 'features/surveys/view/survey-animals/animal';
import { Feature } from 'geojson';
import { IPartialTaxonomy } from './useTaxonomyApi.interface';

export interface ICritterCreate {
critter_id?: string;
wlh_id?: string | null;
animal_id?: string | null;
sex: string;
sex: AnimalSex;
itis_tsn: number;
responsible_region_nr_id?: string | null;
critter_comment?: string | null;
Expand All @@ -16,6 +16,7 @@ export interface ICreateEditAnimalRequest {
critter_id?: string;
nickname: string;
species: IPartialTaxonomy | null;
sex: AnimalSex;
ecological_units: ICreateCritterCollectionUnit[];
wildlife_health_id: string | null;
critter_comment: string | null;
Expand Down