{"size": 2052, "ext": "js", "max_stars_repo_path": "src/utils/storageManager.js", "max_stars_repo_name": "emguy/Meet-Up-Event-Planner", "max_stars_repo_stars_event_min_datetime": null, "max_stars_repo_stars_event_max_datetime": null, "max_issues_repo_path": "src/utils/storageManager.js", "max_issues_repo_name": "emguy/Meet-Up-Event-Planner", "max_issues_repo_issues_event_min_datetime": null, "max_issues_repo_issues_event_max_datetime": null, "max_forks_repo_path": "src/utils/storageManager.js", "max_forks_repo_name": "emguy/Meet-Up-Event-Planner", "max_forks_repo_forks_event_min_datetime": null, "max_forks_repo_forks_event_max_datetime": null}
{"max_stars_count": null, "max_issues_count": null, "max_forks_count": null, "avg_line_length": 26.6493506494, "max_line_length": 99, "alphanum_fraction": 0.6871345029}
/**
 * This module bridges the application to the local storage. It provides
 * several convenient APIs to access or set the user data on the local
 * storage.
 */

/* this the key for accessing application data from the local storage */
var key = 'meet_up_event_planner';

/* this is equivalent to Object.assign() in ES6  */
var objectAssign = require('../utils/utils.js').objectAssign;

/* initialize this module  */
var storageManager = {};

/* this is the data for trail user */
var trialUserData = {
  uid: 'trial',
  name: 'Trial User',
  email: '',
  password: '',
  eventList: require('../misc/preloadedEvents.js')
};

/* this is the template for user data */
var templateUserData = {
  uid: '',
  name: '',
  email: '',
  password: '',
  eventList: require('../misc/preloadedEvents.js')
};

/* this is the user data setter */
storageManager.setUserData = function(userData) {
  var data = {};
  var rawData = localStorage.getItem(key); 
  if (rawData) {
    data = JSON.parse(rawData);
  }
  data[userData.uid] = objectAssign({}, templateUserData, userData);
  localStorage.setItem(key, JSON.stringify(data));
};

/* this is for inserting new user; it will reset the 
 * user data if this user is already exist 
 */
storageManager.addNewUser = function(_uid, _password, _email) {
  var data = {};
  var rawData = localStorage.getItem(key); 
  if (rawData) {
    data = JSON.parse(rawData);
  }
  data[_uid] = objectAssign({}, templateUserData, {uid: _uid, password: _password, email: _email});
  localStorage.setItem(key, JSON.stringify(data));
};

/* this is the user data getter */
storageManager.getUserData = function(uid) {
  var rawData = localStorage.getItem(key); 
  if (!rawData) {
    return;
  }
  var data = JSON.parse(rawData);
  return data[uid];
};

/* initalize the application data on the local storage */
storageManager.init = function(preloadedEvents) {
  storageManager.setUserData(trialUserData);
  storageManager.setUserData({uid: 'root', password: '1111'});
};

/* export the module */
module.exports = storageManager;

