import React from 'react' import { useEffect } from 'react'; import { Control, FieldValues, useFieldArray, useForm, UseFormRegister, UseFormSetValue } from 'react-hook-form'; import { useFetchWithAuth } from '../../hooks/useFetchWithAuth'; import { Manytooneform } from './Manytooneform'; import TableHeadForm from './TableHeadForm'; interface Props { entityName: string, entityId: number, element:string, oneToManyHeader: {id:string, name:string}[], control: Control, register: UseFormRegister, setValue: UseFormSetValue } export const Onetomany = ({entityName, entityId, element, oneToManyHeader, control, register, setValue }:Props) => { const { fields, append, remove } = useFieldArray({ control, name: element }); const getInitData = async () => { const { data, error } = await useFetchWithAuth(entityName + "/" + entityId); if (!error) { setValue(element,data[element]); } else { console.log(error) } } useEffect(() => { getInitData(); }, [entityId]) const renderElement = (key:string, attribute: string) => { switch (attribute){ case 'String': return ; case "ManyToOne": return case 'Long': return ; case 'Boolean': return ; case 'BigDecimal': return ; case 'LocalDateTime': return ; default: console.log(key) return {key}; } } const columnNames = oneToManyHeader.map(column => (column.id)) let defaultvalue = ""; if (columnNames.length>0){ defaultvalue = columnNames.reduce((acc, curr) => { acc = ""; return acc; }, ); }else{ } return (
{ fields.map((line, index) => { return ( { oneToManyHeader.map(column => ( )) } ) }) }
{renderElement(`${element}.${index}.${column.id}`,column.name)}
) }