{"action":"created","comment":{"url":"https://api.github.com/repos/ageuk/companion-app/pulls/comments/69931339","id":69931339,"diff_hunk":"@@ -1,104 +1,70 @@\n // localStorage functions\n-// TODO: implement setting and getting data via localStorage\n /* global Handlebars */\n-var localstorageUtils = { // eslint-disable-line\n-  setPrompts: function (promptsData) {\n-    window.localStorage.setItem('prompts', JSON.stringify(promptsData));\n-  },\n-  getPrompts: function () {\n-    var promptsData = window.localStorage.getItem('prompts');\n-    return promptsData ? JSON.parse(promptsData) : 'No prompts data';\n-  },\n-  updatePrompts: function () {\n-    // TODO: update with new prompts\n-  }\n+var db = {};\n+\n+db.setData = function (key, data) {\n+  window.localStorage[key] = JSON.stringify(data);\n+};\n+\n+db.getData = function (key) {\n+  var data = window.localStorage[key];\n+  return data ? JSON.parse(data) : 'No data found';","path":"public/src/js/main.js","position":23,"original_position":23,"commit_id":"029c1a849307b3b5f9728a78a540f2daaf6020c7","original_commit_id":"029c1a849307b3b5f9728a78a540f2daaf6020c7","user":{"login":"rjmk","id":12041390,"avatar_url":"https://avatars.githubusercontent.com/u/12041390?v=3","gravatar_id":"","url":"https://api.github.com/users/rjmk","html_url":"https://github.com/rjmk","followers_url":"https://api.github.com/users/rjmk/followers","following_url":"https://api.github.com/users/rjmk/following{/other_user}","gists_url":"https://api.github.com/users/rjmk/gists{/gist_id}","starred_url":"https://api.github.com/users/rjmk/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/rjmk/subscriptions","organizations_url":"https://api.github.com/users/rjmk/orgs","repos_url":"https://api.github.com/users/rjmk/repos","events_url":"https://api.github.com/users/rjmk/events{/privacy}","received_events_url":"https://api.github.com/users/rjmk/received_events","type":"User","site_admin":false},"body":"`JSON.parse` is pretty dangerous -- if the data has been overwritten and isn't properly stringified, this will throw an error\r\n\r\nThe only solutions are to use `try`/`catch` (either in each place you use it, or in some helper function), or to use an alternative parsing algorithm (overkill).\r\n\r\nHere, this would look something like\r\n\r\n```js\r\nfunction (key) {\r\n  try {\r\n    return JSON.parse(window.localStorage[key])\r\n  } catch (e) {\r\n    window.localStorage[key] = \"[]\"\r\n    return []\r\n  }\r\n}\r\n```\r\n\r\nNote that I've returned an array here. That's because we're currently expecting an array when we use this function (line 15), and anything but an array (like a string) will cause code explosions. Unfortunately we won't actually be able to guarantee that we're always storing arrays. We need to work out how to deal with that.\r\n\r\nProbably, the programmer will have to keep the type of the object under each key in their head -- that's fairly standard for JS. But we can at least deal with the no data case properly. Here are some options.\r\n\r\n1. This is an easy approach that unfortunately has a lot of overhead at the call sites, and will probably lead to some duplication requiring helper functions\r\n```js\r\nfunction (key, default) {\r\n  try {\r\n    return JSON.parse(window.localStorage[key])\r\n  } catch (e) {\r\n    delete window.localStorage[key] // OR: window.localStorage[key] = default\r\n    return default\r\n  }\r\n}\r\n```\r\n\r\n2. If we make the function do more work, it can reduce overhead at call sites, but will make the app too difficult to reason about, in my opinion\r\n\r\n```js\r\nfunction (key, default) {\r\n  if (default) {\r\n    window.localStorage['__defaults'] = window.localStorage['__defaults'] || {}\r\n    window.localStorage['__defaults'][key] = default\r\n  }\r\n  try {\r\n    return JSON.parse(window.localStorage[key])\r\n  } catch (e) {\r\n    delete window.localStorage[key] // OR: window.localStorage[key] = default\r\n    return window.localStorage['__defaults'][key]\r\n  }\r\n}\r\n```\r\n\r\n3. This approach has a good pedigree, but will probably need some additional work to really be useful\r\n```js\r\nfunction (key) {\r\n  try {\r\n    return Just(JSON.parse(window.localStorage[key]))\r\n  } catch (e) {\r\n    delete window.localStorage[key]\r\n    return Nothing\r\n  }\r\n}\r\n\r\nfunction Just (val) {\r\n  return {\r\n    map: function (f) { return Just(f(val)) }\r\n  }\r\n}\r\n\r\nvar Nothing = {\r\n  map: function () { return Nothing }\r\n}\r\n```\r\n\r\nI tentatively suggest we go with (1)","created_at":"2016-07-07T15:42:56Z","updated_at":"2016-07-07T15:42:56Z","html_url":"https://github.com/ageuk/companion-app/pull/60#discussion_r69931339","pull_request_url":"https://api.github.com/repos/ageuk/companion-app/pulls/60","_links":{"self":{"href":"https://api.github.com/repos/ageuk/companion-app/pulls/comments/69931339"},"html":{"href":"https://github.com/ageuk/companion-app/pull/60#discussion_r69931339"},"pull_request":{"href":"https://api.github.com/repos/ageuk/companion-app/pulls/60"}}},"pull_request":{"url":"https://api.github.com/repos/ageuk/companion-app/pulls/60","id":76568397,"html_url":"https://github.com/ageuk/companion-app/pull/60","diff_url":"https://github.com/ageuk/companion-app/pull/60.diff","patch_url":"https://github.com/ageuk/companion-app/pull/60.patch","issue_url":"https://api.github.com/repos/ageuk/companion-app/issues/60","number":60,"state":"open","locked":false,"title":"Actions setup and localstorage","user":{"login":"andrewMacmurray","id":14013616,"avatar_url":"https://avatars.githubusercontent.com/u/14013616?v=3","gravatar_id":"","url":"https://api.github.com/users/andrewMacmurray","html_url":"https://github.com/andrewMacmurray","followers_url":"https://api.github.com/users/andrewMacmurray/followers","following_url":"https://api.github.com/users/andrewMacmurray/following{/other_user}","gists_url":"https://api.github.com/users/andrewMacmurray/gists{/gist_id}","starred_url":"https://api.github.com/users/andrewMacmurray/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/andrewMacmurray/subscriptions","organizations_url":"https://api.github.com/users/andrewMacmurray/orgs","repos_url":"https://api.github.com/users/andrewMacmurray/repos","events_url":"https://api.github.com/users/andrewMacmurray/events{/privacy}","received_events_url":"https://api.github.com/users/andrewMacmurray/received_events","type":"User","site_admin":false},"body":"+ Set up actions pages\r\n+ Started to move data over into local storage (we're using a dummyData file to populate localStorage and fetching data from there)\r\n+ Refactor prompts / actions routes to be more generic","created_at":"2016-07-07T09:57:10Z","updated_at":"2016-07-07T15:42:56Z","closed_at":null,"merged_at":null,"merge_commit_sha":"f621529902faa278a90653e24d942025c4184d3f","assignee":{"login":"rjmk","id":12041390,"avatar_url":"https://avatars.githubusercontent.com/u/12041390?v=3","gravatar_id":"","url":"https://api.github.com/users/rjmk","html_url":"https://github.com/rjmk","followers_url":"https://api.github.com/users/rjmk/followers","following_url":"https://api.github.com/users/rjmk/following{/other_user}","gists_url":"https://api.github.com/users/rjmk/gists{/gist_id}","starred_url":"https://api.github.com/users/rjmk/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/rjmk/subscriptions","organizations_url":"https://api.github.com/users/rjmk/orgs","repos_url":"https://api.github.com/users/rjmk/repos","events_url":"https://api.github.com/users/rjmk/events{/privacy}","received_events_url":"https://api.github.com/users/rjmk/received_events","type":"User","site_admin":false},"assignees":[{"login":"rjmk","id":12041390,"avatar_url":"https://avatars.githubusercontent.com/u/12041390?v=3","gravatar_id":"","url":"https://api.github.com/users/rjmk","html_url":"https://github.com/rjmk","followers_url":"https://api.github.com/users/rjmk/followers","following_url":"https://api.github.com/users/rjmk/following{/other_user}","gists_url":"https://api.github.com/users/rjmk/gists{/gist_id}","starred_url":"https://api.github.com/users/rjmk/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/rjmk/subscriptions","organizations_url":"https://api.github.com/users/rjmk/orgs","repos_url":"https://api.github.com/users/rjmk/repos","events_url":"https://api.github.com/users/rjmk/events{/privacy}","received_events_url":"https://api.github.com/users/rjmk/received_events","type":"User","site_admin":false}],"milestone":null,"commits_url":"https://api.github.com/repos/ageuk/companion-app/pulls/60/commits","review_comments_url":"https://api.github.com/repos/ageuk/companion-app/pulls/60/comments","review_comment_url":"https://api.github.com/repos/ageuk/companion-app/pulls/comments{/number}","comments_url":"https://api.github.com/repos/ageuk/companion-app/issues/60/comments","statuses_url":"https://api.github.com/repos/ageuk/companion-app/statuses/029c1a849307b3b5f9728a78a540f2daaf6020c7","head":{"label":"ageuk:actions","ref":"actions","sha":"029c1a849307b3b5f9728a78a540f2daaf6020c7","user":{"login":"ageuk","id":20108238,"avatar_url":"https://avatars.githubusercontent.com/u/20108238?v=3","gravatar_id":"","url":"https://api.github.com/users/ageuk","html_url":"https://github.com/ageuk","followers_url":"https://api.github.com/users/ageuk/followers","following_url":"https://api.github.com/users/ageuk/following{/other_user}","gists_url":"https://api.github.com/users/ageuk/gists{/gist_id}","starred_url":"https://api.github.com/users/ageuk/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/ageuk/subscriptions","organizations_url":"https://api.github.com/users/ageuk/orgs","repos_url":"https://api.github.com/users/ageuk/repos","events_url":"https://api.github.com/users/ageuk/events{/privacy}","received_events_url":"https://api.github.com/users/ageuk/received_events","type":"Organization","site_admin":false},"repo":{"id":61796793,"name":"companion-app","full_name":"ageuk/companion-app","owner":{"login":"ageuk","id":20108238,"avatar_url":"https://avatars.githubusercontent.com/u/20108238?v=3","gravatar_id":"","url":"https://api.github.com/users/ageuk","html_url":"https://github.com/ageuk","followers_url":"https://api.github.com/users/ageuk/followers","following_url":"https://api.github.com/users/ageuk/following{/other_user}","gists_url":"https://api.github.com/users/ageuk/gists{/gist_id}","starred_url":"https://api.github.com/users/ageuk/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/ageuk/subscriptions","organizations_url":"https://api.github.com/users/ageuk/orgs","repos_url":"https://api.github.com/users/ageuk/repos","events_url":"https://api.github.com/users/ageuk/events{/privacy}","received_events_url":"https://api.github.com/users/ageuk/received_events","type":"Organization","site_admin":false},"private":false,"html_url":"https://github.com/ageuk/companion-app","description":"An application for guided conversation between a companion and a client.","fork":false,"url":"https://api.github.com/repos/ageuk/companion-app","forks_url":"https://api.github.com/repos/ageuk/companion-app/forks","keys_url":"https://api.github.com/repos/ageuk/companion-app/keys{/key_id}","collaborators_url":"https://api.github.com/repos/ageuk/companion-app/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/ageuk/companion-app/teams","hooks_url":"https://api.github.com/repos/ageuk/companion-app/hooks","issue_events_url":"https://api.github.com/repos/ageuk/companion-app/issues/events{/number}","events_url":"https://api.github.com/repos/ageuk/companion-app/events","assignees_url":"https://api.github.com/repos/ageuk/companion-app/assignees{/user}","branches_url":"https://api.github.com/repos/ageuk/companion-app/branches{/branch}","tags_url":"https://api.github.com/repos/ageuk/companion-app/tags","blobs_url":"https://api.github.com/repos/ageuk/companion-app/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/ageuk/companion-app/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/ageuk/companion-app/git/refs{/sha}","trees_url":"https://api.github.com/repos/ageuk/companion-app/git/trees{/sha}","statuses_url":"https://api.github.com/repos/ageuk/companion-app/statuses/{sha}","languages_url":"https://api.github.com/repos/ageuk/companion-app/languages","stargazers_url":"https://api.github.com/repos/ageuk/companion-app/stargazers","contributors_url":"https://api.github.com/repos/ageuk/companion-app/contributors","subscribers_url":"https://api.github.com/repos/ageuk/companion-app/subscribers","subscription_url":"https://api.github.com/repos/ageuk/companion-app/subscription","commits_url":"https://api.github.com/repos/ageuk/companion-app/commits{/sha}","git_commits_url":"https://api.github.com/repos/ageuk/companion-app/git/commits{/sha}","comments_url":"https://api.github.com/repos/ageuk/companion-app/comments{/number}","issue_comment_url":"https://api.github.com/repos/ageuk/companion-app/issues/comments{/number}","contents_url":"https://api.github.com/repos/ageuk/companion-app/contents/{+path}","compare_url":"https://api.github.com/repos/ageuk/companion-app/compare/{base}...{head}","merges_url":"https://api.github.com/repos/ageuk/companion-app/merges","archive_url":"https://api.github.com/repos/ageuk/companion-app/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/ageuk/companion-app/downloads","issues_url":"https://api.github.com/repos/ageuk/companion-app/issues{/number}","pulls_url":"https://api.github.com/repos/ageuk/companion-app/pulls{/number}","milestones_url":"https://api.github.com/repos/ageuk/companion-app/milestones{/number}","notifications_url":"https://api.github.com/repos/ageuk/companion-app/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/ageuk/companion-app/labels{/name}","releases_url":"https://api.github.com/repos/ageuk/companion-app/releases{/id}","deployments_url":"https://api.github.com/repos/ageuk/companion-app/deployments","created_at":"2016-06-23T10:31:42Z","updated_at":"2016-06-29T16:56:28Z","pushed_at":"2016-07-07T15:11:56Z","git_url":"git://github.com/ageuk/companion-app.git","ssh_url":"git@github.com:ageuk/companion-app.git","clone_url":"https://github.com/ageuk/companion-app.git","svn_url":"https://github.com/ageuk/companion-app","homepage":null,"size":146,"stargazers_count":4,"watchers_count":4,"language":"JavaScript","has_issues":true,"has_downloads":true,"has_wiki":true,"has_pages":false,"forks_count":0,"mirror_url":null,"open_issues_count":47,"forks":0,"open_issues":47,"watchers":4,"default_branch":"master"}},"base":{"label":"ageuk:master","ref":"master","sha":"fd6b854bf4f524b9b622be9ef6b1ba43dcecadad","user":{"login":"ageuk","id":20108238,"avatar_url":"https://avatars.githubusercontent.com/u/20108238?v=3","gravatar_id":"","url":"https://api.github.com/users/ageuk","html_url":"https://github.com/ageuk","followers_url":"https://api.github.com/users/ageuk/followers","following_url":"https://api.github.com/users/ageuk/following{/other_user}","gists_url":"https://api.github.com/users/ageuk/gists{/gist_id}","starred_url":"https://api.github.com/users/ageuk/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/ageuk/subscriptions","organizations_url":"https://api.github.com/users/ageuk/orgs","repos_url":"https://api.github.com/users/ageuk/repos","events_url":"https://api.github.com/users/ageuk/events{/privacy}","received_events_url":"https://api.github.com/users/ageuk/received_events","type":"Organization","site_admin":false},"repo":{"id":61796793,"name":"companion-app","full_name":"ageuk/companion-app","owner":{"login":"ageuk","id":20108238,"avatar_url":"https://avatars.githubusercontent.com/u/20108238?v=3","gravatar_id":"","url":"https://api.github.com/users/ageuk","html_url":"https://github.com/ageuk","followers_url":"https://api.github.com/users/ageuk/followers","following_url":"https://api.github.com/users/ageuk/following{/other_user}","gists_url":"https://api.github.com/users/ageuk/gists{/gist_id}","starred_url":"https://api.github.com/users/ageuk/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/ageuk/subscriptions","organizations_url":"https://api.github.com/users/ageuk/orgs","repos_url":"https://api.github.com/users/ageuk/repos","events_url":"https://api.github.com/users/ageuk/events{/privacy}","received_events_url":"https://api.github.com/users/ageuk/received_events","type":"Organization","site_admin":false},"private":false,"html_url":"https://github.com/ageuk/companion-app","description":"An application for guided conversation between a companion and a client.","fork":false,"url":"https://api.github.com/repos/ageuk/companion-app","forks_url":"https://api.github.com/repos/ageuk/companion-app/forks","keys_url":"https://api.github.com/repos/ageuk/companion-app/keys{/key_id}","collaborators_url":"https://api.github.com/repos/ageuk/companion-app/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/ageuk/companion-app/teams","hooks_url":"https://api.github.com/repos/ageuk/companion-app/hooks","issue_events_url":"https://api.github.com/repos/ageuk/companion-app/issues/events{/number}","events_url":"https://api.github.com/repos/ageuk/companion-app/events","assignees_url":"https://api.github.com/repos/ageuk/companion-app/assignees{/user}","branches_url":"https://api.github.com/repos/ageuk/companion-app/branches{/branch}","tags_url":"https://api.github.com/repos/ageuk/companion-app/tags","blobs_url":"https://api.github.com/repos/ageuk/companion-app/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/ageuk/companion-app/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/ageuk/companion-app/git/refs{/sha}","trees_url":"https://api.github.com/repos/ageuk/companion-app/git/trees{/sha}","statuses_url":"https://api.github.com/repos/ageuk/companion-app/statuses/{sha}","languages_url":"https://api.github.com/repos/ageuk/companion-app/languages","stargazers_url":"https://api.github.com/repos/ageuk/companion-app/stargazers","contributors_url":"https://api.github.com/repos/ageuk/companion-app/contributors","subscribers_url":"https://api.github.com/repos/ageuk/companion-app/subscribers","subscription_url":"https://api.github.com/repos/ageuk/companion-app/subscription","commits_url":"https://api.github.com/repos/ageuk/companion-app/commits{/sha}","git_commits_url":"https://api.github.com/repos/ageuk/companion-app/git/commits{/sha}","comments_url":"https://api.github.com/repos/ageuk/companion-app/comments{/number}","issue_comment_url":"https://api.github.com/repos/ageuk/companion-app/issues/comments{/number}","contents_url":"https://api.github.com/repos/ageuk/companion-app/contents/{+path}","compare_url":"https://api.github.com/repos/ageuk/companion-app/compare/{base}...{head}","merges_url":"https://api.github.com/repos/ageuk/companion-app/merges","archive_url":"https://api.github.com/repos/ageuk/companion-app/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/ageuk/companion-app/downloads","issues_url":"https://api.github.com/repos/ageuk/companion-app/issues{/number}","pulls_url":"https://api.github.com/repos/ageuk/companion-app/pulls{/number}","milestones_url":"https://api.github.com/repos/ageuk/companion-app/milestones{/number}","notifications_url":"https://api.github.com/repos/ageuk/companion-app/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/ageuk/companion-app/labels{/name}","releases_url":"https://api.github.com/repos/ageuk/companion-app/releases{/id}","deployments_url":"https://api.github.com/repos/ageuk/companion-app/deployments","created_at":"2016-06-23T10:31:42Z","updated_at":"2016-06-29T16:56:28Z","pushed_at":"2016-07-07T15:11:56Z","git_url":"git://github.com/ageuk/companion-app.git","ssh_url":"git@github.com:ageuk/companion-app.git","clone_url":"https://github.com/ageuk/companion-app.git","svn_url":"https://github.com/ageuk/companion-app","homepage":null,"size":146,"stargazers_count":4,"watchers_count":4,"language":"JavaScript","has_issues":true,"has_downloads":true,"has_wiki":true,"has_pages":false,"forks_count":0,"mirror_url":null,"open_issues_count":47,"forks":0,"open_issues":47,"watchers":4,"default_branch":"master"}},"_links":{"self":{"href":"https://api.github.com/repos/ageuk/companion-app/pulls/60"},"html":{"href":"https://github.com/ageuk/companion-app/pull/60"},"issue":{"href":"https://api.github.com/repos/ageuk/companion-app/issues/60"},"comments":{"href":"https://api.github.com/repos/ageuk/companion-app/issues/60/comments"},"review_comments":{"href":"https://api.github.com/repos/ageuk/companion-app/pulls/60/comments"},"review_comment":{"href":"https://api.github.com/repos/ageuk/companion-app/pulls/comments{/number}"},"commits":{"href":"https://api.github.com/repos/ageuk/companion-app/pulls/60/commits"},"statuses":{"href":"https://api.github.com/repos/ageuk/companion-app/statuses/029c1a849307b3b5f9728a78a540f2daaf6020c7"}}}}
{"actor":{"display_login":"rjmk"}}