Skip to content

Commit

Permalink
Merge pull request #76 from Financial-Times/remove-formdata-polyfill
Browse files Browse the repository at this point in the history
Refactor to remove formdata polyfill
  • Loading branch information
magsallen committed Aug 23, 2019
2 parents 61dd503 + 7088db2 commit 88c414a
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 10 deletions.
8 changes: 8 additions & 0 deletions demos/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,14 @@ app.get('/', (req, res) => {
});
});

app.get('/simple', (req, res) => {
res.render('demo-simple', {
title: 'Test App',
question: 'How was your visit today?',
type: 'radio-buttons'
});
});

function runPa11yTests () {
const spawn = require('child_process').spawn;
const pa11y = spawn('pa11y-ci');
Expand Down
2 changes: 1 addition & 1 deletion demos/demo-old.html → demos/demo-simple.html
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
<div class="o-grid-container">
<h1>Demo of n-feedback</h1>
<div class="o-grid-row">
{{> template }}
{{> demos/feedback-partial }}
</div>
</div>
<script src="/public/main.js"></script>
Expand Down
30 changes: 22 additions & 8 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,11 @@ const surveyBuilder = require('./src/survey-builder');
const postResponse = require('./src/post-response');
const getAdditionalInfo = require('./src/get-additional-info');
const dictionary = require('./src/dictionary');
require('formdata-polyfill');

function getSurveyData ( surveyId ){
// const surveyDataURL = 'http://local.ft.com:5005/public/survey.json';
// const surveyDataURL = `http://local.ft.com:3002/v1/survey/${surveyId}`;
const surveyDataURL = `https://www.ft.com/__feedback-api/v1/survey/${surveyId}`;
// const surveyDataURL = 'http://local.ft.com:5005/public/survey.json'; // for local development
// const surveyDataURL = `http://local.ft.com:3002/v1/survey/${surveyId}`; // for local development via npm linking
const surveyDataURL = `https://www.ft.com/__feedback-api/v1/survey/${surveyId}`; // production link
return fetch(surveyDataURL, {
headers: {
'Accept': 'application/json',
Expand Down Expand Up @@ -121,12 +120,27 @@ function validate (block){
function generateResponse (overlay){
const context = overlay.content;
const form = document.querySelector('.n-feedback__survey__wrapper-form', context);
const response = {};
(new FormData(form)).forEach((val, key) => {
response[key] = val;
const data = {};

form.querySelectorAll('input,textarea').forEach((element) => {
if (element.type === 'radio') {
if (element.checked) {
data[element.name] = element.value;
}
}

else if (element.type === 'checkbox') {
if (element.checked) {
data[element.name] = (data[element.name] || []).concat(element.value);
}
}

else {
data[element.name] = element.value;
}
});

return response;
return data;
}

function toggleOverlay (overlay){
Expand Down
1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@
"webpack-cli": "^3.1.2"
},
"dependencies": {
"formdata-polyfill": "^3.0.12",
"handlebars": "^4.1.2"
}
}

0 comments on commit 88c414a

Please sign in to comment.