2017-12-31 00:34:31 +05:30
|
|
|
// @flow
|
|
|
|
export type Account = {
|
|
|
|
id: number,
|
|
|
|
username: string,
|
|
|
|
email: string,
|
|
|
|
token: string,
|
2018-11-10 14:33:47 +05:30
|
|
|
refreshToken: ?string
|
2017-12-31 00:34:31 +05:30
|
|
|
};
|
|
|
|
|
|
|
|
export type State = {
|
|
|
|
active: ?number,
|
2018-11-10 14:33:47 +05:30
|
|
|
available: Array<Account>
|
2017-12-31 00:34:31 +05:30
|
|
|
};
|
|
|
|
|
2018-11-10 14:33:47 +05:30
|
|
|
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
|
|
|
|
};
|
2017-12-31 00:34:31 +05:30
|
|
|
export type ResetAction = { type: 'accounts:reset' };
|
|
|
|
|
|
|
|
type Action =
|
|
|
|
| AddAction
|
|
|
|
| RemoveAction
|
|
|
|
| ActivateAction
|
|
|
|
| UpdateTokenAction
|
|
|
|
| ResetAction;
|
|
|
|
|
|
|
|
export function getActiveAccount(state: { accounts: State }): ?Account {
|
2018-11-10 14:33:47 +05:30
|
|
|
const accountId = state.accounts.active;
|
2017-12-31 00:34:31 +05:30
|
|
|
|
|
|
|
return state.accounts.available.find((account) => account.id === accountId);
|
|
|
|
}
|
|
|
|
|
2018-11-10 14:33:47 +05:30
|
|
|
export function getAvailableAccounts(state: {
|
|
|
|
accounts: State
|
|
|
|
}): Array<Account> {
|
2018-03-01 03:28:02 +05:30
|
|
|
return state.accounts.available;
|
|
|
|
}
|
|
|
|
|
2016-10-30 17:42:49 +05:30
|
|
|
export default function accounts(
|
2017-12-31 00:34:31 +05:30
|
|
|
state: State = {
|
2016-11-05 15:41:41 +05:30
|
|
|
active: null,
|
|
|
|
available: []
|
|
|
|
},
|
2017-12-31 00:34:31 +05:30
|
|
|
action: Action
|
|
|
|
): State {
|
|
|
|
switch (action.type) {
|
|
|
|
case 'accounts:add': {
|
2018-11-10 14:33:47 +05:30
|
|
|
if (
|
|
|
|
!action.payload
|
|
|
|
|| !action.payload.id
|
|
|
|
|| !action.payload.token
|
|
|
|
) {
|
|
|
|
throw new Error(
|
|
|
|
'Invalid or empty payload passed for accounts.add'
|
|
|
|
);
|
2016-10-30 17:42:49 +05:30
|
|
|
}
|
2017-12-31 00:34:31 +05:30
|
|
|
const { payload } = action;
|
2016-10-30 17:42:49 +05:30
|
|
|
|
2016-11-13 02:01:44 +05:30
|
|
|
state.available = state.available
|
|
|
|
.filter((account) => account.id !== payload.id)
|
|
|
|
.concat(payload);
|
2016-10-30 17:42:49 +05:30
|
|
|
|
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;
|
|
|
|
});
|
|
|
|
|
2016-10-30 17:42:49 +05:30
|
|
|
return state;
|
2017-12-31 00:34:31 +05:30
|
|
|
}
|
2016-10-30 17:42:49 +05:30
|
|
|
|
2017-12-31 00:34:31 +05:30
|
|
|
case 'accounts:activate': {
|
2018-11-10 14:33:47 +05:30
|
|
|
if (
|
|
|
|
!action.payload
|
|
|
|
|| !action.payload.id
|
|
|
|
|| !action.payload.token
|
|
|
|
) {
|
|
|
|
throw new Error(
|
|
|
|
'Invalid or empty payload passed for accounts.add'
|
|
|
|
);
|
2016-10-30 17:42:49 +05:30
|
|
|
}
|
|
|
|
|
2017-12-31 00:34:31 +05:30
|
|
|
const { payload } = action;
|
|
|
|
|
2016-10-30 17:42:49 +05:30
|
|
|
return {
|
2017-12-31 00:34:31 +05:30
|
|
|
available: state.available.map((account) => {
|
|
|
|
if (account.id === payload.id) {
|
2018-11-10 14:33:47 +05:30
|
|
|
return { ...payload };
|
2017-12-31 00:34:31 +05:30
|
|
|
}
|
|
|
|
|
2018-11-10 14:33:47 +05:30
|
|
|
return { ...account };
|
2017-12-31 00:34:31 +05:30
|
|
|
}),
|
|
|
|
active: payload.id
|
2016-10-30 17:42:49 +05:30
|
|
|
};
|
2017-12-31 00:34:31 +05:30
|
|
|
}
|
2016-10-30 17:42:49 +05:30
|
|
|
|
2017-12-31 00:34:31 +05:30
|
|
|
case 'accounts:reset':
|
|
|
|
return {
|
|
|
|
active: null,
|
|
|
|
available: []
|
|
|
|
};
|
2016-11-14 10:58:25 +05:30
|
|
|
|
2017-12-31 00:34:31 +05:30
|
|
|
case 'accounts:remove': {
|
|
|
|
if (!action.payload || !action.payload.id) {
|
2018-11-10 14:33:47 +05:30
|
|
|
throw new Error(
|
|
|
|
'Invalid or empty payload passed for accounts.remove'
|
|
|
|
);
|
2016-10-30 17:42:49 +05:30
|
|
|
}
|
|
|
|
|
2017-12-31 00:34:31 +05:30
|
|
|
const { payload } = action;
|
|
|
|
|
2016-10-30 17:42:49 +05:30
|
|
|
return {
|
|
|
|
...state,
|
2018-11-10 14:33:47 +05:30
|
|
|
available: state.available.filter(
|
|
|
|
(account) => account.id !== payload.id
|
|
|
|
)
|
2016-10-30 17:42:49 +05:30
|
|
|
};
|
2017-12-31 00:34:31 +05:30
|
|
|
}
|
2016-10-30 17:42:49 +05:30
|
|
|
|
2017-12-31 00:34:31 +05:30
|
|
|
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');
|
|
|
|
}
|
|
|
|
|
2017-12-31 00:34:31 +05:30
|
|
|
const { payload } = action;
|
|
|
|
|
2016-10-30 17:42:49 +05:30
|
|
|
return {
|
2016-11-05 15:41:41 +05:30
|
|
|
...state,
|
2017-12-31 00:34:31 +05:30
|
|
|
available: state.available.map((account) => {
|
|
|
|
if (account.id === state.active) {
|
|
|
|
return {
|
|
|
|
...account,
|
2018-11-10 14:33:47 +05:30
|
|
|
token: payload
|
2017-12-31 00:34:31 +05:30
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2018-11-10 14:33:47 +05:30
|
|
|
return { ...account };
|
|
|
|
})
|
2016-10-30 17:42:49 +05:30
|
|
|
};
|
2017-12-31 00:34:31 +05:30
|
|
|
}
|
2016-11-05 15:41:41 +05:30
|
|
|
|
|
|
|
default:
|
2017-12-31 00:34:31 +05:30
|
|
|
(action: empty);
|
2016-11-05 15:41:41 +05:30
|
|
|
return state;
|
2016-10-30 17:42:49 +05:30
|
|
|
}
|
|
|
|
}
|