2018-09-09 07:29:23 +02:00
|
|
|
import { State, Action, StateContext } from '@ngxs/store';
|
|
|
|
import { TokenData } from '../services/models/mastodon.interfaces';
|
|
|
|
|
|
|
|
export class AddAccount {
|
|
|
|
static readonly type = '[Accounts] Add account';
|
|
|
|
constructor(public account: AccountInfo) {}
|
|
|
|
}
|
|
|
|
|
2018-09-21 06:42:48 +02:00
|
|
|
export class SelectAccount {
|
|
|
|
static readonly type = '[Accounts] Select account';
|
|
|
|
constructor(public account: AccountInfo, public multiselection: boolean = false) {}
|
|
|
|
}
|
|
|
|
|
2019-01-31 07:01:48 +01:00
|
|
|
export class RemoveAccount {
|
|
|
|
static readonly type = '[Accounts] Remove account';
|
|
|
|
constructor(public accountId: string) {}
|
|
|
|
}
|
|
|
|
|
2018-09-09 07:29:23 +02:00
|
|
|
export interface AccountsStateModel {
|
|
|
|
accounts: AccountInfo[];
|
|
|
|
}
|
|
|
|
|
|
|
|
@State<AccountsStateModel>({
|
|
|
|
name: 'registeredaccounts',
|
|
|
|
defaults: {
|
|
|
|
accounts: []
|
|
|
|
}
|
|
|
|
})
|
|
|
|
export class AccountsState {
|
|
|
|
@Action(AddAccount)
|
2018-09-10 04:33:41 +02:00
|
|
|
AddAccount(ctx: StateContext<AccountsStateModel>, action: AddAccount) {
|
2018-09-26 01:29:49 +02:00
|
|
|
const newAcc = action.account;
|
|
|
|
newAcc.id = `${newAcc.username}@${newAcc.instance}`;
|
|
|
|
|
2018-09-09 07:29:23 +02:00
|
|
|
const state = ctx.getState();
|
|
|
|
ctx.patchState({
|
2018-09-26 01:29:49 +02:00
|
|
|
accounts: [...state.accounts, newAcc]
|
2018-09-09 07:29:23 +02:00
|
|
|
});
|
|
|
|
}
|
2018-09-21 06:42:48 +02:00
|
|
|
|
|
|
|
@Action(SelectAccount)
|
|
|
|
SelectAccount(ctx: StateContext<AccountsStateModel>, action: SelectAccount){
|
|
|
|
const state = ctx.getState();
|
|
|
|
const multiSelection = action.multiselection;
|
|
|
|
const selectedAccount = action.account;
|
2018-09-26 01:29:49 +02:00
|
|
|
const copyAccounts = [...state.accounts];
|
2018-09-21 06:42:48 +02:00
|
|
|
if(!multiSelection) {
|
2018-09-26 01:29:49 +02:00
|
|
|
copyAccounts
|
|
|
|
.filter(x => x.id !== selectedAccount.id)
|
|
|
|
.forEach(x => x.isSelected = false);
|
2018-09-21 06:42:48 +02:00
|
|
|
}
|
2018-09-26 01:29:49 +02:00
|
|
|
const acc = copyAccounts.find(x => x.id === selectedAccount.id);
|
2018-09-21 06:42:48 +02:00
|
|
|
acc.isSelected = !acc.isSelected;
|
|
|
|
|
|
|
|
ctx.patchState({
|
2018-09-26 01:29:49 +02:00
|
|
|
accounts: copyAccounts
|
2018-09-21 06:42:48 +02:00
|
|
|
});
|
|
|
|
}
|
2019-01-31 07:01:48 +01:00
|
|
|
|
|
|
|
@Action(RemoveAccount)
|
|
|
|
RemoveAccount(ctx: StateContext<AccountsStateModel>, action: RemoveAccount){
|
|
|
|
const state = ctx.getState();
|
|
|
|
const filteredAccounts = state.accounts.filter(x => x.id !== action.accountId);
|
|
|
|
ctx.patchState({
|
|
|
|
accounts: filteredAccounts
|
|
|
|
});
|
|
|
|
}
|
2018-09-09 07:29:23 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
export class AccountInfo {
|
2018-09-26 01:29:49 +02:00
|
|
|
id: string;
|
|
|
|
order: number;
|
2018-09-09 07:29:23 +02:00
|
|
|
username: string;
|
|
|
|
instance: string;
|
|
|
|
token: TokenData;
|
2018-09-26 01:29:49 +02:00
|
|
|
isSelected: boolean;
|
2018-09-09 07:29:23 +02:00
|
|
|
}
|