Skip to content

Commit 7236124

Browse files
Fix MailChimp audience selection (#1214)
* start fix for mailchimp audiences * Fix for mailchimp audience selection * format
1 parent 5303e13 commit 7236124

3 files changed

Lines changed: 55 additions & 57 deletions

File tree

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@gitbook/integration-mailchimp': patch
3+
---
4+
5+
Fix Mailchimp audience selection for accounts with multiple audiences

‎integrations/mailchimp/src/index.tsx‎

Lines changed: 21 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -36,17 +36,17 @@ type MailchimpBlockState = {
3636

3737
type 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

4146
const DEFAULT_CTA = 'Sign up to our mailing list to receive updates!';
4247
const 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
*/
211195
const 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>

‎integrations/mailchimp/src/sdk.ts‎

Lines changed: 29 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,37 @@
11
/**
2-
* Returns all available mailing lists
2+
* Returns all available mailing lists (audiences), fetching all pages.
33
*/
44
export async function getMailingLists(
55
apiEndpoint: string,
66
accessToken: string,
77
): Promise<MailchimpListItem[]> {
8-
const resp = await fetch(`${apiEndpoint}/3.0/lists`, {
9-
headers: {
10-
Authorization: `Bearer ${accessToken}`,
11-
},
12-
});
8+
const allLists: MailchimpListItem[] = [];
9+
const count = 100;
10+
let offset = 0;
11+
12+
while (true) {
13+
const resp = await fetch(`${apiEndpoint}/3.0/lists?count=${count}&offset=${offset}`, {
14+
headers: {
15+
Authorization: `Bearer ${accessToken}`,
16+
},
17+
});
18+
19+
if (!resp.ok) {
20+
const body = await resp.text();
21+
throw new Error(`Mailchimp API error: ${resp.status} ${resp.statusText} - ${body}`);
22+
}
23+
24+
const { lists = [], total_items = 0 } = (await resp.json()) as MailchimpListsResponse;
25+
allLists.push(...lists);
26+
27+
if (allLists.length >= total_items || lists.length === 0) {
28+
break;
29+
}
30+
31+
offset += count;
32+
}
1333

14-
const { lists = [] } = (await resp.json()) as MailchimpListsResponse;
15-
return lists;
34+
return allLists;
1635
}
1736

1837
/**
@@ -52,7 +71,8 @@ export async function subscribeUserToList(
5271
});
5372

5473
if (!resp.ok) {
55-
throw new Error(`Mailchimp API error: ${resp.status} ${resp.statusText}`);
74+
const body = await resp.text();
75+
throw new Error(`Mailchimp API error: ${resp.status} ${resp.statusText} - ${body}`);
5676
}
5777
}
5878

0 commit comments

Comments
 (0)