1
0
mirror of https://github.com/Unleash/unleash.git synced 2025-04-01 01:18:10 +02:00
unleash.unleash/frontend/src/store/user/index.js
ivaosthu 67bdb16659 Show a dialog if the username cookie is not set.
This is used by unleash server to show who changes what.
By making this a required input allow us to make sure
it is always set.
2016-11-24 21:32:29 +01:00

58 lines
1.5 KiB
JavaScript

import { Map as $Map } from 'immutable';
import { USER_UPDATE_USERNAME, USER_SAVE } 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}=${userName}; expires=Thu, 18 Dec 2099 12:00:00 UTC`;
}
function getInitState () {
const userName = 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);
default:
return state;
}
};
export default settingStore;