@ministryofjustice/hmpps-digital-prison-reporting-frontend
Version:
The Digital Prison Reporting Frontend contains templates and code to help display data effectively in UI applications.
83 lines (82 loc) • 3.08 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.getRoutePrefix = exports.getDpdPathSuffix = exports.createQuerystringFromObject = exports.clearFilterValue = void 0;
/* eslint-disable @typescript-eslint/no-explicit-any */
const enum_1 = require("../components/_filters/filter-input/enum");
exports.clearFilterValue = '~clear~';
const createUrlForParameters = (currentQueryParams, updateQueryParams, fields) => {
let queryParams;
if (updateQueryParams) {
queryParams = {
...currentQueryParams,
selectedPage: '1',
};
Object.keys(updateQueryParams).forEach((q) => {
if (updateQueryParams[q]) {
queryParams[q] = updateQueryParams[q];
}
else {
Object.keys(queryParams)
.filter((key) => key === q || key.startsWith(`${q}.`))
.forEach((key) => {
queryParams[key] = exports.clearFilterValue;
});
}
});
}
else {
queryParams = {
selectedPage: '1',
pageSize: currentQueryParams.pageSize,
sortColumn: currentQueryParams.sortColumn,
sortedAsc: currentQueryParams.sortedAsc,
dataProductDefinitionsPath: currentQueryParams.dataProductDefinitionsPath,
};
}
const nonEmptyQueryParams = {};
Object.keys(queryParams)
.filter((key) => queryParams[key])
.forEach((key) => {
nonEmptyQueryParams[key] = queryParams[key];
});
return (0, exports.createQuerystringFromObject)(nonEmptyQueryParams, fields);
};
const createQuerystringFromObject = (source, fields) => {
const querystring = Object.keys(source)
.flatMap((key) => {
const fieldDef = fields === null || fields === void 0 ? void 0 : fields.find((f) => `filters.${f.name}` === key);
const value = source[key];
if (Array.isArray(value)) {
return value.map((v) => `${encodeURI(key)}=${encodeURI(v)}`);
}
if (fieldDef && fieldDef.filter.type.toLowerCase() === enum_1.FilterType.multiselect.toLowerCase()) {
const values = value.split(',');
return values.map((v) => {
return `${encodeURI(key)}=${encodeURI(v)}`;
});
}
return [`${encodeURI(key)}=${encodeURI(value)}`];
})
.join('&');
return `?${querystring}`;
};
exports.createQuerystringFromObject = createQuerystringFromObject;
const getDpdPathSuffix = (dpdsPath) => {
if (dpdsPath && dpdsPath !== '') {
return `?dataProductDefinitionsPath=${dpdsPath}`;
}
return '';
};
exports.getDpdPathSuffix = getDpdPathSuffix;
const getRoutePrefix = (config) => {
let prefix = config === null || config === void 0 ? void 0 : config.routePrefix;
if (!prefix) {
prefix = '/dpr';
}
if (prefix === 'dpr') {
prefix = '';
}
return prefix;
};
exports.getRoutePrefix = getRoutePrefix;
exports.default = createUrlForParameters;