@@ -121,27 +121,55 @@ const ContentForm: React.FC<IContentFormProps> = ({
121121 const [ seriesOptions , setSeriesOptions ] = React . useState < IOptionItem [ ] > ( [ ] ) ;
122122 const [ seriesOtherOptions , setSeriesOtherOptions ] = React . useState < IOptionItem [ ] > ( [ ] ) ;
123123 const [ seriesOtherCreated , setSeriesOtherCreated ] = React . useState < string > ( '' ) ;
124+ const [ contentPrepTime , setContentPrepTime ] = React . useState < string > ( '' ) ;
124125
125126 const contentId = parseInt ( id ?? '0' ) ;
126127 const urlParams = new URLSearchParams ( window . location . search ) ;
127128 const showPostedOn = [ '' , 'true' ] . includes ( urlParams . get ( 'showPostedOn' ) ?? 'false' ) ;
128129
130+ // Function to filter series based on source and other criteria
131+ const filterSeries = React . useCallback (
132+ ( sourceId : number | '' , seriesId : number | '' , isOther : boolean = false ) => {
133+ return series . filter (
134+ ( f ) =>
135+ f . isOther === isOther &&
136+ ( ( ! ! seriesId && f . id === seriesId ) || // Include current selected
137+ ( f . isEnabled && ( ! sourceId || ! f . sourceId || f . sourceId === sourceId ) ) ) ,
138+ ) ;
139+ } ,
140+ [ series ] ,
141+ ) ;
142+
143+ // Function to create series options based on source and other criteria
144+ const filterSeriesOptions = React . useCallback (
145+ ( sourceId : number | '' , seriesId : number | '' ) => {
146+ const options = filterSeries ( sourceId , seriesId , false ) . map (
147+ ( m ) => new OptionItem < number | '' > ( m . name , m . id , ! m . isEnabled ) ,
148+ ) ;
149+ setSeriesOptions ( options ) ;
150+ } ,
151+ [ filterSeries ] ,
152+ ) ;
153+
154+ // Function to create other series options based on source and other criteria
155+ const filterSeriesOtherOptions = React . useCallback (
156+ ( sourceId : number | '' , seriesId : number | '' ) => {
157+ let options = filterSeries ( sourceId , seriesId , true ) . map (
158+ ( m ) => new OptionItem < number | '' > ( m . name , m . id , ! m . isEnabled ) ,
159+ ) ;
160+ if ( seriesOtherCreated ) options = options . concat ( new OptionItem ( seriesOtherCreated , '' ) ) ;
161+ setSeriesOtherOptions ( options ) ;
162+ } ,
163+ [ filterSeries , seriesOtherCreated ] ,
164+ ) ;
165+
129166 React . useEffect ( ( ) => {
130- setSeriesOptions (
131- series . filter ( ( f ) => ! f . isOther ) . map ( ( m : any ) => new OptionItem ( m . name , m . id , ! m . isEnabled ) ) ,
132- ) ;
133- } , [ series ] ) ;
167+ filterSeriesOptions ( form . sourceId , form . seriesId ) ;
168+ } , [ filterSeriesOptions , form . seriesId , form . sourceId ] ) ;
134169
135170 React . useEffect ( ( ) => {
136- // create a list of "Other Series" options
137- // and concat the created value if necessary
138- let filteredSeriesOptions = series
139- . filter ( ( f ) => f . isOther )
140- . map ( ( m : any ) => new OptionItem ( m . name , m . id , ! m . isEnabled ) ) ;
141- if ( seriesOtherCreated )
142- filteredSeriesOptions = filteredSeriesOptions . concat ( new OptionItem ( seriesOtherCreated , '' ) ) ;
143- setSeriesOtherOptions ( filteredSeriesOptions ) ;
144- } , [ series , seriesOtherCreated ] ) ;
171+ filterSeriesOtherOptions ( form . sourceId , form . seriesId ) ;
172+ } , [ filterSeriesOtherOptions , form . sourceId , form . seriesId ] ) ;
145173
146174 React . useEffect ( ( ) => {
147175 if ( contentId > 0 && contentId !== form . id ) {
@@ -152,7 +180,11 @@ const ContentForm: React.FC<IContentFormProps> = ({
152180 React . useEffect ( ( ) => {
153181 setAvStream ( ) ;
154182 } , [ setAvStream ] ) ;
155- const [ contentPrepTime , setContentPrepTime ] = React . useState < string > ( '' ) ;
183+
184+ const onPrepTimeChanged = React . useCallback ( ( value : string ) => {
185+ setContentPrepTime ( value ) ;
186+ } , [ ] ) ;
187+
156188 return (
157189 < styled . ContentForm className = "content-form fvh" ref = { refForm } >
158190 < FormPage className = "fvh" >
@@ -171,10 +203,6 @@ const ContentForm: React.FC<IContentFormProps> = ({
171203 const source = sources . find ( ( s ) => s . id === props . values . sourceId ) ;
172204 const program = series . find ( ( s ) => s . id === props . values . seriesId ) ;
173205
174- const onPrepTimeChanged = ( value : string ) => {
175- setContentPrepTime ( value ) ;
176- } ;
177-
178206 return (
179207 < Col className = "content-col fvh" >
180208 < ContentFormToolBar
@@ -241,6 +269,14 @@ const ContentForm: React.FC<IContentFormProps> = ({
241269 if ( ! ! source ?. mediaTypeId )
242270 props . setFieldValue ( 'mediaTypeId' , source . mediaTypeId ) ;
243271 }
272+ filterSeriesOptions (
273+ newValue ?. value ?? '' ,
274+ props . values . seriesId ,
275+ ) ;
276+ filterSeriesOtherOptions (
277+ newValue ?. value ?? '' ,
278+ props . values . seriesId ,
279+ ) ;
244280 } }
245281 options = { filterEnabledOptions (
246282 sourceOptions ,
@@ -628,10 +664,7 @@ const ContentForm: React.FC<IContentFormProps> = ({
628664 ( s : any ) => s . value === props . values . seriesId ,
629665 ) ?? ''
630666 }
631- options = { filterEnabledOptions (
632- seriesOptions ,
633- props . values . seriesId ,
634- ) }
667+ options = { seriesOptions }
635668 isDisabled = { ! ! props . values . otherSeries }
636669 onChange = { ( e ) => {
637670 props . setFieldValue ( 'otherSeries' , '' ) ;
@@ -646,8 +679,13 @@ const ContentForm: React.FC<IContentFormProps> = ({
646679 options = { seriesOtherOptions }
647680 onChange = { ( e : any ) => {
648681 let foundSeries : ISeriesModel | undefined ;
649- foundSeries = series . find ( ( c ) => c . id === e . value ) ;
650- if ( ! ! foundSeries && foundSeries . isOther ) {
682+ foundSeries = series . find ( ( c ) => c . id === e ?. value ) ;
683+ if ( e ?. value == null || e ?. value === '' ) {
684+ // Clear selected value
685+ props . setFieldValue ( 'otherSeries' , '' ) ;
686+ props . setFieldValue ( 'seriesId' , '' ) ;
687+ setSeriesOtherCreated ( '' ) ;
688+ } else if ( ! ! foundSeries && foundSeries . isOther ) {
651689 // this is a known "Other Series"
652690 props . setFieldValue ( 'otherSeries' , '' ) ;
653691 props . setFieldValue ( 'seriesId' , foundSeries . id ) ;
@@ -657,10 +695,11 @@ const ContentForm: React.FC<IContentFormProps> = ({
657695 onCreateOption = { ( inputValue : string ) => {
658696 // this is a "created" option, but we need to check if
659697 // it's a duplicate of an existing option this is !isOther
698+ const value = inputValue . trim ( ) ;
660699 let foundSeries : ISeriesModel | undefined = series . find (
661700 ( s ) =>
662701 s . name . toLocaleLowerCase ( ) ===
663- inputValue . toLocaleLowerCase ( ) ,
702+ value . toLocaleLowerCase ( ) ,
664703 ) ;
665704 if ( ! ! foundSeries ) {
666705 // this is an existing series - not isOther
@@ -669,10 +708,10 @@ const ContentForm: React.FC<IContentFormProps> = ({
669708 setSeriesOtherCreated ( '' ) ;
670709 } else {
671710 // this is a new "other" series
672- props . setFieldValue ( 'otherSeries' , inputValue ) ;
711+ props . setFieldValue ( 'otherSeries' , value ) ;
673712 props . setFieldValue ( 'seriesId' , undefined ) ;
674713 // save the new created series/program name
675- setSeriesOtherCreated ( inputValue ) ;
714+ setSeriesOtherCreated ( value ) ;
676715 }
677716 } }
678717 value = {
0 commit comments