@@ -11,9 +11,9 @@ import {
1111 displayError ,
1212 displaySuccess ,
1313} from "../util/list/toast-component/display-toast" ;
14- import EventDatabaseApiFeedTypeTemplate from "./feed-source-type-templates/EventDatabaseApiFeedType.template " ;
15- import NotifiedFeedTypeTemplate from "./feed-source- type-templates/NotifiedFeedType.template " ;
16- import CalendarFeedTypeTemplate from "./feed-source- type-templates/CalendarFeedType.template " ;
14+ import EventDatabaseFeedType from "./templates/event-database-feed-type.jsx " ;
15+ import NotifiedFeedType from "./templates/notified- feed-type.jsx " ;
16+ import CalendarFeedType from "./templates/calendar- feed-type.jsx " ;
1717
1818/**
1919 * The theme manager component.
@@ -50,7 +50,7 @@ function FeedSourceManager({
5050
5151 const [ dynamicFormElement , setDynamicFormElement ] = useState ( ) ;
5252 const [ submitting , setSubmitting ] = useState ( false ) ;
53- const [ formStateObject , setFormStateObject ] = useState ( ) ;
53+ const [ formStateObject , setFormStateObject ] = useState ( { } ) ;
5454
5555 const [
5656 postV2FeedSources ,
@@ -67,28 +67,31 @@ function FeedSourceManager({
6767 value : "App\\Feed\\EventDatabaseApiFeedType" ,
6868 title : t ( "dynamic-fields.event-database-api-feed-type.title" ) ,
6969 key : "1" ,
70- secrets : "host" ,
71- template : < EventDatabaseApiFeedTypeTemplate mode = { saveMethod } /> ,
70+ secretsDefault : {
71+ "host" : ""
72+ } ,
7273 } ,
7374 {
7475 value : "App\\Feed\\NotifiedFeedType" ,
7576 title : t ( "dynamic-fields.notified-feed-type.title" ) ,
7677 key : "2" ,
77- secrets : "token" ,
78- template : < NotifiedFeedTypeTemplate mode = { saveMethod } /> ,
78+ secretsDefault : {
79+ "token" : "" ,
80+ } ,
7981 } ,
8082 {
8183 value : "App\\Feed\\CalendarApiFeedType" ,
8284 title : t ( "dynamic-fields.calendar-api-feed-type.title" ) ,
8385 key : "3" ,
84- secrets : "resources" ,
85- template : < CalendarFeedTypeTemplate mode = { saveMethod } /> ,
86+ secretsDefault : {
87+ "resources" : [ ]
88+ } ,
8689 } ,
8790 {
8891 value : "App\\Feed\\RssFeedType" ,
8992 title : t ( "dynamic-fields.rss-feed-type.title" ) ,
90- key : "5 " ,
91- template : null ,
93+ key : "4 " ,
94+ secretsDefault : { } ,
9295 } ,
9396 ] ;
9497
@@ -109,46 +112,28 @@ function FeedSourceManager({
109112 setFormStateObject ( { ...initialState } ) ;
110113 } , [ initialState ] ) ;
111114
112- useEffect ( ( ) => {
113- if ( formStateObject ) {
114- const option = feedSourceTypeOptions . find (
115- ( opt ) => opt . value === formStateObject . feedType
116- ) ;
117- if ( option && option . template ) {
118- setDynamicFormElement (
119- cloneElement ( option . template , {
120- handleInput,
121- formStateObject,
122- } )
123- ) ;
124- } else {
125- setDynamicFormElement ( null ) ;
126- }
115+ const handleSecretInput = ( { target} ) => {
116+ const localFormStateObject = { ...formStateObject } ;
117+ if ( ! localFormStateObject . secrets ) {
118+ localFormStateObject . secrets = { } ;
127119 }
128- } , [ formStateObject ] ) ;
120+ localFormStateObject . secrets [ target . id ] = target . value ;
121+ setFormStateObject ( localFormStateObject ) ;
122+ } ;
129123
130- useEffect ( ( ) => {
131- if ( formStateObject ?. feedType ) {
132- const selectedFeedTypeSecret = feedSourceTypeOptions . find (
133- ( option ) => option . value === formStateObject . feedType
134- ) . secrets ;
135-
136- if ( selectedFeedTypeSecret ) {
137- const secretsArray = selectedFeedTypeSecret
138- . split ( "," )
139- . map ( ( prop ) => prop . trim ( ) ) ;
140-
141- formStateObject . secrets = secretsArray ?. reduce ( ( acc , secret ) => {
142- acc [ secret ] = formStateObject [ secret ] ;
143- return acc ;
144- } , { } ) ;
145- }
146- }
147- } , [ formStateObject , formStateObject ?. feedType ] ) ;
124+ const onFeedTypeChange = ( { target} ) => {
125+ const value = target . value
126+ const option = feedSourceTypeOptions . find ( ( opt ) => opt . value === value ) ;
127+ const newFormStateObject = { ...formStateObject } ;
128+ newFormStateObject . feedType = value ;
129+ newFormStateObject . secrets = { ...option . secretsDefault } ;
130+ setFormStateObject ( newFormStateObject ) ;
131+ }
148132
149133 /** Save feed source. */
150134 function saveFeedSource ( ) {
151135 setLoadingMessage ( t ( "loading-messages.saving-feed-source" ) ) ;
136+
152137 if ( saveMethod === "POST" ) {
153138 postV2FeedSources ( {
154139 feedSourceFeedSourceInput : JSON . stringify ( formStateObject ) ,
@@ -205,6 +190,8 @@ function FeedSourceManager({
205190 handleSubmit = { handleSubmit }
206191 isLoading = { isLoading || submitting }
207192 loadingMessage = { loadingMessage }
193+ onFeedTypeChange = { onFeedTypeChange }
194+ handleSecretInput = { handleSecretInput }
208195 feedSourceTypeOptions = { feedSourceTypeOptions }
209196 dynamicFormElement = { dynamicFormElement }
210197 mode = { saveMethod }
0 commit comments