/** ************************************************************************* * The contents of this file were generated with Amplify Studio. * * Please refrain from making any modifications to this file. * * Any changes to this file will be overwritten when running amplify pull. * ************************************************************************* */ /* eslint-disable */ import * as React from 'react'; import { fetchByPath, validateField } from './utils'; import { Member, Team } from '../models'; import { getOverrideProps, useDataStoreBinding } from '@aws-amplify/ui-react/internal'; import { Autocomplete, Badge, Button, Divider, Flex, Grid, Icon, ScrollView, Text, TextField, useTheme, } from '@aws-amplify/ui-react'; import { DataStore } from 'aws-amplify'; function ArrayField({ items = [], onChange, label, inputFieldRef, children, hasError, setFieldValue, currentFieldValue, defaultFieldValue, lengthLimit, getBadgeText, }) { const { tokens } = useTheme(); const [selectedBadgeIndex, setSelectedBadgeIndex] = React.useState(); const [isEditing, setIsEditing] = React.useState(); React.useEffect(() => { if (isEditing) { inputFieldRef?.current?.focus(); } }, [isEditing]); const removeItem = async (removeIndex) => { const newItems = items.filter((value, index) => index !== removeIndex); await onChange(newItems); setSelectedBadgeIndex(undefined); }; const addItem = async () => { if ((currentFieldValue !== undefined || currentFieldValue !== null || currentFieldValue !== '') && !hasError) { const newItems = [...items]; if (selectedBadgeIndex !== undefined) { newItems[selectedBadgeIndex] = currentFieldValue; setSelectedBadgeIndex(undefined); } else { newItems.push(currentFieldValue); } await onChange(newItems); setIsEditing(false); } }; const arraySection = ( {label} {!!items?.length && ( {items.map((value, index) => { return ( { setSelectedBadgeIndex(index); setFieldValue(getBadgeText ? getBadgeText(items[index]) : items[index]); setIsEditing(true); }} > {getBadgeText ? getBadgeText(value) : value.toString()} { event.stopPropagation(); removeItem(index); }} /> ); })} )} ); if (lengthLimit !== undefined && items.length >= lengthLimit && !isEditing) { return arraySection; } return ( {isEditing && children} {!isEditing ? ( <> {label} ) : ( {(currentFieldValue || isEditing) && ( )} )} ); } export default function MyMemberForm(props) { const { clearOnSuccess = true, onSuccess, onError, onSubmit, onCancel, onValidate, onChange, overrides, ...rest } = props; const initialValues = { name: undefined, team: undefined, }; const [name, setName] = React.useState(initialValues.name); const [team, setTeam] = React.useState(initialValues.team); const [errors, setErrors] = React.useState({}); const resetStateValues = () => { setName(initialValues.name); setTeam(initialValues.team); setCurrentTeamValue(undefined); setCurrentTeamDisplayValue(undefined); setErrors({}); }; const [currentTeamDisplayValue, setCurrentTeamDisplayValue] = React.useState(undefined); const [currentTeamValue, setCurrentTeamValue] = React.useState(undefined); const teamRef = React.createRef(); const teamRecords = useDataStoreBinding({ type: 'collection', model: Team, }).items; const getDisplayValue = { team: (record) => record?.id, }; const validations = { name: [], team: [], }; const runValidationTasks = async (fieldName, currentValue, getDisplayValue) => { const value = getDisplayValue ? getDisplayValue(currentValue) : currentValue; let validationResponse = validateField(value, validations[fieldName]); const customValidator = fetchByPath(onValidate, fieldName); if (customValidator) { validationResponse = await customValidator(value, validationResponse); } setErrors((errors) => ({ ...errors, [fieldName]: validationResponse })); return validationResponse; }; return ( { event.preventDefault(); let modelFields = { name, team, }; const validationResponses = await Promise.all( Object.keys(validations).reduce((promises, fieldName) => { if (Array.isArray(modelFields[fieldName])) { promises.push( ...modelFields[fieldName].map((item) => runValidationTasks(fieldName, item, getDisplayValue[fieldName]), ), ); return promises; } promises.push(runValidationTasks(fieldName, modelFields[fieldName], getDisplayValue[fieldName])); return promises; }, []), ); if (validationResponses.some((r) => r.hasError)) { return; } if (onSubmit) { modelFields = onSubmit(modelFields); } try { await DataStore.save(new Member(modelFields)); if (onSuccess) { onSuccess(modelFields); } if (clearOnSuccess) { resetStateValues(); } } catch (err) { if (onError) { onError(modelFields, err.message); } } }} {...rest} {...getOverrideProps(overrides, 'MyMemberForm')} > { let { value } = e.target; if (onChange) { const modelFields = { name: value, team, }; const result = onChange(modelFields); value = result?.name ?? value; } if (errors.name?.hasError) { runValidationTasks('name', value); } setName(value); }} onBlur={() => runValidationTasks('name', name)} errorMessage={errors.name?.errorMessage} hasError={errors.name?.hasError} {...getOverrideProps(overrides, 'name')} > { let value = items[0]; if (onChange) { const modelFields = { name, team: value, }; const result = onChange(modelFields); value = result?.team ?? value; } setTeam(value); setCurrentTeamValue(undefined); setCurrentTeamDisplayValue(undefined); }} currentFieldValue={currentTeamValue} label={'Team'} items={team ? [team] : []} hasError={errors.team?.hasError} getBadgeText={getDisplayValue.team} setFieldValue={currentTeamDisplayValue} inputFieldRef={teamRef} defaultFieldValue={undefined} > ({ id: r.id, label: getDisplayValue.team?.(r) ?? r.id, }))} onSelect={({ id, label }) => { setCurrentTeamValue(teamRecords.find((r) => r.id === id)); setCurrentTeamDisplayValue(label); }} onChange={(e) => { let { value } = e.target; if (errors.team?.hasError) { runValidationTasks('team', value); } setCurrentTeamDisplayValue(value); setCurrentTeamValue(undefined); }} onBlur={() => runValidationTasks('team', team)} errorMessage={errors.team?.errorMessage} hasError={errors.team?.hasError} ref={teamRef} {...getOverrideProps(overrides, 'team')} > ); }