UNPKG

@ministryofjustice/hmpps-digital-prison-reporting-frontend

Version:

The Digital Prison Reporting Frontend contains templates and code to help display data effectively in UI applications.

106 lines (103 loc) 3.18 kB
import { buildRequestAction } from '../../../my-reports/my-reports-list-item/my-reports-list-item-actions/utils.js'; import { actionTemplates } from './actionsTemplate.js'; const getActions = ({ refresh, print, share, copy, download }) => { const actions = []; if (refresh && refresh.url && refresh.executionId) { actions.push(setRefreshAction(actionTemplates.refresh, refresh)); } if (print) { actions.push(setPrintAction(actionTemplates.printable, print)); } if (share) { actions.push(setShareAction(actionTemplates.sharable, share)); } if (copy) { actions.push(setCopyAction(actionTemplates.copy, copy)); } if (download) { actions.push(setDownloadAction(actionTemplates.downloadable, download)); } return actions; }; const setRefreshAction = (template, data) => { const { url: href } = data; return { ...template, href, }; }; const setShareAction = (template, data) => { const { reportName, name, url } = data; const href = `mailto:?subject=${reportName}-${name}&body=${encodeURIComponent(url)}`; return { ...template, href, }; }; const setCopyAction = (template, data) => { const { url: href } = data; return { ...template, href, }; }; const setDownloadAction = (template, data) => { const { canDownload, enabled } = data; const { text, ariaLabelText } = template; const ariaLabel = canDownload ? ariaLabelText : 'Enable download'; return { ...template, text: canDownload ? text : 'Enable download', disabled: !enabled, attributes: { ...data, }, ariaLabelText: !enabled ? `${ariaLabel}, disabled` : ariaLabel, }; }; const setPrintAction = (template, data) => { const { enabled } = data; const { ariaLabelText } = template; return { ...template, disabled: !enabled, href: '#', ariaLabelText: !enabled ? `${ariaLabelText}, disabled` : ariaLabelText, }; }; const setActions = (res, req, definitionData, downloadConfig, requestData) => { const { reportName, name, printable } = definitionData; const requestAction = buildRequestAction(res, req, requestData); let shareConfig; let copyConfig; if (requestAction) { shareConfig = { reportName, name, url: requestAction.href, }; copyConfig = { url: requestAction.href, }; } let refreshConfig; if (requestData?.executionId && requestAction) { refreshConfig = { url: requestAction.href, executionId: requestData.executionId, }; } return getActions({ ...(downloadConfig && { download: downloadConfig }), ...(shareConfig && { share: shareConfig }), ...(refreshConfig && { refresh: refreshConfig }), ...(copyConfig && { copy: copyConfig }), print: { enabled: printable }, }); }; var ReportActionsUtils = { getActions, setActions, }; export { ReportActionsUtils as default, getActions }; //# sourceMappingURL=utils.js.map