-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
* Server endpoint * Export functionality * Working export * Loading spinner * Fix unintended change * Rearrange files * Fix missing import * Remove unused imports * PR fixes * Pass down active tileset * Make time period carry over * Don't re-zoom to entity on load * Remove tooltip on loading state change * Font sizes * Pass locale through to export * Explicitly pass date range to export * Disable export button when user is not logged in * Fix error message * Show error mesage * Project logo in export * Zoom in pdf * Fix date wrapper * Hide export button when not logged in * Fix text width --------- Co-authored-by: Andrew <vanbeekandrew@gmail.com>
- Loading branch information
Showing
37 changed files
with
741 additions
and
163 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
77 changes: 77 additions & 0 deletions
77
packages/tupaia-web-server/src/routes/ExportMapOverlayRoute.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
/* | ||
* Tupaia | ||
* Copyright (c) 2017 - 2024 Beyond Essential Systems Pty Ltd | ||
* | ||
*/ | ||
|
||
import { Request } from 'express'; | ||
import { Route } from '@tupaia/server-boilerplate'; | ||
import { TupaiaWebExportMapOverlayRequest } from '@tupaia/types'; | ||
import { stringifyQuery } from '@tupaia/utils'; | ||
import { downloadPageAsPDF } from '@tupaia/server-utils'; | ||
|
||
export type ExportMapOverlayRequest = Request< | ||
TupaiaWebExportMapOverlayRequest.Params, | ||
TupaiaWebExportMapOverlayRequest.ResBody, | ||
TupaiaWebExportMapOverlayRequest.ReqBody, | ||
TupaiaWebExportMapOverlayRequest.ReqQuery | ||
>; | ||
|
||
const downloadMapOverlayAsPdf = ( | ||
projectCode: string, | ||
entityCode: string, | ||
mapOverlayCode: string, | ||
baseUrl: TupaiaWebExportMapOverlayRequest.ReqBody['baseUrl'], | ||
cookie: string, | ||
cookieDomain: TupaiaWebExportMapOverlayRequest.ReqBody['cookieDomain'], | ||
zoom: TupaiaWebExportMapOverlayRequest.ReqBody['zoom'], | ||
center: TupaiaWebExportMapOverlayRequest.ReqBody['center'], | ||
tileset: TupaiaWebExportMapOverlayRequest.ReqBody['tileset'], | ||
hiddenValues: TupaiaWebExportMapOverlayRequest.ReqBody['hiddenValues'], | ||
mapOverlayPeriod?: TupaiaWebExportMapOverlayRequest.ReqBody['mapOverlayPeriod'], | ||
locale?: TupaiaWebExportMapOverlayRequest.ReqBody['locale'], | ||
) => { | ||
const endpoint = `${projectCode}/${entityCode}/map-overlay-pdf-export`; | ||
const pdfPageUrl = stringifyQuery(baseUrl, endpoint, { | ||
zoom, | ||
center, | ||
tileset, | ||
hiddenValues, | ||
overlay: mapOverlayCode, | ||
overlayPeriod: mapOverlayPeriod, | ||
locale, | ||
}); | ||
|
||
return downloadPageAsPDF(pdfPageUrl, cookie, cookieDomain, true); | ||
}; | ||
|
||
export class ExportMapOverlayRoute extends Route<ExportMapOverlayRequest> { | ||
protected type = 'download' as const; | ||
|
||
public async buildResponse() { | ||
const { projectCode, entityCode, mapOverlayCode } = this.req.params; | ||
const { baseUrl, cookieDomain, zoom, center, tileset, hiddenValues, mapOverlayPeriod, locale } = | ||
this.req.body; | ||
const { cookie } = this.req.headers; | ||
|
||
if (!cookie) { | ||
throw new Error(`Must have a valid session to export a map overlay`); | ||
} | ||
|
||
const buffer = await downloadMapOverlayAsPdf( | ||
projectCode, | ||
entityCode, | ||
mapOverlayCode, | ||
baseUrl, | ||
cookie, | ||
cookieDomain, | ||
zoom, | ||
center, | ||
tileset, | ||
hiddenValues, | ||
mapOverlayPeriod, | ||
locale, | ||
); | ||
return { contents: buffer, type: 'application/pdf' }; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
60 changes: 60 additions & 0 deletions
60
packages/tupaia-web/src/api/mutations/useExportMapOverlay.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
/* | ||
* Tupaia | ||
* Copyright (c) 2017 - 2024 Beyond Essential Systems Pty Ltd | ||
*/ | ||
import { useMutation } from 'react-query'; | ||
import { LatLng } from 'leaflet'; | ||
import { MapOverlay } from '@tupaia/types'; | ||
import { LegendProps } from '@tupaia/ui-map-components'; | ||
import { EntityCode, ProjectCode } from '../../types'; | ||
import { API_URL, post } from '../api'; | ||
import { downloadPDF } from '../../utils'; | ||
|
||
type ExportDashboardBody = { | ||
projectCode?: ProjectCode; | ||
entityCode?: EntityCode; | ||
mapOverlayCode?: MapOverlay['code']; | ||
zoom: number; | ||
center: LatLng; | ||
hiddenValues: LegendProps['hiddenValues']; | ||
tileset: string; | ||
mapOverlayPeriod?: string; | ||
}; | ||
|
||
// Requests a map overlay PDF export from the server, and returns the response | ||
export const useExportMapOverlay = (fileName: string) => { | ||
return useMutation<any, Error, ExportDashboardBody, unknown>( | ||
({ | ||
projectCode, | ||
entityCode, | ||
mapOverlayCode, | ||
zoom, | ||
center, | ||
hiddenValues, | ||
tileset, | ||
mapOverlayPeriod, | ||
}: ExportDashboardBody) => { | ||
// Auth cookies are saved against this domain. Pass this to server, so that when it pretends to be us, it can do the same. | ||
const cookieDomain = new URL(API_URL).hostname; | ||
|
||
return post(`mapOverlays/${projectCode}/${entityCode}/${mapOverlayCode}/export`, { | ||
responseType: 'blob', | ||
data: { | ||
cookieDomain, | ||
baseUrl: window.location.origin, | ||
zoom, | ||
center: JSON.stringify(center), | ||
hiddenValues: JSON.stringify(hiddenValues), | ||
tileset, | ||
mapOverlayPeriod, | ||
locale: window.navigator?.language || 'en-AU', | ||
}, | ||
}); | ||
}, | ||
{ | ||
onSuccess: data => { | ||
downloadPDF(data, fileName); | ||
}, | ||
}, | ||
); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.