Skip to content

Commit

Permalink
Remove relationships step after adding person
Browse files Browse the repository at this point in the history
  • Loading branch information
bameyrick committed Oct 24, 2018
1 parent 62817a8 commit e5a7e5f
Show file tree
Hide file tree
Showing 105 changed files with 10,022 additions and 1,768 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,4 @@ Gemfile.lock
.ruby-version
.idea
js/compiled/
s/
1 change: 1 addition & 0 deletions .nvmrc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
v8.12.0
131 changes: 131 additions & 0 deletions _prototypes/your-household-v9/assets/household.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,131 @@
import {autoIncrementId} from './utils';

export const HOUSEHOLD_MEMBERS_STORAGE_KEY = 'household-members';
export const USER_HOUSEHOLD_MEMBER_ID = 'person_me';
export const HOUSEHOLD_MEMBER_TYPE = 'household-member';
export const VISITOR_TYPE = 'visitor';

/**
* Types
*/
export function person(opts) {
if (opts.firstName === '' || opts.lastName === '') {
console.log('Unable to create person with data: ',
opts.firstName,
!opts.middleName,
!opts.lastName);
}

let middleName = opts.middleName || '';

return {
fullName: opts.firstName + ' ' + middleName + ' ' + opts.lastName,
firstName: opts.firstName,
middleName,
lastName: opts.lastName
};
}

/**
* Storage
*/
export function getUserAsHouseholdMember() {
return getAllHouseholdMembers().find((member) => {
return member['@person'].id === USER_HOUSEHOLD_MEMBER_ID;
});
}

export function deleteUserAsHouseholdMember() {
deleteHouseholdMember(USER_HOUSEHOLD_MEMBER_ID);
}

export function deleteHouseholdMember(personId) {
let members = getAllHouseholdMembers().filter((member) => {
return member['@person'].id !== personId;
});

sessionStorage.setItem(HOUSEHOLD_MEMBERS_STORAGE_KEY,
JSON.stringify(members));
}

export function updateUserAsHouseholdMember(person, memberData) {
let userAsHouseholdMember = getUserAsHouseholdMember();

userAsHouseholdMember
? updateHouseholdMember(userAsHouseholdMember['@person'], memberData)
: addHouseholdMember(person, memberData, USER_HOUSEHOLD_MEMBER_ID);
}

export function updateHouseholdMember(person, memberData) {
let membersUpdated = getAllHouseholdMembers().map((member) => {
return member['@person'].id === person.id
? {...member, ...memberData, '@person': {...member['@person'], ...person}}
: member;
});

sessionStorage.setItem(HOUSEHOLD_MEMBERS_STORAGE_KEY,
JSON.stringify(membersUpdated));
}

export function addHouseholdMember(person, memberData, id) {
let people = getAllHouseholdMembers() || [];
memberData = memberData || {};

/**
* User is always first in the household list
*/
people[id === USER_HOUSEHOLD_MEMBER_ID ? 'unshift' : 'push']({
...memberData,
type: memberData.type || HOUSEHOLD_MEMBER_TYPE,
'@person': {
...person,
id: id || 'person' + autoIncrementId('household-members')
}
});

sessionStorage.setItem(HOUSEHOLD_MEMBERS_STORAGE_KEY, JSON.stringify(people));
}

export function getAllHouseholdMembers() {
return JSON.parse(sessionStorage.getItem(HOUSEHOLD_MEMBERS_STORAGE_KEY)) || [];
}

export function getHouseholdMemberByPersonId(id) {
return getAllHouseholdMembers().find(function(member) {
return member['@person'].id === id;
});
}

export function getMemberPersonId(member) {
return member['@person'].id;
}

/**
* Comparators
*/
export function isVisitor(member) {
return member.type === window.ONS.storage.KEYS.VISITOR_TYPE;
}

export function isHouseholdMember(member) {
return member.type === window.ONS.storage.KEYS.HOUSEHOLD_MEMBER_TYPE;
}

export function isOtherHouseholdMember(member) {
return member.type === window.ONS.storage.KEYS.HOUSEHOLD_MEMBER_TYPE &&
member['@person'].id !== window.ONS.storage.IDS.USER_HOUSEHOLD_MEMBER_ID;
}

