1
0
mirror of https://github.com/Unleash/unleash.git synced 2025-04-10 01:16:39 +02:00
unleash.unleash/frontend/src/store/user/index.js
Ivar f56118ac0f Should also decode cookie value when reading.
This solves encoding issues and closes #39
2017-01-24 11:45:46 +01:00

60 lines
1.6 KiB
JavaScript

import { Map as $Map } from 'immutable';
import { USER_UPDATE_USERNAME, USER_SAVE, USER_EDIT } from './actions';
const COOKIE_NAME = 'username';
// Ref: http://stackoverflow.com/questions/10730362/get-cookie-by-name
function readCookie () {
const nameEQ = `${COOKIE_NAME}=`;
const ca = document.cookie.split(';');
for (let i = 0;i < ca.length;i++) {
let c = ca[i];
while (c.charAt(0) == ' ') { // eslint-disable-line eqeqeq
c = c.substring(1, c.length);
}
if (c.indexOf(nameEQ) === 0) {
return c.substring(nameEQ.length, c.length);
}
}
}
function writeCookie (userName) {
document.cookie = `${COOKIE_NAME}=${encodeURIComponent(userName)}; expires=Thu, 18 Dec 2099 12:00:00 UTC`;
}
function getInitState () {
const userName = decodeURIComponent(readCookie(COOKIE_NAME));
const showDialog = !userName;
return new $Map({ userName, showDialog });
}
function updateUserName (state, action) {
return state.set('userName', action.value);
}
function save (state) {
const userName = state.get('userName');
if (userName) {
writeCookie(userName);
return state.set('showDialog', false);
} else {
return state;
}
}
const settingStore = (state = getInitState(), action) => {
switch (action.type) {
case USER_UPDATE_USERNAME:
return updateUserName(state, action);
case USER_SAVE:
return save(state);
case USER_EDIT:
return state.set('showDialog', true);
default:
return state;
}
};
export default settingStore;