From 876ae19d9efac7675ae60bf75d792e308e4d6ad2 Mon Sep 17 00:00:00 2001 From: Filipa Lacerda Date: Mon, 2 Sep 2019 17:43:42 +0100 Subject: Creates utils for the job log With the new job log json format we need a parser on the frontend --- app/assets/javascripts/jobs/store/utils.js | 40 ++++++++++++++++++++++++++++++ 1 file changed, 40 insertions(+) create mode 100644 app/assets/javascripts/jobs/store/utils.js (limited to 'app/assets/javascripts') diff --git a/app/assets/javascripts/jobs/store/utils.js b/app/assets/javascripts/jobs/store/utils.js new file mode 100644 index 00000000000..de7de92ed2e --- /dev/null +++ b/app/assets/javascripts/jobs/store/utils.js @@ -0,0 +1,40 @@ +/** + * Parses the job log content into a structure usable by the template + * + * For collaspible lines (section_header = true): + * - creates a new array to hold the lines that are collpasible, + * - adds a isClosed property to handle toggle + * - adds a isHeader property to handle template logic + * For each line: + * - adds the index as lineNumber + * + * @param {Array} lines + * @returns {Array} + */ +export default (lines = []) => + lines.reduce((acc, line, index) => { + if (line.section_header) { + acc.push({ + isClosed: true, + isHeader: true, + line: { + ...line, + lineNumber: index, + }, + + lines: [], + }); + } else if (acc.length && acc[acc.length - 1].isHeader) { + acc[acc.length - 1].lines.push({ + ...line, + lineNumber: index, + }); + } else { + acc.push({ + ...line, + lineNumber: index, + }); + } + + return acc; + }, []); -- cgit v1.2.1