Skip to content
This repository has been archived by the owner on Mar 19, 2024. It is now read-only.

Commit

Permalink
Merge 1c02853 into 39b0160
Browse files Browse the repository at this point in the history
  • Loading branch information
markysoft committed Feb 23, 2018
2 parents 39b0160 + 1c02853 commit 4230bd0
Show file tree
Hide file tree
Showing 15 changed files with 876 additions and 6 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -60,3 +60,5 @@ typings/
# css files
*.css
*.css.map

app/views/includes/header-items.nunjucks
2 changes: 1 addition & 1 deletion .istanbul.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,5 @@ check:
global:
statements: 80
lines: 80
branches: 37
branches: 55
functions: 80
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
0.2.0 / TBA
=======
- Upgrade python in Alpine to 2.7.14-r0
- Build header items nunjucks

0.1.0 / 2018-02-21
=======
Expand Down
Binary file added app/.DS_Store
Binary file not shown.
22 changes: 22 additions & 0 deletions app/lib/header/buildHeaderItems.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
const cleanHeaderUrl = require('./cleanHeaderUrl');

const seperator = '';

function getAnchor(item) {
return `<a href="${cleanHeaderUrl(item.URL)}">${item.Title}</a>`;
}

function getSubmenu(submenus) {
// eslint-disable-next-line no-use-before-define
return submenus ? `<ul>${buildLinkTags(submenus)}</ul>` : '';
}

function buildLinkTags(menus) {
return menus.map(item => `<li>${getAnchor(item)}${getSubmenu(item.Submenus)}</li>`).join(seperator);
}

function buildHeaderItems(menus) {
return buildLinkTags(menus);
}

module.exports = buildHeaderItems;
17 changes: 17 additions & 0 deletions app/lib/header/cleanHeaderUrl.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
const siteToReplace = 'http://site';
const nhsukDomain = 'https://www.nhs.uk';

function getUrl(item) {
return item ? item.split(',')[0] : '';
}

function replaceSpaces(url) {
// encodeURIComponent can't be used as the source URLs are already partially encoded
return url.replace(/ /g, '%20');
}

function cleanHeaderUrl(item) {
return replaceSpaces(getUrl(item).replace(siteToReplace, nhsukDomain));
}

module.exports = cleanHeaderUrl;
20 changes: 20 additions & 0 deletions app/lib/header/getHeader.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
const request = require('request');

async function getHeaderContent(url) {
return new Promise((resolve, reject) => {
request(url, (error, response, body) => {
if (!error && response.statusCode === 200) {
resolve(body);
} else {
reject(error);
}
});
});
}

async function getHeader(url) {
const body = await getHeaderContent(url);
return JSON.parse(body);
}

module.exports = getHeader;
1 change: 1 addition & 0 deletions config/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,5 @@ module.exports = {
env: process.env.NODE_ENV || 'development',
root: rootPath,
port: process.env.PORT || 3000,
headerApiUrl: 'https://refdata-api.azurewebsites.net/api/fullheadermenu',
};
30 changes: 30 additions & 0 deletions headerItems.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
const fs = require('fs');
const getHeader = require('./app/lib/header/getHeader');
const buildHeaderItems = require('./app/lib/header/buildHeaderItems');
const log = require('./app/lib/logger');
const config = require('./config/config');

const headerFileName = 'app/views/includes/header-items.nunjucks';

function saveFile(output, filename) {
fs.writeFileSync(filename, output, (err) => {
if (err) {
throw err;
}
log.info(`${headerFileName} written.`);
});
}

async function saveHeaderItems() {
try {
log.info(`Building '${headerFileName}'`);
const response = await getHeader(config.headerApiUrl);
const headerItems = buildHeaderItems(response);
saveFile(headerItems, headerFileName);
log.info('Build complete.');
} catch (ex) {
log.error(ex);
}
}

saveHeaderItems();
10 changes: 6 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "sexual-health-service-finder",
"version": "0.1.0",
"version": "0.2.0",
"description": "Helping to connect people to sexual health services.",
"main": "app.js",
"scripts": {
Expand All @@ -12,6 +12,7 @@
"coverage-generate-no-metrics": "yarn istanbul -- cover _mocha -- --exit \"./test/**/!(metrics).js\"",
"coverage-upload-coveralls": "cat ./coverage/lcov.info | coveralls",
"git-hook": "yarn lint && yarn coverage-generate-no-metrics && yarn coverage-check",
"header-build": "node headerItems.js",
"istanbul": "NODE_ENV=test istanbul",
"lint": "yarn lint-backend && yarn lint-frontend",
"lint-backend": "eslint --ext .js,.json .",
Expand All @@ -20,8 +21,8 @@
"postrewrite": "yarn git-hook",
"precommit": "yarn git-hook",
"prepush": "yarn git-hook && yarn snyk test",
"start-watch": "yarn brunch watch & nodemon app.js",
"test": "NODE_ENV=test mocha --exit --recursive test",
"start-watch": "yarn header-build && yarn brunch watch & nodemon app.js",
"test": "yarn header-build && NODE_ENV=test mocha --exit --recursive test",
"test-ci": "yarn ci-hook && yarn coverage-upload-coveralls",
"test-watch": "yarn test -- --watch --reporter min"
},
Expand Down Expand Up @@ -56,6 +57,7 @@
"nhsuk-bunyan-logger": "^1.7.0",
"node-sass": "^4.7.2",
"nunjucks": "3.0.1",
"request": "^2.83.0",
"sass-brunch": "^2.10.4"
},
"devDependencies": {
Expand All @@ -79,4 +81,4 @@
"proxyquire": "^1.7.10",
"snyk": "^1.40.2"
}
}
}
Loading

0 comments on commit 4230bd0

Please sign in to comment.