|
| 1 | + |
| 2 | +import React from "react"; |
| 3 | +import { useForm } from 'react-hook-form'; |
| 4 | + |
| 5 | +import { EAction} from "../ManageApiProductsCommon"; |
| 6 | +import { TAPManagedAssetDisplay_Attributes } from "../../../../displayServices/APManagedAssetDisplayService"; |
| 7 | +import { TAPAttributeDisplayList } from "../../../../displayServices/APAttributesDisplayService/APAttributesDisplayService"; |
| 8 | +import { EditNewApAttributeListForm } from "../../../../components/APManageAttributes/EditNewApAttributeListForm"; |
| 9 | + |
| 10 | +import '../../../../components/APComponents.css'; |
| 11 | +import "../ManageApiProducts.css"; |
| 12 | + |
| 13 | +export interface IEditNewMetaAttributesFormProps { |
| 14 | + action: EAction; |
| 15 | + formId: string; |
| 16 | + apManagedAssetDisplay_Attributes: TAPManagedAssetDisplay_Attributes; |
| 17 | + onSubmit: (apManagedAssetDisplay_Attributes: TAPManagedAssetDisplay_Attributes) => void; |
| 18 | +} |
| 19 | + |
| 20 | +export const EditNewMetaAttributesForm: React.FC<IEditNewMetaAttributesFormProps> = (props: IEditNewMetaAttributesFormProps) => { |
| 21 | + const ComponentName = 'EditNewMetaAttributesForm'; |
| 22 | + |
| 23 | + type TManagedObject = TAPManagedAssetDisplay_Attributes; |
| 24 | + type TManagedObjectFormData = { |
| 25 | + meta_attribute_list: TAPAttributeDisplayList; |
| 26 | + }; |
| 27 | + type TManagedObjectFormDataEnvelope = { |
| 28 | + formData: TManagedObjectFormData; |
| 29 | + } |
| 30 | + |
| 31 | + const transform_ManagedObject_To_FormDataEnvelope = (mo: TManagedObject): TManagedObjectFormDataEnvelope => { |
| 32 | + const fd: TManagedObjectFormData = { |
| 33 | + meta_attribute_list: mo.apMeta_ApAttributeDisplayList, |
| 34 | + }; |
| 35 | + return { |
| 36 | + formData: fd |
| 37 | + }; |
| 38 | + } |
| 39 | + |
| 40 | + const create_ManagedObject_From_FormEntities = ({formDataEnvelope}: { |
| 41 | + formDataEnvelope: TManagedObjectFormDataEnvelope; |
| 42 | + }): TManagedObject => { |
| 43 | + const mo: TManagedObject = props.apManagedAssetDisplay_Attributes; |
| 44 | + const fd: TManagedObjectFormData = formDataEnvelope.formData; |
| 45 | + mo.apMeta_ApAttributeDisplayList = fd.meta_attribute_list; |
| 46 | + return mo; |
| 47 | + } |
| 48 | + |
| 49 | + const [managedObject] = React.useState<TManagedObject>(props.apManagedAssetDisplay_Attributes); |
| 50 | + const [managedObjectFormDataEnvelope, setManagedObjectFormDataEnvelope] = React.useState<TManagedObjectFormDataEnvelope>(); |
| 51 | + // const [apiCallStatus, setApiCallStatus] = React.useState<TApiCallState | null>(null); |
| 52 | + const managedObjectUseForm = useForm<TManagedObjectFormDataEnvelope>(); |
| 53 | + |
| 54 | + const doInitialize = async () => { |
| 55 | + setManagedObjectFormDataEnvelope(transform_ManagedObject_To_FormDataEnvelope(managedObject)); |
| 56 | + } |
| 57 | + |
| 58 | + // * useEffect Hooks * |
| 59 | + |
| 60 | + React.useEffect(() => { |
| 61 | + doInitialize(); |
| 62 | + }, []); /* eslint-disable-line react-hooks/exhaustive-deps */ |
| 63 | + |
| 64 | + React.useEffect(() => { |
| 65 | + if(managedObjectFormDataEnvelope) managedObjectUseForm.setValue('formData', managedObjectFormDataEnvelope.formData); |
| 66 | + }, [managedObjectFormDataEnvelope]) /* eslint-disable-line react-hooks/exhaustive-deps */ |
| 67 | + |
| 68 | + const onSubmitManagedObjectForm = (newMofde: TManagedObjectFormDataEnvelope) => { |
| 69 | + // const funcName = 'onSubmitManagedObjectForm'; |
| 70 | + // const logName = `${ComponentName}.${funcName}()`; |
| 71 | + // alert(`${logName}: newMofde = ${JSON.stringify(newMofde, null, 2)}`); |
| 72 | + props.onSubmit(create_ManagedObject_From_FormEntities({ |
| 73 | + formDataEnvelope: newMofde, |
| 74 | + })); |
| 75 | + } |
| 76 | + |
| 77 | + const onInvalidSubmitManagedObjectForm = () => { |
| 78 | + // placeholder |
| 79 | + } |
| 80 | + |
| 81 | + const onChange_MetaAttributes = (apAttributeDisplayList: TAPAttributeDisplayList) => { |
| 82 | + const funcName = 'onChange_MetaAttributes'; |
| 83 | + const logName = `${ComponentName}.${funcName}()`; |
| 84 | + if(managedObjectFormDataEnvelope === undefined) throw new Error(`${logName}: managedObjectFormDataEnvelope === undefined`); |
| 85 | + const newMofde: TManagedObjectFormDataEnvelope = { |
| 86 | + formData: { |
| 87 | + ...managedObjectFormDataEnvelope.formData, |
| 88 | + meta_attribute_list: apAttributeDisplayList |
| 89 | + } |
| 90 | + }; |
| 91 | + setManagedObjectFormDataEnvelope(newMofde); |
| 92 | + } |
| 93 | + |
| 94 | + const renderManagedObjectForm = () => { |
| 95 | + const funcName = 'renderManagedObjectForm'; |
| 96 | + const logName = `${ComponentName}.${funcName}()`; |
| 97 | + if(managedObjectFormDataEnvelope === undefined) throw new Error(`${logName}: managedObjectFormDataEnvelope === undefined`); |
| 98 | + const uniqueKey_MetaAttributes = ComponentName+'_EditNewApAttributeListForm_mo.apMeta_ApAttributeDisplayList'; |
| 99 | + return ( |
| 100 | + <div className="card p-mt-4"> |
| 101 | + <div className="p-fluid"> |
| 102 | + <form id={props.formId} onSubmit={managedObjectUseForm.handleSubmit(onSubmitManagedObjectForm, onInvalidSubmitManagedObjectForm)} className="p-fluid"> |
| 103 | + {/* empty form, required for submit button */} |
| 104 | + </form> |
| 105 | + |
| 106 | + <div className="p-field"> |
| 107 | + {/* version attributes */} |
| 108 | + <div className="p-text-bold p-mb-3">General Attributes:</div> |
| 109 | + <EditNewApAttributeListForm |
| 110 | + key={uniqueKey_MetaAttributes} |
| 111 | + uniqueKeyPrefix={uniqueKey_MetaAttributes} |
| 112 | + apAttributeDisplayList={managedObjectFormDataEnvelope.formData.meta_attribute_list} |
| 113 | + attributeName_Name="Attribute Name" |
| 114 | + attributeValue_Name="Value" |
| 115 | + onChange={onChange_MetaAttributes} |
| 116 | + /> |
| 117 | + </div> |
| 118 | + </div> |
| 119 | + </div> |
| 120 | + ); |
| 121 | + } |
| 122 | + |
| 123 | + |
| 124 | + return ( |
| 125 | + <div className="manage-api-products"> |
| 126 | + |
| 127 | + { managedObjectFormDataEnvelope && renderManagedObjectForm() } |
| 128 | + |
| 129 | + </div> |
| 130 | + ); |
| 131 | +} |
0 commit comments