|
| 1 | +import { Container } from '@ifrc-go/ui'; |
| 2 | +import { |
| 3 | + isDefined, |
| 4 | + isNotDefined, |
| 5 | +} from '@togglecorp/fujs'; |
| 6 | + |
| 7 | +import SparkEmbed from '#components/domain/SparkEmbed'; |
| 8 | +import Page from '#components/Page'; |
| 9 | +import { powerBiReportId1 } from '#config'; |
| 10 | +import { useRequest } from '#utils/restRequest'; |
| 11 | + |
| 12 | +// Backend returns snake_case keys |
| 13 | +type BackendPowerBiAuth = { |
| 14 | + embed_url: string; |
| 15 | + embed_token: string; |
| 16 | + report_id?: string; |
| 17 | + expires_at?: string; // new field for expiry (ISO string) |
| 18 | +}; |
| 19 | + |
| 20 | +/** @knipignore */ |
| 21 | +// eslint-disable-next-line import/prefer-default-export |
| 22 | +export function Component() { |
| 23 | + const { |
| 24 | + response: authRaw, |
| 25 | + pending, |
| 26 | + error, |
| 27 | + } = useRequest({ |
| 28 | + skip: !powerBiReportId1, |
| 29 | + url: '/api/v2/auth-power-bi/', |
| 30 | + preserveResponse: true, |
| 31 | + query: powerBiReportId1 ? ({ |
| 32 | + report_id: powerBiReportId1, |
| 33 | + }) : undefined, |
| 34 | + }); |
| 35 | + |
| 36 | + // FIXME: the typings should be generated in the server |
| 37 | + const auth = authRaw as unknown as BackendPowerBiAuth | undefined; |
| 38 | + const embedUrl = auth?.embed_url; |
| 39 | + const accessToken = auth?.embed_token; |
| 40 | + const reportId = auth?.report_id; |
| 41 | + |
| 42 | + return ( |
| 43 | + <Page |
| 44 | + // FIXME: use strings |
| 45 | + title="SPARK" |
| 46 | + // FIXME: use strings |
| 47 | + heading="SPARK" |
| 48 | + > |
| 49 | + <Container |
| 50 | + pending={pending} |
| 51 | + errored={!!error} |
| 52 | + errorMessage={error?.value.messageForNotification} |
| 53 | + empty={isNotDefined(powerBiReportId1) |
| 54 | + || isNotDefined(embedUrl) |
| 55 | + || isNotDefined(accessToken)} |
| 56 | + // FIXME: use strings |
| 57 | + emptyMessage="Page not available!" |
| 58 | + > |
| 59 | + {isDefined(embedUrl) && isDefined(accessToken) && ( |
| 60 | + <SparkEmbed |
| 61 | + embedUrl={embedUrl} |
| 62 | + accessToken={accessToken} |
| 63 | + reportId={reportId} |
| 64 | + /> |
| 65 | + )} |
| 66 | + </Container> |
| 67 | + </Page> |
| 68 | + ); |
| 69 | +} |
| 70 | + |
| 71 | +Component.displayName = 'Spark'; |
0 commit comments