Generate adoption reports as PDFs
This commit is contained in:
+34
-4
@@ -4691,11 +4691,41 @@ function App() {
|
||||
return;
|
||||
}
|
||||
|
||||
const code = selectedBirdAdoptionTransferCode || (await handleCreateAdoptionTransferCode());
|
||||
if (code) {
|
||||
openAdoptionReport(code, reportWindow, printFriendly);
|
||||
} else {
|
||||
if (!selectedBird) {
|
||||
reportWindow.close();
|
||||
return;
|
||||
}
|
||||
|
||||
setAdoptionReportError('');
|
||||
setCreatingAdoptionReportCode(true);
|
||||
|
||||
try {
|
||||
const response = await apiFetch(
|
||||
`/birds/${selectedBird.id}/reports/adoption${printFriendly ? '?printFriendly=true' : ''}`,
|
||||
authToken,
|
||||
{ method: 'POST' },
|
||||
);
|
||||
|
||||
if (!response.ok) {
|
||||
throw new Error(await readErrorMessage(response, 'Unable to create adoption report.'));
|
||||
}
|
||||
|
||||
const transferCode = response.headers.get('X-FlockPal-Transfer-Code');
|
||||
if (transferCode) {
|
||||
setAdoptionTransferCodes((current) => ({ ...current, [selectedBird.id]: transferCode }));
|
||||
}
|
||||
|
||||
const reportBlob = await response.blob();
|
||||
const reportUrl = URL.createObjectURL(reportBlob);
|
||||
reportWindow.location.href = reportUrl;
|
||||
window.setTimeout(() => URL.revokeObjectURL(reportUrl), 60_000);
|
||||
} catch (reportError) {
|
||||
reportWindow.close();
|
||||
const message = reportError instanceof Error ? reportError.message : 'Unable to create adoption report.';
|
||||
setAdoptionReportError(message);
|
||||
setError(message);
|
||||
} finally {
|
||||
setCreatingAdoptionReportCode(false);
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
Reference in New Issue
Block a user