2020-01-18 02:07:52 +05:30
|
|
|
import { Action } from './actions/pure-actions';
|
|
|
|
|
2017-12-31 00:34:31 +05:30
|
|
|
export type Account = {
|
2020-05-24 04:38:24 +05:30
|
|
|
id: number;
|
|
|
|
username: string;
|
|
|
|
email: string;
|
|
|
|
token: string;
|
|
|
|
refreshToken: string | null;
|
2020-10-27 04:16:57 +05:30
|
|
|
isDeleted: boolean;
|
2017-12-31 00:34:31 +05:30
|
|
|
};
|
|
|
|
|
|
|
|
export type State = {
|
2020-05-24 04:38:24 +05:30
|
|
|
active: number | null;
|
|
|
|
available: Array<Account>;
|
2018-11-10 14:33:47 +05:30
|
|
|
};
|
2017-12-31 00:34:31 +05:30
|
|
|
|
2019-12-07 16:58:52 +05:30
|
|
|
export function getActiveAccount(state: { accounts: State }): Account | null {
|
2020-05-24 04:38:24 +05:30
|
|
|
const accountId = state.accounts.active;
|
2017-12-31 00:34:31 +05:30
|
|
|
|
2020-05-24 04:38:24 +05:30
|
|
|
return state.accounts.available.find((account) => account.id === accountId) || null;
|
2017-12-31 00:34:31 +05:30
|
|
|
}
|
|
|
|
|
2020-05-24 04:38:24 +05:30
|
|
|
export function getAvailableAccounts(state: { accounts: State }): Array<Account> {
|
|
|
|
return state.accounts.available;
|
2018-03-01 03:28:02 +05:30
|
|
|
}
|
|
|
|
|
2020-10-27 04:16:57 +05:30
|
|
|
/**
|
|
|
|
* Move deleted accounts to the end of the accounts list.
|
|
|
|
*/
|
|
|
|
export function getSortedAccounts(state: { accounts: State }): ReadonlyArray<Account> {
|
|
|
|
return state.accounts.available.sort((acc1, acc2) => {
|
|
|
|
if (acc1.isDeleted && !acc2.isDeleted) {
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!acc1.isDeleted && acc2.isDeleted) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2016-10-30 17:42:49 +05:30
|
|
|
export default function accounts(
|
2020-05-24 04:38:24 +05:30
|
|
|
state: State = {
|
|
|
|
active: null,
|
|
|
|
available: [],
|
|
|
|
},
|
|
|
|
action: Action,
|
2017-12-31 00:34:31 +05:30
|
|
|
): State {
|
2020-05-24 04:38:24 +05:30
|
|
|
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');
|
|
|
|
}
|
2016-10-30 17:42:49 +05:30
|
|
|
|
2020-05-24 04:38:24 +05:30
|
|
|
const { payload } = action;
|
2016-10-30 17:42:49 +05:30
|
|
|
|
2020-05-24 04:38:24 +05:30
|
|
|
state.available = state.available.filter((account) => account.id !== payload.id).concat(payload);
|
2017-12-31 00:34:31 +05:30
|
|
|
|
2020-05-24 04:38:24 +05:30
|
|
|
state.available.sort((account1, account2) => {
|
|
|
|
if (account1.username === account2.username) {
|
|
|
|
return 0;
|
|
|
|
}
|
2016-10-30 17:42:49 +05:30
|
|
|
|
2020-05-24 04:38:24 +05:30
|
|
|
return account1.username > account2.username ? 1 : -1;
|
|
|
|
});
|
2016-10-30 17:42:49 +05:30
|
|
|
|
2020-05-24 04:38:24 +05:30
|
|
|
return state;
|
|
|
|
}
|
2017-12-31 00:34:31 +05:30
|
|
|
|
2020-05-24 04:38:24 +05:30
|
|
|
case 'accounts:activate': {
|
|
|
|
if (!action.payload || !action.payload.id || !action.payload.token) {
|
|
|
|
throw new Error('Invalid or empty payload passed for accounts.add');
|
|
|
|
}
|
2019-11-27 14:33:32 +05:30
|
|
|
|
2020-05-24 04:38:24 +05:30
|
|
|
const { payload } = action;
|
2016-10-30 17:42:49 +05:30
|
|
|
|
2020-05-24 04:38:24 +05:30
|
|
|
return {
|
|
|
|
available: state.available.map((account) => {
|
|
|
|
if (account.id === payload.id) {
|
|
|
|
return { ...payload };
|
|
|
|
}
|
|
|
|
|
|
|
|
return { ...account };
|
|
|
|
}),
|
|
|
|
active: payload.id,
|
|
|
|
};
|
|
|
|
}
|
2019-11-27 14:33:32 +05:30
|
|
|
|
2020-05-24 04:38:24 +05:30
|
|
|
case 'accounts:reset':
|
|
|
|
return {
|
|
|
|
active: null,
|
|
|
|
available: [],
|
|
|
|
};
|
2016-11-05 15:41:41 +05:30
|
|
|
|
2020-05-24 04:38:24 +05:30
|
|
|
case 'accounts:remove': {
|
|
|
|
if (!action.payload || !action.payload.id) {
|
|
|
|
throw new Error('Invalid or empty payload passed for accounts.remove');
|
|
|
|
}
|
2019-11-27 14:33:32 +05:30
|
|
|
|
2020-05-24 04:38:24 +05:30
|
|
|
const { payload } = action;
|
2019-11-27 14:33:32 +05:30
|
|
|
|
2020-05-24 04:38:24 +05:30
|
|
|
return {
|
|
|
|
...state,
|
|
|
|
available: state.available.filter((account) => account.id !== payload.id),
|
|
|
|
};
|
|
|
|
}
|
2017-12-31 00:34:31 +05:30
|
|
|
|
2020-05-24 04:38:24 +05:30
|
|
|
case 'accounts:updateToken': {
|
2020-10-27 04:16:57 +05:30
|
|
|
return partiallyUpdateActiveAccount(state, {
|
|
|
|
token: action.payload,
|
|
|
|
});
|
|
|
|
}
|
2020-05-24 04:38:24 +05:30
|
|
|
|
2020-10-27 04:16:57 +05:30
|
|
|
case 'accounts:markAsDeleted': {
|
|
|
|
return partiallyUpdateActiveAccount(state, {
|
|
|
|
isDeleted: action.payload,
|
|
|
|
});
|
2020-05-24 04:38:24 +05:30
|
|
|
}
|
2016-10-30 17:42:49 +05:30
|
|
|
}
|
2019-12-07 16:58:52 +05:30
|
|
|
|
2020-05-24 04:38:24 +05:30
|
|
|
return state;
|
2016-10-30 17:42:49 +05:30
|
|
|
}
|
2020-10-27 04:16:57 +05:30
|
|
|
|
|
|
|
function partiallyUpdateActiveAccount(state: State, payload: Partial<Account>): State {
|
|
|
|
return {
|
|
|
|
...state,
|
|
|
|
available: state.available.map((account) => {
|
|
|
|
if (account.id === state.active) {
|
|
|
|
return {
|
|
|
|
...account,
|
|
|
|
...payload,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
return { ...account };
|
|
|
|
}),
|
|
|
|
};
|
|
|
|
}
|