Sengi-Windows-MacOS-Linux/src/app/states/accounts.state.ts

116 lines
3.3 KiB
TypeScript
Raw Normal View History

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) {}
}
2023-08-20 07:33:07 +02:00
export class ReorderAccounts {
static readonly type = '[Accounts] Reorder';
constructor(public accounts: AccountInfo[]) {}
}
export class SelectAccount {
static readonly type = '[Accounts] Select account';
constructor(public account: AccountInfo, public multiselection: boolean = false) {}
}
export class UpdateAccount {
static readonly type = '[Accounts] Update account';
constructor(public account: AccountInfo) {}
}
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)
AddAccount(ctx: StateContext<AccountsStateModel>, action: AddAccount) {
2019-02-11 03:59:00 +01:00
const state = ctx.getState();
const newAcc = action.account;
newAcc.id = `${newAcc.username}@${newAcc.instance}`;
2019-02-11 03:59:00 +01:00
if(state.accounts.filter(x => x.isSelected).length === 0)
newAcc.isSelected = true;
2018-09-09 07:29:23 +02:00
ctx.patchState({
accounts: [...state.accounts, newAcc]
2018-09-09 07:29:23 +02:00
});
}
2023-08-20 07:33:07 +02:00
@Action(ReorderAccounts)
ReorderAccounts(ctx: StateContext<AccountsStateModel>, action: ReorderAccounts){
// const state = ctx.getState();
const reorderedAccounts = action.accounts;
ctx.patchState({
accounts: [...reorderedAccounts]
});
}
@Action(UpdateAccount)
UpdateAccount(ctx: StateContext<AccountsStateModel>, action: UpdateAccount){
const state = ctx.getState();
let editedAcc = state.accounts.find(x => x.id === action.account.id);
editedAcc.token = action.account.token;
ctx.patchState({
accounts: [...state.accounts]
});
}
@Action(SelectAccount)
SelectAccount(ctx: StateContext<AccountsStateModel>, action: SelectAccount){
const state = ctx.getState();
2019-02-11 03:59:00 +01:00
2019-06-15 05:48:07 +02:00
const selectedAccount = action.account;
2019-02-11 03:59:00 +01:00
const oldSelectedAccount = state.accounts.find(x => x.isSelected);
2019-06-15 05:48:07 +02:00
if(oldSelectedAccount != null && selectedAccount.id === oldSelectedAccount.id) return;
2019-02-11 03:59:00 +01:00
const acc = state.accounts.find(x => x.id === selectedAccount.id);
acc.isSelected = true;
2019-06-15 05:48:07 +02:00
if(oldSelectedAccount != null) oldSelectedAccount.isSelected = false;
ctx.patchState({
2019-02-11 03:59:00 +01:00
accounts: [...state.accounts]
});
}
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);
2019-02-11 03:59:00 +01:00
if(filteredAccounts.length === 1)
filteredAccounts[0].isSelected = true;
2019-01-31 07:01:48 +01:00
ctx.patchState({
accounts: filteredAccounts
});
}
2018-09-09 07:29:23 +02:00
}
export class AccountInfo {
id: string;
order: number;
2018-09-09 07:29:23 +02:00
username: string;
instance: string;
token: TokenData;
isSelected: boolean;
2018-09-09 07:29:23 +02:00
}