import React from 'react'; import { yupResolver } from '@hookform/resolvers/yup'; import…
- User
import React from 'react';
import { yupResolver } from '@hookform/resolvers/yup'; import { Checkbox, FormControlLabel, FormGroup } from '@material-ui/core'; import cx from 'classnames'; import { isAfter, isBefore, isValid } from 'date-fns'; import { Controller, useForm } from 'react-hook-form'; import { useTranslation } from 'react-i18next'; import * as Yup from 'yup';
import { Button, DateTimePicker, Label, RadioGroup, Select, TagSelector, } from 'components/common'; import { MAX_DATE, MIN_DATE, eventDateOptions } from 'constants/manualActions'; import { useFlagsContext } from 'contexts/FlagsContext'; import { blockSettlementDefault, useManualActionsContext, } from 'contexts/ManualActionsContext'; import { BranchStatus } from 'models/BranchStatus'; import { Competition, ListCompetitions } from 'models/Competition'; import { ListEvents } from 'models/Context'; import { EventSearchCheckBoxesOptions, EventSearchFormValues, } from 'models/EventSearchForm'; import { FeatureFlags } from 'models/FeatureFlags'; import { Sport } from 'models/Sport'; import { getDatesFromRange } from 'utils/Date';
export interface EventSearchFormProps { sports: Sport[]; competitions: Competition[]; selectedSearchValues: EventSearchFormValues; listCompetitions: ListCompetitions; listCompetitionsStatus: BranchStatus; listEvents: ListEvents; listEventsStatus: BranchStatus; rowsPerPage: number; disableEventStatus: boolean; }
const showBlockSettlement = (flags: FeatureFlags): boolean => !flags.BLOCK_SETTLEMENT_FLAG;
const checkBoxes: EventSearchCheckBoxesOptions = [ { labelName: 'resettlementBlockedEvents', formName: 'eventStatusBlocked', }, { labelName: 'settlementBlockedEvents', formName: 'eventStatus', }, { labelName: 'unsettledEvents', formName: 'isFullyResulted', }, ];
export const EventSearchForm: React.FC = ({ sports, competitions, selectedSearchValues, listCompetitions, listCompetitionsStatus, listEvents, listEventsStatus, rowsPerPage, disableEventStatus, }) => { const { t } = useTranslation(); const { flags } = useFlagsContext();
const { setBlockSettlementPayload } = useManualActionsContext();
const { control, handleSubmit, setValue, watch } = useForm({ resolver: yupResolver( Yup.object().shape({ sport: Yup.string().required(), competitions: Yup.array().required(), fromDate: Yup.date().required(), toDate: Yup.date().required(), eventDate: Yup.string().nullable(), eventStatusBlocked: Yup.boolean().required(), isFullyResulted: Yup.boolean(), eventStatus: Yup.boolean(), }), ), });
const eventDateWatch = watch('eventDate'); const sportWatch = watch('sport'); const competitionsWatch = watch('competitions'); const toDateWatch = watch('toDate'); const fromDateWatch = watch('fromDate');
React.useEffect(() => { if (typeof eventDateWatch !== 'undefined' && eventDateWatch) { const { fromDate, toDate } = getDatesFromRange( new Date(Date.now()), eventDateWatch, );
setValue('fromDate', fromDate); setValue('toDate', toDate); selectedSearchValues.fromDate = fromDate; selectedSearchValues.toDate = toDate; } else if (typeof eventDateWatch === 'undefined') { setValue('fromDate', selectedSearchValues.fromDate); setValue('toDate', selectedSearchValues.toDate); } - Assistant
Yes, you can create a function or custom hook to handle this logic and keep your JSX cleaner. Here's an example of how you might create a function to determine if the submit button should be disabled:
const isSubmitButtonDisabled = ( sport: string, competitions: string[], fromDate: Date, toDate: Date, listEventsStatus: BranchStatus ) => { if (!sport || !competitions?.length) return true; if (!isValid(toDate) || !isValid(fromDate)) return true; if (!isBefore(fromDate as Date, toDate as Date)) return true; if (isBefore(fromDate as Date, new Date(MIN_DATE))) return true; if (isAfter(toDate as Date, new Date(MAX_DATE))) return true; if (listEventsStatus === BranchStatus.LOADING) return true; return false; };Then, you can use this function in the JSX like this:
<Button testId="event-search-form-submit-button" type="submit" buttonColor="positive" disabled={isSubmitButtonDisabled( sportWatch, competitionsWatch, fromDateWatch, toDateWatch, listEventsStatus )} className="event-search-form__submit-button" loading={listEventsStatus === BranchStatus.LOADING} > {t('common.cta.submit')} </Button> - User
no thank you. anything else? using yup?
- Assistant
Yes, you can use Yup validation schema to handle some of the validation logic. For example, you can add custom validation tests to validate the
fromDateand values using the schema itself. Here's how you can modify the schema to handle the date validation:
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.