#31 Using Vuex Getters To Populate The Account Securities And Deposits
Tuesday, January 7, 2020
In this article we will create several different vuex getters that we can use when we access an account that will automatically populate the deposits and securities that are associated with the account.
Parts
- Part 45: Adjusting Shares
- Part 44: Plan Percentages
- Part 43: Home Securities
- Part 42: Updating Plans
- Part 41: Plan Details View
- Part 40: Portfolio Getters
- Part 39: Portfolio Plan
- Part 38: Portfolio Category
- Part 37: Account Securities
- Part 36: Account Transfer
- Part 35: View Account Security
- Part 34: Updating Deposit
- Part 33: View Account Deposit
- Part 32: Display Account Details
- Part 31: Account Getters
- Part 30: Deposits And Securities
- Part 29: Add Accounts Details
- Part 28: Refactoring Accounts
- Part 27: Add Security Models
- Part 26: Edit Security Details
- Part 25: View Security Details
- Part 24: Navigating To Details
- Part 23: Getters Validation
- Part 22: Query Parameters
- Part 21: Tab Entries
- Part 20: Tab Header
- Part 19: List View
- Part 18: Vuex Getters
- Part 17: End Domain Model
- Part 16: Start Domain Model
- Part 15: Pop Routes
- Part 14: Push Routes
- Part 13: Removing Accounts
- Part 12: Vuex (Decorators)
- Part 11: Vuex (Accounts)
- Part 10: The App Bar (Settings)
- Part 9: Remove Consumer Rxjs
- Part 8: The App Bar (Back)
- Part 7: Structuring Our App
- Part 6: Animation Between Views
- Part 5: Navigation Fade
- Part 4: Navigation Requests
- Part 3: Fade Animations (cont.)
- Part 2: Fade Animations
- Part 1: Splash Screen
Code Snippets
store-constants.ts (1:13)
...
export const GETTER_ACCOUNT_DEPOSIT = "GETTER_ACCOUNT_DEPOSIT";
export const GETTER_ACCOUNT_DEPOSITS = "GETTER_ACCOUNT_DEPOSITS";
export const GETTER_ACCOUNT_SECURITIES = "GETTER_ACCOUNT_SECURITIES";
export const GETTER_ACCOUNT_SECURITY = "GETTER_ACCOUNT_SECURITY";
...
account-types.ts (1:42)
import {
...,
GETTER_ACCOUNT_DEPOSIT,
GETTER_ACCOUNT_DEPOSITS,
GETTER_ACCOUNT_SECURITIES,
GETTER_ACCOUNT_SECURITY,
...
} from "@/store/store-constants";
export type GetterAccountDeposit = (id: number) => AccountDepositModel;
export type GetterAccountDeposits = (accountId: number) => AccountDepositModel[];
export type GetterAccountSecurity = (id: number) => AccountSecurityModel;
export type GetterAccountSecurities = (accountId: number) => AccountSecurityModel[];
...
export interface IAccountGetters {
...
[GETTER_ACCOUNT_DEPOSIT]: GetterAccountDeposit;
[GETTER_ACCOUNT_DEPOSITS]: GetterAccountDeposits;
[GETTER_ACCOUNT_SECURITIES]: GetterAccountSecurities;
[GETTER_ACCOUNT_SECURITY]: GetterAccountSecurity;
...
}
store-types.ts (3:41)
...
import { IAccountGetters, ... } from "@/store/account-types";
...
export interface IStoreGetters extends IAccountGetters, ISecurityGetters {}
...
Advertisement
account-module.ts (4:13)
...
import {
...,
GETTER_ACCOUNT_DEPOSIT,
GETTER_ACCOUNT_DEPOSITS,
GETTER_ACCOUNT_SECURITIES,
GETTER_ACCOUNT_SECURITY,
...,
GETTER_SECURITY,
...,
} from "@/store/store-constants";
...
import { IStoreGetters, ... } from "@/store/store-types";
...
export const accountGetters: StoreGetterTree = {
...,
[GETTER_ACCOUNT_DEPOSIT]: (storeState, getters: IAccountGetters) => {
return (id: number) => {
const deposit = findById(storeState[STATE_ACCOUNTS_DEPOSITS], id)!;
storeActionValidator
.begin()
.while(StoreActions.Getting)
.throwIf(deposit)
.isUndefined(undefinedMessage("deposit", id, storeState[STATE_ACCOUNTS_DEPOSITS].index));
deposit.account = getters[GETTER_ACCOUNT](deposit.accountId);
return deposit;
};
},
[GETTER_ACCOUNT_DEPOSITS]: (storeState) => {
return (accountId: number) => {
return storeState[STATE_ACCOUNTS_DEPOSITS].items.filter((x) => x.accountId === accountId);
};
},
[GETTER_ACCOUNT_SECURITIES]: (storeState, getters: IStoreGetters) => {
return (accountId: number) => {
const accountSecurities = storeState[STATE_ACCOUNTS_SECURITIES].items.filter(
(x) => x.accountId === accountId,
);
accountSecurities.forEach((x) => {
x.security = getters[GETTER_SECURITY](x.securityId);
});
return accountSecurities;
};
},
[GETTER_ACCOUNT_SECURITY]: (storeState, getters: IStoreGetters) => {
return (id: number) => {
const accountSecurity = findById(storeState[STATE_ACCOUNTS_SECURITIES], id)!;
storeActionValidator
.begin()
.while(StoreActions.Getting)
.throwIf(accountSecurity)
.isUndefined(undefinedMessage("accountSecurity", id, storeState[STATE_ACCOUNTS_SECURITIES].index));
accountSecurity.account = getters[GETTER_ACCOUNT](accountSecurity.accountId);
accountSecurity.security = getters[GETTER_SECURITY](accountSecurity.securityId);
return accountSecurity;
};
},
...
};
index.ts (13:07)
export * from "@/store/account-deposit-model";
...
export * from "@/store/account-security-model";
...
account-model.ts (13:45)
import { AccountDepositModel, AccountSecurityModel } from "@/store";
...
export class AccountModel {
private _deposits: AccountDepositModel[] = [];
...
private _securities: AccountSecurityModel[] = [];
...
public get deposits() {
return this._deposits;
}
public set deposits(deposits: AccountDepositModel[]) {
this._deposits = deposits;
}
...
public get securities() {
return this._securities;
}
public set securities(securities: AccountSecurityModel[]) {
this._securities = securities;
}
...
}
account-module.ts (15:37)
...
export const accountGetters: StoreGetterTree = {
[GETTER_ACCOUNT]: (state, getters: IAccountGetters) => {
return (id: number) => {
...
account.deposits = getters[GETTER_ACCOUNT_DEPOSITS](account.id);
account.securities = getters[GETTER_ACCOUNT_SECURITIES](account.id);
...
};
},
...
}
...
AccountsDetails.vue (16:40)
<script lang="ts">
...
export default class AccountsDetails extends Vue {
...
private load() {
...
console.log(account);
}
...
}
</script>
Exciton Interactive LLC