accounts-frontend/packages/app/components/accounts/reducer.ts

117 lines
3.1 KiB
TypeScript
Raw Normal View History

import { Action } from './actions/pure-actions';
export type Account = {
2020-05-24 04:38:24 +05:30
id: number;
username: string;
email: string;
token: string;
refreshToken: string | null;
};
export type State = {
2020-05-24 04:38:24 +05:30
active: number | null;
available: Array<Account>;
};
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;
2020-05-24 04:38:24 +05:30
return state.accounts.available.find((account) => account.id === accountId) || null;
}
2020-05-24 04:38:24 +05:30
export function getAvailableAccounts(state: { accounts: State }): Array<Account> {
return state.accounts.available;
}
export default function accounts(
2020-05-24 04:38:24 +05:30
state: State = {
active: null,
available: [],
},
action: Action,
): 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');
}
2020-05-24 04:38:24 +05:30
const { payload } = action;
2020-05-24 04:38:24 +05:30
state.available = state.available.filter((account) => account.id !== payload.id).concat(payload);
2020-05-24 04:38:24 +05:30
state.available.sort((account1, account2) => {
if (account1.username === account2.username) {
return 0;
}
2020-05-24 04:38:24 +05:30
return account1.username > account2.username ? 1 : -1;
});
2020-05-24 04:38:24 +05:30
return state;
}
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');
}
2020-05-24 04:38:24 +05:30
const { payload } = action;
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,
};
}
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');
}
2020-05-24 04:38:24 +05:30
const { payload } = action;
2020-05-24 04:38:24 +05:30
return {
...state,
available: state.available.filter((account) => account.id !== payload.id),
};
}
2020-05-24 04:38:24 +05:30
case 'accounts:updateToken': {
if (typeof action.payload !== 'string') {
throw new Error('payload must be a jwt token');
}
2020-05-24 04:38:24 +05:30
const { payload } = action;
return {
2020-05-24 04:38:24 +05:30
...state,
available: state.available.map((account) => {
if (account.id === state.active) {
return {
...account,
token: payload,
};
}
return { ...account };
}),
};
2020-05-24 04:38:24 +05:30
}
}
2019-12-07 16:58:52 +05:30
2020-05-24 04:38:24 +05:30
return state;
}