@ministryofjustice/hmpps-digital-prison-reporting-frontend
Version:
The Digital Prison Reporting Frontend contains templates and code to help display data effectively in UI applications.
144 lines (141 loc) • 4.46 kB
JavaScript
import { RequestStatus } from '../../../types/UserReports.js';
import { ListType } from '../types.js';
import { todayAsUiDateTime, apiTimestampToUiDateTime } from '../../../utils/dateHelper.js';
import { getRouteLocals } from '../../../utils/localsHelper.js';
import { getReportTitleData } from '../../../utils/reportStoreHelper.js';
import { buildActionsCell } from './my-reports-list-item-actions/utils.js';
/**
* Builds the row
*
* @param {StoredReportData} data
* @param {RequestStatus} status
* @param {Request} req
* @param {Response} res
* @param {ListType} listType
* @return {*}
*/
const buildMyReportListRow = (data, status, req, res, listType) => {
if (listType !== ListType.SUBSCRIPTIONS) {
return {
title: buildTitleCell(data),
filters: buildFiltersCell(data),
status,
actions: buildActionsCell(data, res, req, listType),
meta: buildMeta(data, res, listType),
};
}
return {
title: buildTitleCell(data),
description: buildDescriptionCell(data),
schedule: buildScheduleCell(data),
actions: buildActionsCell(data, res, req, listType),
};
};
/**
* Builds the titls cell data
*
* @param {StoredReportData} data
* @return {*} {DprMyReportTitle}
*/
const buildTitleCell = (data) => {
return {
...getReportTitleData(data),
reportType: data.type,
timestamp: buildTimestamp(data),
};
};
const buildDescriptionCell = (data) => {
const { description } = data;
return description;
};
const buildScheduleCell = (data) => {
const { schedule } = data;
return schedule;
};
/**
* Builds the timestamp to display
*
* @param {StoredReportData} data
* @return {*}
*/
const buildTimestamp = (data) => {
const { status, timestamp } = data;
const now = todayAsUiDateTime();
switch (status) {
case RequestStatus.FAILED: {
const { failed } = timestamp;
const ts = failed ? apiTimestampToUiDateTime(failed) : now;
return `Failed at ${ts}`;
}
case RequestStatus.ABORTED: {
const { aborted } = timestamp;
const ts = aborted ? apiTimestampToUiDateTime(aborted) : now;
return `Aborted at ${ts}`;
}
case RequestStatus.FINISHED: {
const { completed } = timestamp;
const ts = completed ? apiTimestampToUiDateTime(completed) : now;
return `Ready at ${ts}`;
}
case RequestStatus.EXPIRED: {
const { expired } = timestamp;
const ts = expired ? apiTimestampToUiDateTime(expired) : now;
return `Expired at ${ts}`;
}
case RequestStatus.READY: {
const { lastViewed, refresh } = timestamp;
if (refresh) {
const ts = refresh ? apiTimestampToUiDateTime(refresh) : now;
return `Refreshed at ${ts}`;
}
const ts = lastViewed ? apiTimestampToUiDateTime(lastViewed) : now;
return `Last viewed at ${ts}`;
}
case RequestStatus.SUBMITTED:
case RequestStatus.STARTED:
case RequestStatus.PICKED: {
const { requested } = timestamp;
const ts = requested ? apiTimestampToUiDateTime(requested) : now;
return `Requested at ${ts}`;
}
default: {
const { lastViewed } = timestamp;
const ts = lastViewed ? apiTimestampToUiDateTime(lastViewed) : now;
return `Last viewed at ${ts}`;
}
}
};
/**
* Builds the filters cell data
*
* @param {StoredReportData} data
* @return {*} {DprMyReportFilters}
*/
const buildFiltersCell = (data) => {
const prerequest = data.query?.summary || [];
const interactive = data.interactiveQuery?.summary || [];
return {
...(prerequest && { prerequest }),
...(interactive && { interactive }),
};
};
/**
* Builds the meta data needed by the polling class
*
* @param {StoredReportData} data
* @param {Response} res
* @return {*}
*/
const buildMeta = (data, res, listType) => {
const { userReportsList } = getRouteLocals(res);
return {
id: data.id,
reportId: data.reportId,
tableId: data.tableId,
executionId: data.executionId,
path: userReportsList,
listType,
};
};
export { buildMyReportListRow };
//# sourceMappingURL=utils.js.map