accounts-frontend/src/components/accounts/reducer.js

131 lines
3.7 KiB
JavaScript
Raw Normal View History

// @flow
export type Account = {
id: number,
username: string,
email: string,
token: string,
refreshToken: ?string,
};
export type State = {
active: ?number,
available: Array<Account>,
};
export type AddAction = { type: 'accounts:add', payload: Account};
export type RemoveAction = { type: 'accounts:remove', payload: Account};
export type ActivateAction = { type: 'accounts:activate', payload: Account};
export type UpdateTokenAction = { type: 'accounts:updateToken', payload: string };
export type ResetAction = { type: 'accounts:reset' };
type Action =
| AddAction
| RemoveAction
| ActivateAction
| UpdateTokenAction
| ResetAction;
export function getActiveAccount(state: { accounts: State }): ?Account {
const activeAccount = state.accounts.active;
// TODO: remove activeAccount.id, when will be sure, that magor part of users have migrated to new state structure
const accountId: number | void = typeof activeAccount === 'number' ? activeAccount : (activeAccount || {}).id;
return state.accounts.available.find((account) => account.id === accountId);
}
export default function accounts(
state: State = {
2016-11-05 15:41:41 +05:30
active: null,
available: []
},
action: Action
): State {
switch (action.type) {
case 'accounts:add': {
if (!action.payload || !action.payload.id || !action.payload.token) {
throw new Error('Invalid or empty payload passed for accounts.add');
}
const { payload } = action;
state.available = state.available
.filter((account) => account.id !== payload.id)
.concat(payload);
2016-11-13 17:46:21 +05:30
state.available.sort((account1, account2) => {
if (account1.username === account2.username) {
return 0;
}
return account1.username > account2.username ? 1 : -1;
});
return state;
}
case 'accounts:activate': {
if (!action.payload || !action.payload.id || !action.payload.token) {
throw new Error('Invalid or empty payload passed for accounts.add');
}
const { payload } = action;
return {
available: state.available.map((account) => {
if (account.id === payload.id) {
return {...payload};
}
return {...account};
}),
active: payload.id
};
}
case 'accounts:reset':
return {
active: null,
available: []
};
2016-11-14 10:58:25 +05:30
case 'accounts:remove': {
if (!action.payload || !action.payload.id) {
throw new Error('Invalid or empty payload passed for accounts.remove');
}
const { payload } = action;
return {
...state,
available: state.available.filter((account) => account.id !== payload.id)
};
}
case 'accounts:updateToken': {
if (typeof action.payload !== 'string') {
2016-11-05 15:41:41 +05:30
throw new Error('payload must be a jwt token');
}
const { payload } = action;
return {
2016-11-05 15:41:41 +05:30
...state,
available: state.available.map((account) => {
if (account.id === state.active) {
return {
...account,
token: payload,
};
}
return {...account};
}),
};
}
2016-11-05 15:41:41 +05:30
default:
(action: empty);
2016-11-05 15:41:41 +05:30
return state;
}
}