mattermost-mobile/src/reducers/entities/users.js
enahum 9125d0cac2 Initial Redux Store (#84)
* Moved SelectServer view state into store

* Moved view state for Login to stores

* Renamed RequestStatus values

* Merged duplicated view state for login

* initial redux store

* Fix eslint

* user store and force logout when necessary

* channel store

* address feedback

* Revert naming the property of data
2016-12-06 15:11:40 -05:00

216 lines
5.6 KiB
JavaScript

// Copyright (c) 2016 Mattermost, Inc. All Rights Reserved.
// See License.txt for license information.
import {combineReducers} from 'redux';
import {UsersTypes} from 'constants';
import {initialPagingState, profilesToSet, addProfileToSet, removeProfileFromSet} from './helpers';
function currentId(state = '', action) {
switch (action.type) {
case UsersTypes.RECEIVED_ME:
return action.data.id;
case UsersTypes.LOGOUT_SUCCESS:
return '';
}
return state;
}
function myPreferences(state = {}, action) {
const nextState = {...state};
switch (action.type) {
case UsersTypes.RECEIVED_PREFERENCES: {
const preferences = action.data;
for (const p of preferences) {
nextState[`${p.category}--${p.name}`] = p.value;
}
return nextState;
}
case UsersTypes.RECEIVED_PREFERENCE: {
const p = action.data;
nextState[`${p.category}--${p.name}`] = p.value;
return nextState;
}
case UsersTypes.LOGOUT_SUCCESS:
return {};
default:
return state;
}
}
function mySessions(state = [], action) {
switch (action.type) {
case UsersTypes.RECEIVED_SESSIONS:
return [...action.data];
case UsersTypes.RECEIVED_REVOKED_SESSION: {
let index = -1;
const length = state.length;
for (let i = 0; i < length; i++) {
if (state[i].id === action.data.id) {
index = i;
break;
}
}
if (index > -1) {
return state.slice(0, index).concat(state.slice(index + 1));
}
return state;
}
case UsersTypes.LOGOUT_SUCCESS:
return [];
default:
return state;
}
}
function myAudits(state = [], action) {
switch (action.type) {
case UsersTypes.RECEIVED_AUDITS:
return [...action.data];
case UsersTypes.LOGOUT_SUCCESS:
return [];
default:
return state;
}
}
function profiles(state = {items: {}, offset: 0, count: 0}, action) {
const nextState = {...state};
switch (action.type) {
case UsersTypes.RECEIVED_ME: {
const id = action.data.id;
nextState.items = {
...nextState.items,
[id]: {
...nextState.items[id],
...action.data
}
};
return nextState;
}
case UsersTypes.RECEIVED_PROFILES:
if (action.offset != null && action.count != null) {
nextState.offset = action.offset + action.count;
nextState.count += action.count;
}
nextState.items = Object.assign({}, nextState.items, action.data);
return nextState;
case UsersTypes.LOGOUT_SUCCESS:
return {items: {}, offset: 0, count: 0};
default:
return state;
}
}
function profilesInTeam(state = initialPagingState(), action) {
switch (action.type) {
case UsersTypes.RECEIVED_PROFILES_IN_TEAM:
return profilesToSet(state, action);
case UsersTypes.LOGOUT_SUCCESS:
return initialPagingState();
default:
return state;
}
}
function profilesInChannel(state = initialPagingState(), action) {
switch (action.type) {
case UsersTypes.RECEIVED_PROFILE_IN_CHANNEL:
return addProfileToSet(state, action.user_id);
case UsersTypes.RECEIVED_PROFILES_IN_CHANNEL:
return profilesToSet(state, action);
case UsersTypes.RECEIVED_PROFILE_NOT_IN_CHANNEL:
return removeProfileFromSet(state, action.user_id);
case UsersTypes.LOGOUT_SUCCESS:
return initialPagingState();
default:
return state;
}
}
function profilesNotInChannel(state = initialPagingState(), action) {
switch (action.type) {
case UsersTypes.RECEIVED_PROFILE_NOT_IN_CHANNEL:
return addProfileToSet(state, action.user_id);
case UsersTypes.RECEIVED_PROFILES_NOT_IN_CHANNEL:
return profilesToSet(state, action);
case UsersTypes.RECEIVED_PROFILE_IN_CHANNEL:
return removeProfileFromSet(state, action.user_id);
case UsersTypes.LOGOUT_SUCCESS:
return initialPagingState();
default:
return state;
}
}
function statuses(state = {}, action) {
switch (action.type) {
case UsersTypes.RECEIVED_STATUSES: {
const nextState = {...state};
return Object.assign({}, nextState, action.data);
}
case UsersTypes.LOGOUT_SUCCESS:
return {};
default:
return state;
}
}
export default combineReducers({
// the current selected user
currentId,
// object where the key is the category-name and has the corresponding value
myPreferences,
// array with the user's sessions
mySessions,
// array with the user's audits
myAudits,
// object containing items, count and offset where items is an object where every key is a user id
// and has an object with the users details
profiles,
// object containing items, count and offset where items is an object where every key is a user id
// and has a Set with the users id that are members of the team
profilesInTeam,
// object containing items, count and offset where items is an object where every key is a user id
// and has a Set with the users id that are members of the channel
profilesInChannel,
// object containing items, count and offset where items is an object where every key is a user id
// and has a Set with the users id that are members of the channel
profilesNotInChannel,
// object where every key is the user id and has a value with the current status of each user
statuses
});