1+ import { useCurrentBranch } from "@/entities/branches/ui/branches-provider" ;
12import { createObject } from "@/entities/nodes/api/createObject" ;
23import { updateObjectWithId } from "@/entities/nodes/api/updateObjectWithId" ;
4+ import { AttributeType , RelationshipType } from "@/entities/nodes/getObjectItemDisplayValue" ;
5+ import { useGetObject } from "@/entities/nodes/object/domain/get-object.query" ;
6+ import { useSchema } from "@/entities/schema/ui/hooks/useSchema" ;
7+ import { NODE_TRIGGER_ATTRIBUTE_MATCH , NODE_TRIGGER_RULE } from "@/entities/triggers/constants" ;
38import graphqlClient from "@/shared/api/graphql/graphqlClientApollo" ;
49import { Button } from "@/shared/components/buttons/button-primitive" ;
10+ import { DynamicInput } from "@/shared/components/form/dynamic-form" ;
11+ import { LabelFormField } from "@/shared/components/form/fields/common" ;
12+ import DropdownField from "@/shared/components/form/fields/dropdown.field" ;
513import { NodeFormProps } from "@/shared/components/form/node-form" ;
6- import { DynamicDropdownFieldProps , FormFieldValue } from "@/shared/components/form/type" ;
14+ import {
15+ DynamicDropdownFieldProps ,
16+ FormAttributeValue ,
17+ FormFieldValue ,
18+ } from "@/shared/components/form/type" ;
719import { getCurrentFieldValue } from "@/shared/components/form/utils/getFieldDefaultValue" ;
20+ import { getFormFieldsFromSchema } from "@/shared/components/form/utils/getFormFieldsFromSchema" ;
21+ import { getRelationshipDefaultValue } from "@/shared/components/form/utils/getRelationshipDefaultValue" ;
822import { getCreateMutationFromFormDataOnly } from "@/shared/components/form/utils/mutations/getCreateMutationFromFormData" ;
23+ import { DropdownOption } from "@/shared/components/inputs/dropdown" ;
24+ import { Skeleton } from "@/shared/components/skeleton" ;
925import { ALERT_TYPES , Alert } from "@/shared/components/ui/alert" ;
1026import { Form , FormSubmit } from "@/shared/components/ui/form" ;
1127import { datetimeAtom } from "@/shared/stores/time.atom" ;
1228import { stringifyWithoutQuotes } from "@/shared/utils/string" ;
1329import { gql } from "@apollo/client" ;
1430import { useAtomValue } from "jotai" ;
15- import { FieldValues , useForm } from "react-hook-form" ;
31+ import { FieldValues , useForm , useFormContext } from "react-hook-form" ;
1632import { toast } from "react-toastify" ;
1733
18- import { useCurrentBranch } from "@/entities/branches/ui/branches-provider" ;
19- import { AttributeType , RelationshipType } from "@/entities/nodes/getObjectItemDisplayValue" ;
20- import { useGetObject } from "@/entities/nodes/object/domain/get-object.query" ;
21- import { useSchema } from "@/entities/schema/ui/hooks/useSchema" ;
22- import { DynamicInput } from "@/shared/components/form/dynamic-form" ;
23- import { LabelFormField } from "@/shared/components/form/fields/common" ;
24- import DropdownField from "@/shared/components/form/fields/dropdown.field" ;
25- import { getFormFieldsFromSchema } from "@/shared/components/form/utils/getFormFieldsFromSchema" ;
26- import { getRelationshipDefaultValue } from "@/shared/components/form/utils/getRelationshipDefaultValue" ;
27- import { DropdownOption } from "@/shared/components/inputs/dropdown" ;
28- import { Skeleton } from "@/shared/components/skeleton" ;
29- import { useParams } from "react-router" ;
30- import { NODE_TRIGGER_ATTRIBUTE_MATCH } from "../constants" ;
31-
3234interface NodeAttributeMatchFormProps extends NodeFormProps { }
3335
3436export const NodeAttributeMatchForm = ( {
@@ -41,9 +43,6 @@ export const NodeAttributeMatchForm = ({
4143} : NodeAttributeMatchFormProps ) => {
4244 const { currentBranch } = useCurrentBranch ( ) ;
4345 const date = useAtomValue ( datetimeAtom ) ;
44- const { objectKind, objectid } = useParams ( ) ;
45- const { schema } = useSchema ( objectKind ) ;
46- const { data, isPending } = useGetObject ( { objectSchema : schema , objectId : objectid } ) ;
4746
4847 const schemaFields = getFormFieldsFromSchema ( {
4948 ...props ,
@@ -123,7 +122,7 @@ export const NodeAttributeMatchForm = ({
123122 } ,
124123 } ) ;
125124
126- if ( currentObject ) {
125+ if ( currentObject ?. id ) {
127126 toast ( < Alert type = { ALERT_TYPES . SUCCESS } message = { "Node attribute match updated!" } /> , {
128127 toastId : "alert-success-node-attribute-match-updated" ,
129128 } ) ;
@@ -145,11 +144,7 @@ export const NodeAttributeMatchForm = ({
145144 return (
146145 < div className = { "bg-white flex flex-col flex-1 overflow-auto p-4" } >
147146 < Form form = { form } onSubmit = { handleSubmit } >
148- < NodeAttributeField
149- field = { attributeField }
150- kind = { data ?. node_kind ?. value }
151- isLoading = { isPending }
152- />
147+ < NodeAttributeField field = { attributeField } />
153148
154149 { fields . map ( ( field ) => {
155150 return < DynamicInput key = { field . name } { ...field } /> ;
@@ -170,15 +165,23 @@ export const NodeAttributeMatchForm = ({
170165} ;
171166
172167interface NodeAttributeFieldProps {
173- kind ?: string ;
174- isLoading ?: boolean ;
175168 field ?: DynamicDropdownFieldProps ;
176169}
177170
178- const NodeAttributeField = ( { field, kind, isLoading } : NodeAttributeFieldProps ) => {
179- const { schema } = useSchema ( kind ) ;
171+ const NodeAttributeField = ( { field } : NodeAttributeFieldProps ) => {
172+ const form = useFormContext ( ) ;
173+
174+ const { schema } = useSchema ( NODE_TRIGGER_RULE ) ;
175+ const selectedTriggerField : FormAttributeValue = form . watch ( "trigger" ) ;
176+
177+ const { data, isPending } = useGetObject ( {
178+ objectId : selectedTriggerField . value ?. id ,
179+ objectSchema : schema ,
180+ } ) ;
180181
181- if ( isLoading ) {
182+ const { schema : peerSchema } = useSchema ( data ?. node_kind ?. value ) ;
183+
184+ if ( isPending ) {
182185 return (
183186 < div className = "space-y-2" >
184187 < LabelFormField
@@ -193,12 +196,19 @@ const NodeAttributeField = ({ field, kind, isLoading }: NodeAttributeFieldProps)
193196 }
194197
195198 const attributeOptions : Array < DropdownOption > =
196- schema ?. attributes ?. map ( ( attribute ) => {
199+ peerSchema ?. attributes ?. map ( ( attribute ) => {
197200 return {
198201 value : attribute . name ,
199202 label : attribute . label ?? attribute . name ,
200203 } ;
201204 } ) ?? [ ] ;
202205
203- return < DropdownField { ...field } name = "attribute_name" items = { attributeOptions } /> ;
206+ return (
207+ < DropdownField
208+ { ...field }
209+ key = { data ?. node_kind ?. value }
210+ name = "attribute_name"
211+ items = { attributeOptions }
212+ />
213+ ) ;
204214} ;
0 commit comments