Skip to content

Commit 50d6d10

Browse files
gnpriceWesleyAC
authored andcommitted
action sheets [nfc]: Factor out makeButtonCallback.
This deduplicates a bit of code, notably the try/catch and the alert on error.
1 parent 96ef22d commit 50d6d10

File tree

1 file changed

+28
-32
lines changed

1 file changed

+28
-32
lines changed

src/message/messageActionSheet.js

Lines changed: 28 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -321,6 +321,22 @@ export const constructNonHeaderActionButtons = ({
321321
}
322322
};
323323

324+
function makeButtonCallback<Args: HeaderArgs | MessageArgs>(
325+
buttonList: Button<Args>[],
326+
args: Args,
327+
) {
328+
return buttonIndex => {
329+
(async () => {
330+
const pressedButton: Button<Args> = buttonList[buttonIndex];
331+
try {
332+
await pressedButton(args);
333+
} catch (err) {
334+
Alert.alert(args._(pressedButton.errorMessage), err.message);
335+
}
336+
})();
337+
};
338+
}
339+
324340
export const showMessageActionSheet = ({
325341
showActionSheetWithOptions,
326342
callbacks,
@@ -345,27 +361,17 @@ export const showMessageActionSheet = ({
345361
narrow: Narrow,
346362
|}): void => {
347363
const buttonList = constructNonHeaderActionButtons({ backgroundData, message, narrow });
348-
const callback = buttonIndex => {
349-
(async () => {
350-
const pressedButton: Button<MessageArgs> = buttonList[buttonIndex];
351-
try {
352-
await pressedButton({
353-
...backgroundData,
354-
...callbacks,
355-
message,
356-
narrow,
357-
});
358-
} catch (err) {
359-
Alert.alert(callbacks._(pressedButton.errorMessage), err.message);
360-
}
361-
})();
362-
};
363364
showActionSheetWithOptions(
364365
{
365366
options: buttonList.map(button => callbacks._(button.title)),
366367
cancelButtonIndex: buttonList.length - 1,
367368
},
368-
callback,
369+
makeButtonCallback(buttonList, {
370+
...backgroundData,
371+
...callbacks,
372+
message,
373+
narrow,
374+
}),
369375
);
370376
};
371377

@@ -397,27 +403,17 @@ export const showHeaderActionSheet = ({
397403
stream,
398404
topic,
399405
});
400-
const callback = buttonIndex => {
401-
(async () => {
402-
const pressedButton: Button<HeaderArgs> = buttonList[buttonIndex];
403-
try {
404-
await pressedButton({
405-
...backgroundData,
406-
...callbacks,
407-
stream,
408-
topic,
409-
});
410-
} catch (err) {
411-
Alert.alert(callbacks._(pressedButton.errorMessage), err.message);
412-
}
413-
})();
414-
};
415406
showActionSheetWithOptions(
416407
{
417408
title: `#${stream} > ${topic}`,
418409
options: buttonList.map(button => callbacks._(button.title)),
419410
cancelButtonIndex: buttonList.length - 1,
420411
},
421-
callback,
412+
makeButtonCallback(buttonList, {
413+
...backgroundData,
414+
...callbacks,
415+
stream,
416+
topic,
417+
}),
422418
);
423419
};

0 commit comments

Comments
 (0)