Skip to content

Commit

Permalink
fix(26237): fix context for the RJSF form
Browse files Browse the repository at this point in the history
  • Loading branch information
vanch3d committed Sep 19, 2024
1 parent d9a1e30 commit eaf53e6
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ export const MqttTransformationField: FC<FieldProps<OutwardSubscription[], RJSFS
const [selectedItem, setSelectedItem] = useState<number | undefined>(undefined)
const [subsData, setSubsData] = useState<OutwardSubscription[] | undefined>(props.formData)

const { adapterId, adapterType } = props.formContext || {}

useEffect(() => {
// TODO[NVL] Add validation and persistence
return () => undefined
Expand Down Expand Up @@ -92,6 +94,8 @@ export const MqttTransformationField: FC<FieldProps<OutwardSubscription[], RJSFS
<AccordionPanel pb={4}>
{selectedItem !== undefined && (
<SubscriptionContainer
adapterId={adapterId}
adapterType={adapterType}
item={subsData[selectedItem]}
onClose={handleClose}
onSubmit={handleSubmit}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,21 @@ export enum MappingStrategy {

interface SubscriptionContainerProps {
item: OutwardSubscription
adapterType?: string
adapterId?: string
onClose: () => void
onSubmit: (newItem: OutwardSubscription) => void
onChange: (id: keyof OutwardSubscription, v: JsonNode | string | string[] | undefined) => void
}

const SubscriptionContainer: FC<SubscriptionContainerProps> = ({ item, onClose, onSubmit, onChange }) => {
const SubscriptionContainer: FC<SubscriptionContainerProps> = ({
adapterId,
adapterType,
item,
onClose,
onSubmit,
onChange,
}) => {
const [strategy] = useState<MappingStrategy>(MappingStrategy.TYPED)
const validation = useMappingValidation(item)

Expand All @@ -40,7 +49,12 @@ const SubscriptionContainer: FC<SubscriptionContainerProps> = ({ item, onClose,
</VStack>
<VStack flex={2} alignItems="stretch">
<Box maxW="50%">
<SelectDestinationTag values={[item.node]} onChange={(v) => onChange('node', v)} />
<SelectDestinationTag
adapterId={adapterId}
adapterType={adapterType}
values={[item.node]}
onChange={(v) => onChange('node', v)}
/>
</Box>
<HStack alignItems="stretch">
{strategy != MappingStrategy.EXACT && (
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ const AdapterSubscriptionManager: FC<AdapterSubscriptionManagerProps> = ({ type
</DrawerHeader>
<DrawerBody display="flex" flexDirection="column" gap={6}>
{!adapterId && <ErrorMessage message={t('protocolAdapter.error.loading')} />}
{adapterId && <SubscriptionForm id={adapterId} type={type} />}
{adapterId && <SubscriptionForm adapterId={adapterId} adapterType={selectedNode?.data.type} type={type} />}
</DrawerBody>
</DrawerContent>
</Drawer>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,19 +12,28 @@ import { customFormatsValidator } from '@/modules/ProtocolAdapters/utils/validat
import { adapterJSFFields, adapterJSFWidgets } from '@/modules/ProtocolAdapters/utils/uiSchema.utils.ts'
import { useSubscriptionManager } from '@/modules/Subscriptions/hooks/useSubscriptionManager.tsx'
import { useTranslation } from 'react-i18next'
import { AdapterContext } from '@/modules/ProtocolAdapters/types.ts'

interface SubscriptionFormProps {
id: string
adapterId: string
adapterType?: string
type: 'inward' | 'outward'
}

// TODO[NVL] Should replicate the config from the adapter form; share component?
const SubscriptionForm: FC<SubscriptionFormProps> = ({ id, type }) => {
const SubscriptionForm: FC<SubscriptionFormProps> = ({ adapterId, adapterType, type }) => {
const { t } = useTranslation()
const { inwardManager, outwardManager } = useSubscriptionManager(id)
const { inwardManager, outwardManager } = useSubscriptionManager(adapterId)

const subscriptionManager = type === 'inward' ? inwardManager : outwardManager

const context: AdapterContext = {
isEditAdapter: true,
isDiscoverable: false,
adapterType: adapterType,
adapterId: adapterId,
}

const onFormSubmit = useCallback(
(data: IChangeEvent) => {
const subscriptions = data.formData?.subscriptions
Expand All @@ -48,6 +57,7 @@ const SubscriptionForm: FC<SubscriptionFormProps> = ({ id, type }) => {
formData={subscriptionManager.formData}
widgets={adapterJSFWidgets}
fields={adapterJSFFields}
formContext={context}
templates={{
ObjectFieldTemplate,
FieldTemplate,
Expand Down

0 comments on commit eaf53e6

Please sign in to comment.