Skip to content

Commit 7e82382

Browse files
committed
Await for callback
1 parent 70f1d8c commit 7e82382

File tree

2 files changed

+38
-22
lines changed

2 files changed

+38
-22
lines changed

apps/meteor/client/views/room/contextualBar/ExportMessages/ExportMessages.tsx

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -111,8 +111,8 @@ const ExportMessages = () => {
111111
// Remove HTML from download options
112112
const downloadOutputOptions = outputOptions.slice(1);
113113

114-
const roomExportMutation = useRoomExportMutation();
115-
const downloadExportMutation = useDownloadExportMutation();
114+
const { mutateAsync: exportRoom } = useRoomExportMutation();
115+
const { mutateAsync: exportAndDownload } = useDownloadExportMutation();
116116

117117
const { selectedMessageStore } = useContext(SelectedMessageContext);
118118
const messageCount = useCountSelected();
@@ -145,32 +145,43 @@ const ExportMessages = () => {
145145

146146
const { mutateAsync: exportAsPDF } = useExportMessagesAsPDFMutation();
147147

148-
const handleExport = async ({ type, toUsers, dateFrom, dateTo, format, subject, additionalEmails }: ExportMessagesFormValues) => {
148+
const handleExport = async ({
149+
type,
150+
toUsers,
151+
dateFrom,
152+
dateTo,
153+
format,
154+
subject,
155+
additionalEmails,
156+
}: ExportMessagesFormValues): Promise<void> => {
149157
const messages = selectedMessageStore.getSelectedMessages();
150158

151159
if (type === 'download') {
152160
if (format === 'pdf') {
153-
return exportAsPDF(messages);
161+
await exportAsPDF(messages);
162+
return;
154163
}
155164

156165
if (format === 'json') {
157-
return downloadExportMutation.mutateAsync({
166+
await exportAndDownload({
158167
mids: messages,
159168
});
169+
return;
160170
}
161171
}
162172

163173
if (type === 'file') {
164-
return roomExportMutation.mutateAsync({
174+
await exportRoom({
165175
rid: room._id,
166176
type: 'file',
167177
...(dateFrom && { dateFrom }),
168178
...(dateTo && { dateTo }),
169179
format: format as 'html' | 'json',
170180
});
181+
return;
171182
}
172183

173-
roomExportMutation.mutateAsync({
184+
await exportRoom({
174185
rid: room._id,
175186
type: 'email',
176187
toUsers,

apps/meteor/client/views/room/contextualBar/ExportMessages/useExportMessagesAsPDFMutation.tsx

Lines changed: 20 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -107,22 +107,27 @@ export const useExportMessagesAsPDFMutation = () => {
107107

108108
const instance = pdf();
109109

110-
const callback = async () => {
111-
const link = document.createElement('a');
112-
link.href = URL.createObjectURL(await instance.toBlob());
113-
link.download = `exportedMessages-${new Date().toISOString()}.pdf`;
114-
document.body.appendChild(link);
115-
link.click();
116-
document.body.removeChild(link);
117-
URL.revokeObjectURL(link.href);
118-
};
110+
await new Promise<void>((resolve, reject) => {
111+
const callback = async () => {
112+
const link = document.createElement('a');
113+
link.href = URL.createObjectURL(await instance.toBlob());
114+
link.download = `exportedMessages-${new Date().toISOString()}.pdf`;
115+
document.body.appendChild(link);
116+
link.click();
117+
document.body.removeChild(link);
118+
URL.revokeObjectURL(link.href);
119+
resolve();
120+
};
119121

120-
try {
121-
instance.on('change', callback);
122-
instance.updateContainer(jsx);
123-
} finally {
124-
instance.removeListener('change', callback);
125-
}
122+
try {
123+
instance.on('change', callback);
124+
instance.updateContainer(jsx);
125+
} catch (error) {
126+
reject(error);
127+
} finally {
128+
instance.removeListener('change', callback);
129+
}
130+
});
126131
},
127132
onError: (error) => {
128133
dispatchToastMessage({ type: 'error', message: error });

0 commit comments

Comments
 (0)