-
Notifications
You must be signed in to change notification settings - Fork 84
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #3201 from LiteFarmOrg/LF-4156-create-animal-batch…
…-creation-form-card Lf 4156 create animal batch creation form card
- Loading branch information
Showing
28 changed files
with
608 additions
and
49 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
153 changes: 153 additions & 0 deletions
153
packages/webapp/src/components/Animals/AddAnimalsFormCard/AddAnimalsFormCard.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,153 @@ | ||
/* | ||
* Copyright 2024 LiteFarm.org | ||
* This file is part of LiteFarm. | ||
* | ||
* LiteFarm is free software: you can redistribute it and/or modify | ||
* it under the terms of the GNU General Public License as published by | ||
* the Free Software Foundation, either version 3 of the License, or | ||
* (at your option) any later version. | ||
* | ||
* LiteFarm is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU General Public License for more details, see <https://www.gnu.org/licenses/>. | ||
*/ | ||
|
||
import { Controller, useFormContext } from 'react-hook-form'; | ||
|
||
import NumberInput from '../../Form/NumberInput'; | ||
import Checkbox from '../../Form/Checkbox'; | ||
import SexDetails from '../../Form/SexDetails'; | ||
import styles from './styles.module.scss'; | ||
import Input from '../../Form/Input'; | ||
import Card from '../../CardV2'; | ||
import { Main } from '../../Typography'; | ||
import TextButton from '../../Form/Button/TextButton'; | ||
import { ReactComponent as XIcon } from '../../../assets/images/x-icon.svg'; | ||
import { type Details as SexDetailsType } from '../../Form/SexDetails/SexDetailsPopover'; | ||
import { | ||
AnimalBreedSelect, | ||
AnimalTypeSelect, | ||
type AnimalBreedSelectProps, | ||
type AnimalTypeSelectProps, | ||
type Option, | ||
} from './AnimalSelect'; | ||
import { useTranslation } from 'react-i18next'; | ||
|
||
const FIELD_NAMES = { | ||
TYPE: 'type', | ||
BREED: 'breed', | ||
SEX_DETAILS: 'sexDetails', | ||
COUNT: 'count', | ||
CREATE_INDIVIDUAL_PROFILES: 'createIndividualProfiles', | ||
GROUP: 'group', | ||
BATCH: 'batch', | ||
} as const; | ||
|
||
// Should be moved up to parent component that calls useForm | ||
type FormFields = { | ||
[FIELD_NAMES.TYPE]?: Option; | ||
[FIELD_NAMES.BREED]?: Option; | ||
[FIELD_NAMES.SEX_DETAILS]: SexDetailsType; | ||
[FIELD_NAMES.COUNT]?: number; | ||
[FIELD_NAMES.CREATE_INDIVIDUAL_PROFILES]?: boolean; | ||
[FIELD_NAMES.GROUP]?: string; | ||
[FIELD_NAMES.BATCH]?: string; | ||
}; | ||
|
||
type AddAnimalsFormCardProps = AnimalTypeSelectProps & | ||
AnimalBreedSelectProps & { | ||
sexDetailsOptions: SexDetailsType; | ||
onIndividualProfilesCheck?: (isChecked: boolean) => void; | ||
onRemoveButtonClick?: (e: React.MouseEvent<HTMLButtonElement>) => void; | ||
showRemoveButton?: boolean; | ||
isActive?: boolean; | ||
}; | ||
|
||
export default function AddAnimalsFormCard({ | ||
typeOptions, | ||
breedOptions, | ||
sexDetailsOptions, | ||
onTypeChange, | ||
onIndividualProfilesCheck, | ||
showRemoveButton, | ||
onRemoveButtonClick, | ||
isActive, | ||
}: AddAnimalsFormCardProps) { | ||
const { control, watch, register } = useFormContext<FormFields>(); | ||
const { t } = useTranslation(); | ||
const watchAnimalCount = watch(FIELD_NAMES.COUNT) || 0; | ||
const watchAnimalType = watch(FIELD_NAMES.TYPE); | ||
const shouldCreateIndividualProfiles = watch(FIELD_NAMES.CREATE_INDIVIDUAL_PROFILES); | ||
|
||
return ( | ||
<Card className={styles.form} isActive={isActive}> | ||
<div className={styles.formHeader}> | ||
<Main>{t('ADD_ANIMAL.ADD_TO_INVENTORY')}</Main> | ||
{showRemoveButton && ( | ||
<TextButton className={styles.removeBtn} onClick={onRemoveButtonClick}> | ||
<XIcon /> {t('common:REMOVE')} | ||
</TextButton> | ||
)} | ||
</div> | ||
<AnimalTypeSelect | ||
name={FIELD_NAMES.TYPE} | ||
control={control} | ||
typeOptions={typeOptions} | ||
onTypeChange={onTypeChange} | ||
/> | ||
<AnimalBreedSelect | ||
name={FIELD_NAMES.BREED} | ||
control={control} | ||
breedOptions={breedOptions} | ||
isTypeSelected={!!watchAnimalType} | ||
/> | ||
|
||
<div className={styles.countAndSexDetailsWrapper}> | ||
<NumberInput | ||
name={FIELD_NAMES.COUNT} | ||
control={control} | ||
defaultValue={0} | ||
label={t('common:COUNT')} | ||
className={styles.countInput} | ||
allowDecimal={false} | ||
showStepper | ||
/> | ||
<Controller | ||
name={FIELD_NAMES.SEX_DETAILS} | ||
control={control} | ||
render={({ field }) => ( | ||
<SexDetails | ||
initialDetails={sexDetailsOptions} | ||
maxCount={watchAnimalCount} | ||
onConfirm={(details) => field.onChange(details)} | ||
/> | ||
)} | ||
/> | ||
</div> | ||
<Checkbox | ||
label={t('ADD_ANIMAL.CREATE_INDIVIDUAL_PROFILES')} | ||
tooltipContent={t('ADD_ANIMAL.CREATE_INDIVIDUAL_PROFILES_TOOLTIP')} | ||
hookFormRegister={register(FIELD_NAMES.CREATE_INDIVIDUAL_PROFILES)} | ||
onChange={(e) => onIndividualProfilesCheck?.((e.target as HTMLInputElement).checked)} | ||
/> | ||
{shouldCreateIndividualProfiles ? ( | ||
// @ts-ignore | ||
<Input | ||
label={t('ADD_ANIMAL.GROUP_NAME')} | ||
optional | ||
placeholder={t('ADD_ANIMAL.GROUP_NAME_PLACEHOLDER')} | ||
hookFormRegister={register(FIELD_NAMES.GROUP)} | ||
/> | ||
) : ( | ||
// @ts-ignore | ||
<Input | ||
label={t('ADD_ANIMAL.BATCH_NAME')} | ||
optional | ||
placeholder={t('ADD_ANIMAL.BATCH_NAME_PLACEHOLDER')} | ||
hookFormRegister={register(FIELD_NAMES.BATCH)} | ||
/> | ||
)} | ||
</Card> | ||
); | ||
} |
Oops, something went wrong.