export const tempAwayQuestionSentenceMap = {
'three-more': 'People who usually live outside the UK who are staying in the UK for 3 months or more',
'perm-away': 'People who work away from home within the UK, if this is' +
' their permanent or family home',
'armed-forces': 'Members of the armed forces, if this is their permanent or' +
' family home',
'less-twelve': 'People who are temporarily outside the UK for less than 12' +
' months',
'usually-temp': 'People staying temporarily who usually live in the UK but' +
' do not have another UK address, for example, relatives, friends',
'other': 'Other people who usually live here, including anyone temporarily' +
' away from home'
};
146 changes: 146 additions & 0 deletions _prototypes/your-household-v9/assets/numbers-to-words.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,146 @@
/**
* Copied from:
* https://codereview.stackexchange.com/questions/90349/changing-number-to-words-in-javascript
* ===============
*/
var ONE_TO_NINETEEN = [
'one', 'two', 'three', 'four', 'five',
'six', 'seven', 'eight', 'nine', 'ten',
'eleven', 'twelve', 'thirteen', 'fourteen', 'fifteen',
'sixteen', 'seventeen', 'eighteen', 'nineteen'
];

var TENS = [
'ten', 'twenty', 'thirty', 'forty', 'fifty',
'sixty', 'seventy', 'eighty', 'ninety'
];

var SCALES = ['thousand', 'million', 'billion', 'trillion'];

// helper function for use with Array.filter
function isTruthy(item) {
return !!item;
}

// convert a number into 'chunks' of 0-999
function chunk(number) {
var thousands = [];

while(number > 0) {
thousands.push(number % 1000);
number = Math.floor(number / 1000);
}

return thousands;
}

// translate a number from 1-999 into English
function inEnglish(number) {
var thousands, hundreds, tens, ones, words = [];

if(number < 20) {
return ONE_TO_NINETEEN[number - 1]; // may be undefined
}

if(number < 100) {
ones = number % 10;
tens = number / 10 | 0; // equivalent to Math.floor(number / 10)

words.push(TENS[tens - 1]);
words.push(inEnglish(ones));

return words.filter(isTruthy).join('-');
}

hundreds = number / 100 | 0;
words.push(inEnglish(hundreds));
words.push('hundred');
words.push(inEnglish(number % 100));

return words.filter(isTruthy).join(' ');
}

// append the word for a scale. Made for use with Array.map
function appendScale(chunk, exp) {
var scale;
if (!chunk) {
return null;
}
scale = SCALES[exp - 1];
return [chunk, scale].filter(isTruthy).join(' ');
}

/**
* ===============
* End copy
*/

/**
* Modification - decorator
*/
var NUMBER_TO_POSITION_TEXT_MAP = {
'one': 'first',
'two': 'second',
'three': 'third',
'four': 'fourth',
'five': 'fifth',
'six': 'sixth',
'seven': 'seventh',
'eight': 'eighth',
'nine': 'nineth',
'ten': 'tenth',
'eleven': 'eleventh',
'twelve': 'twelveth',
'thirteen': 'thirteenth',
'fourteen': 'fourteenth',
'fifteen': 'fifteenth',
'sixteen': 'sixteenth',
'seventeen': 'seventeenth',
'eighteen': 'eighteenth',
'nineteen': 'nineteenth',

'twenty': 'twentieth',
'thirty': 'thirtieth',
'forty': 'fortieth',
'fifty': 'fiftieth',
'sixty': 'sixtieth',
'seventy': 'seventieth',
'eighty': 'eightieth',
'ninety': 'ninetieth',
'hundred': 'hundredth',

'thousand': 'thousandth',
'million': 'millionth',
'billion': 'billionth',
'trillion': 'trillionth'
};

export function numberToPositionWord(num) {
const str = chunk(num)
.map(inEnglish)
.map(appendScale)
.filter(isTruthy)
.reverse()
.join(' ');

const sub = str.split(' '),
lastWordDashSplitArr = sub[sub.length - 1].split('-'),
lastWord = lastWordDashSplitArr[lastWordDashSplitArr.length - 1],

newLastWord = (lastWordDashSplitArr.length > 1? lastWordDashSplitArr[0] + '-' : '') +
NUMBER_TO_POSITION_TEXT_MAP[lastWord];

/*console.log('str:', str);
console.log('sub:', sub);
console.log('lastWordDashSplitArr:', lastWordDashSplitArr);
console.log('lastWord:', lastWord);
console.log('newLastWord:', newLastWord);*/

const subCopy = [].concat(sub);
subCopy.pop();
const prefix = subCopy.join(' ');
const result = (prefix ? prefix + ' ' : '') + newLastWord;

// console.log('result', (prefix ? prefix + ' ' : '') + newLastWord);
return result;
}
Loading

0 comments on commit e5a7e5f

Please sign in to comment.