import { HexString } from '@gear-js/api'; import { Button } from '@gear-js/ui'; import { zodResolver } from '@hookform/resolvers/zod'; import clsx from 'clsx'; import { FormProvider, useForm } from 'react-hook-form'; import { z } from 'zod'; import { useProgramIdSchema } from '@/hooks'; import CloseSVG from '@/shared/assets/images/actions/close.svg?react'; import { Input } from '@/shared/ui'; import styles from './programs-form.module.scss'; const FIELD_NAME = { PROGRAM_ID: 'id', } as const; type Values = { [FIELD_NAME.PROGRAM_ID]: string; }; type FormattedValues = { [FIELD_NAME.PROGRAM_ID]: HexString; }; const DEFAULT_VALUES: Values = { [FIELD_NAME.PROGRAM_ID]: '', } as const; type Props = { value: HexString[]; voucherValue?: HexString[]; onChange: (value: (prevState: HexString[]) => HexString[]) => void; }; const ProgramsForm = ({ value, voucherValue = [], onChange }: Props) => { const programIdSchema = useProgramIdSchema(value); const schema = z.object({ [FIELD_NAME.PROGRAM_ID]: programIdSchema, }); const form = useForm({ defaultValues: DEFAULT_VALUES, resolver: zodResolver(schema), }); const add = (id: HexString) => onChange((prevValue) => [...prevValue, id]); const remove = (id: HexString) => onChange((prevValue) => prevValue.filter((_id) => id !== _id)); const handleSubmit = ({ id }: FormattedValues) => { add(id); form.reset(); }; const renderPreviousPrograms = () => voucherValue.map((id) => (
  • {id}
  • )); const renderPrograms = () => value.map((id) => (
  • {id}
  • )); return (
    {/* temporary button alignment fix */}
    {Boolean([...voucherValue, ...value].length) && (
      {renderPreviousPrograms()} {renderPrograms()}
    )}
    ); }; export { ProgramsForm };