11// Libraries
2- import React , { FC } from 'react'
2+ import React , { FC , useContext , useEffect } from 'react'
33
44// Components
55import {
@@ -24,6 +24,7 @@ import {Subscription} from 'src/types/subscriptions'
2424// Styles
2525import 'src/writeData/subscriptions/components/SubscriptionDetails.scss'
2626import { event } from 'src/cloud/utils/reporting'
27+ import { WriteDataDetailsContext } from 'src/writeData/components/WriteDataDetailsContext'
2728
2829interface Props {
2930 currentSubscription : Subscription
@@ -37,77 +38,92 @@ const SubscriptionFormContent: FC<Props> = ({
3738 updateForm,
3839 className,
3940 edit,
40- } ) => (
41- < Grid >
42- < Grid . Row >
43- < Grid . Column widthSM = { Columns . Twelve } >
44- < Form . ValidationElement
45- label = "Topic"
46- value = { currentSubscription . topic }
47- helpText = "Subscribe to a topic to start receiving messages."
48- required = { true }
49- validationFunc = { ( ) =>
50- handleValidation ( 'Topic' , currentSubscription . topic )
51- }
52- >
53- { status => (
54- < Input
55- type = { InputType . Text }
56- placeholder = "Enter a topic (wildcards accepted)"
57- name = "topic"
58- autoFocus = { false }
59- value = { currentSubscription . topic }
60- onChange = { e => {
61- updateForm ( {
62- ...currentSubscription ,
63- topic : e . target . value ,
64- } )
65- } }
66- onBlur = { ( ) =>
67- event (
68- 'completed form field' ,
69- { formField : 'topic' } ,
70- { feature : 'subscriptions' }
71- )
72- }
73- status = { edit ? status : ComponentStatus . Disabled }
74- maxLength = { 56 }
75- testID = { `${ className } -subscription-form--topic` }
76- />
77- ) }
78- </ Form . ValidationElement >
79- </ Grid . Column >
80- < Grid . Column widthXS = { Columns . Twelve } >
81- < Heading
82- element = { HeadingElement . H3 }
83- weight = { FontWeight . Bold }
84- className = { `${ className } -subscription-form__header` }
85- >
86- Write Destination
87- </ Heading >
88- { className === 'create' || edit ? (
89- < div >
41+ } ) => {
42+ const { bucket, buckets, changeBucket} = useContext ( WriteDataDetailsContext )
43+ useEffect ( ( ) => {
44+ if ( bucket . name === currentSubscription ?. bucket ) {
45+ return
46+ }
47+
48+ const found = buckets . find ( b => b . name === currentSubscription ?. bucket )
49+ if ( found ) {
50+ changeBucket ( found )
51+ }
52+ } , [ ] )
53+
54+ return (
55+ < Grid >
56+ < Grid . Row >
57+ < Grid . Column widthSM = { Columns . Twelve } >
58+ < Form . ValidationElement
59+ label = "Topic"
60+ value = { currentSubscription . topic }
61+ helpText = "Subscribe to a topic to start receiving messages."
62+ required = { true }
63+ validationFunc = { ( ) =>
64+ handleValidation ( 'Topic' , currentSubscription . topic )
65+ }
66+ >
67+ { status => (
68+ < Input
69+ type = { InputType . Text }
70+ placeholder = "Enter a topic (wildcards accepted)"
71+ name = "topic"
72+ autoFocus = { false }
73+ value = { currentSubscription . topic }
74+ onChange = { e => {
75+ updateForm ( {
76+ ...currentSubscription ,
77+ topic : e . target . value ,
78+ } )
79+ } }
80+ onBlur = { ( ) =>
81+ event (
82+ 'completed form field' ,
83+ { formField : 'topic' } ,
84+ { feature : 'subscriptions' }
85+ )
86+ }
87+ status = { edit ? status : ComponentStatus . Disabled }
88+ maxLength = { 56 }
89+ testID = { `${ className } -subscription-form--topic` }
90+ />
91+ ) }
92+ </ Form . ValidationElement >
93+ </ Grid . Column >
94+ < Grid . Column widthXS = { Columns . Twelve } >
95+ < Heading
96+ element = { HeadingElement . H3 }
97+ weight = { FontWeight . Bold }
98+ className = { `${ className } -subscription-form__header` }
99+ >
100+ Write Destination
101+ </ Heading >
102+ { className === 'create' || edit ? (
103+ < div >
104+ < Heading
105+ element = { HeadingElement . H5 }
106+ weight = { FontWeight . Regular }
107+ className = { `${ className } -subscription-form__text` }
108+ >
109+ Select a bucket to write your data to.
110+ </ Heading >
111+ < WriteDataHelperBuckets className = "write-data--subscriptions-title" />
112+ </ div >
113+ ) : (
90114 < Heading
91- element = { HeadingElement . H5 }
115+ element = { HeadingElement . H4 }
92116 weight = { FontWeight . Regular }
93117 className = { `${ className } -subscription-form__text` }
118+ testID = { `${ className } -subscription-form--bucket` }
94119 >
95- Select a bucket to write your data to.
120+ Bucket: { currentSubscription && currentSubscription . bucket }
96121 </ Heading >
97- < WriteDataHelperBuckets className = "write-data--subscriptions-title" />
98- </ div >
99- ) : (
100- < Heading
101- element = { HeadingElement . H4 }
102- weight = { FontWeight . Regular }
103- className = { `${ className } -subscription-form__text` }
104- >
105- Bucket: { currentSubscription && currentSubscription . bucket }
106- </ Heading >
107- ) }
108- </ Grid . Column >
109- </ Grid . Row >
110- </ Grid >
111- )
122+ ) }
123+ </ Grid . Column >
124+ </ Grid . Row >
125+ </ Grid >
126+ )
127+ }
112128
113129export default SubscriptionFormContent
0 commit comments