@@ -36,17 +36,17 @@ type MailchimpBlockState = {
3636
3737type MailchimpAction =
3838 | { action : 'subscribe' ; email : string }
39- | { action : '@ui.modal.close' ; listIndex : string | undefined ; cta : string ; listIds : string [ ] } ;
39+ | {
40+ action : '@ui.modal.close' ;
41+ listId : string | undefined ;
42+ defaultListId : string | undefined ;
43+ cta : string ;
44+ } ;
4045
4146const DEFAULT_CTA = 'Sign up to our mailing list to receive updates!' ;
4247const DEFAULT_DISCLAIMER =
4348 'By clicking Subscribe, you agree to the processing of your email address in accordance with our privacy policy.' ;
4449
45- function parsePositiveInteger ( value : string ) : number | null {
46- const num = parseInt ( value , 10 ) ;
47- return num >= 0 && ! isNaN ( num ) ? num : null ;
48- }
49-
5050/**
5151 * A Block to subscribe to a Mailchimp mailing list.
5252 */
@@ -107,28 +107,12 @@ const mailchimpSubscribe = createComponent<
107107 }
108108 }
109109 case '@ui.modal.close' : {
110- if ( ! ( 'listIndex' in action ) || ! ( 'listIds' in action ) || ! ( ' cta' in action ) ) {
110+ if ( ! ( 'cta' in action ) ) {
111111 throw new ExposableError ( 'Invalid @ui.modal.close action payload' ) ;
112112 }
113- const listIndex = ( ( ) => {
114- if ( ! action . listIndex ) {
115- return null ;
116- }
117- const listIndex = parsePositiveInteger ( action . listIndex ) ;
118- if ( listIndex === null ) {
119- throw new ExposableError (
120- `Invalid @ui.modal.close listIndex value: ${ action . listIndex } ` ,
121- ) ;
122- }
123- return listIndex ;
124- } ) ( ) ;
125- const listId =
126- listIndex !== null && listIndex < action . listIds . length
127- ? action . listIds [ listIndex ]
128- : null ;
129113 return {
130114 props : {
131- listId : listId ?? element . props . listId ,
115+ listId : action . listId ?? action . defaultListId ?? element . props . listId ,
132116 cta : action . cta ,
133117 } ,
134118 } ;
@@ -210,17 +194,15 @@ async function resolveMailingListId(
210194 */
211195const settingsModal = createComponent <
212196 { listId : string | undefined ; cta : string } ,
213- { cta : string ; listIndex : string | undefined } ,
197+ { cta : string ; listId : string | undefined } ,
214198 { } ,
215199 MailchimpRuntimeContext
216200> ( {
217201 componentId : 'settingsModal' ,
218202
219203 initialState : ( props ) => ( {
220204 cta : props . cta ,
221-
222- // Store an index instead of an id so we can pre-select something
223- listIndex : undefined ,
205+ listId : props . listId ,
224206 } ) ,
225207
226208 async render ( element , context ) {
@@ -237,12 +219,9 @@ const settingsModal = createComponent<
237219
238220 const { listId } = element . props ;
239221 const lists = await getMailingLists ( configuration . api_endpoint , accessToken ) ;
240- const initialListIndex = listId
241- ? ( ( ) => {
242- const index = lists . findIndex ( ( list ) => list . id === element . props . listId ) ;
243- return index === - 1 ? undefined : index ;
244- } ) ( )
245- : undefined ;
222+ const defaultListId = lists [ 0 ] ?. id ;
223+ const selectedListId =
224+ listId && lists . some ( ( list ) => list . id === listId ) ? listId : defaultListId ;
246225
247226 return (
248227 < modal
@@ -253,8 +232,8 @@ const settingsModal = createComponent<
253232 label = "Save"
254233 onPress = { {
255234 action : '@ui.modal.close' ,
256- listIds : lists . map ( ( list ) => list . id ) ,
257- listIndex : element . dynamicState ( 'listIndex' ) ,
235+ listId : element . dynamicState ( 'listId' ) ,
236+ defaultListId ,
258237 cta : element . dynamicState ( 'cta' ) ,
259238 } }
260239 />
@@ -269,18 +248,12 @@ const settingsModal = createComponent<
269248 < text > Audience</ text >
270249 { lists . length > 0 ? (
271250 < select
272- state = "listIndex"
273- initialValue = {
274- initialListIndex !== undefined
275- ? String ( initialListIndex )
276- : undefined
277- }
278- options = { lists . map ( ( list , index ) => {
279- return {
280- label : list . name ,
281- id : String ( index ) ,
282- } ;
283- } ) }
251+ state = "listId"
252+ initialValue = { selectedListId }
253+ options = { lists . map ( ( list ) => ( {
254+ label : list . name ,
255+ id : list . id ,
256+ } ) ) }
284257 />
285258 ) : (
286259 < text >
0 commit comments