#37 Creating And Updating Account Securities In The Vuex Store
Sunday, February 16, 2020
In this article we will implement the ability to add, update and transfer account securities from one account to another. Along the way we will need to change the way we get the account securities to account for wanting them to be sorted alphabetically by the symbol of the security. We will also need to handle a few complications related to transferring an account security from one account to another.
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:31)
...
export const ACTION_ADD_ACCOUNT_SECURITY = "ACTION_ADD_ACCOUNT_SECURITY";
...
export const ACTION_UPDATE_ACCOUNT_SECURITY = "ACTION_UPDATE_ACCOUNT_SECURITY";
...
export const MUTATION_ADD_ACCOUNT_SECURITY = "MUTATION_ADD_ACCOUNT_SECURITY";
...
export const MUTATION_UPDATE_ACCOUNT_SECURITY = "MUTATION_UPDATE_ACCOUNT_SECURITY";
...
account-types.ts (2:36)
...
export type PayloadAddAccountSecurity = AccountSecurityModel;
...
export type PayloadUpdateAccountSecurity = AccountSecurityModel;
...
functions.ts (3:26)
...
export function add<T extends IStateItem>(
...
func?: (item: T) => string | number,
...
) {
...
const items = [...state.items, item];
if (typeof func !== "undefined") {
state.items = sort(items, func, options);
} else {
state.items = items;
}
...
}
...
account-module.ts (5:04)
...
import {
...
ACTION_ADD_ACCOUNT_SECURITY,
...
ACTION_UPDATE_ACCOUNT_SECURITY,
...
MUTATION_ADD_ACCOUNT_SECURITY,
...
MUTATION_UPDATE_ACCOUNT_SECURITY,
...
} from "@/store/store-constants";
...
import {
...
PayloadAddAccountSecurity,
...
PayloadUpdateAccountSecurity,
} from "@/store/account-types";
...
export const accountActions: StoreActionTree = {
...
[ACTION_ADD_ACCOUNT_SECURITY](this, { commit }, payload: PayloadAddAccountSecurity) {
commit(MUTATION_ADD_ACCOUNT_SECURITY, payload);
},
...
[ACTION_UPDATE_ACCOUNT_SECURITY](this, { commit }, payload: PayloadUpdateAccountSecurity) {
commit(MUTATION_UPDATE_ACCOUNT_SECURITY, payload);
},
};
...
export const accountMutations: StoreMutationTree = {
...
[MUTATION_ADD_ACCOUNT_SECURITY](storeState, payload: PayloadAddAccountSecurity) {
add(storeState[STATE_ACCOUNTS_SECURITIES], payload);
},
...
[MUTATION_UPDATE_ACCOUNT_SECURITY](storeState, payload: PayloadUpdateAccountSecurity) {
const state = storeState[STATE_ACCOUNTS_SECURITIES];
const security = findById(state, payload.id)!;
storeActionValidator
.begin()
.while(StoreActions.Updating)
.throwIf(security)
.isUndefined(undefinedMessage("security", payload.id, state.index));
security.accountId = payload.accountId;
security.securityId = payload.securityId;
security.shares = payload.shares;
state.items = [...state.items];
},
};
...
Advertisement
AccountsSecurity.vue (10:21)
<script lang="ts">
...
import {
AccountSecurityModel,
...
ACTION_ADD_ACCOUNT_SECURITY,
...
ACTION_UPDATE_ACCOUNT_SECURITY,
...
PayloadAddAccountSecurity,
...
PayloadUpdateAccountSecurity,
...
} from "@/store";
...
export default class AccountsSecurity extends Vue {
@Action(ACTION_ADD_ACCOUNT_SECURITY) private readonly addSecurity!: ActionFn<PayloadAddAccountSecurity>;
...
@Action(ACTION_UPDATE_ACCOUNT_SECURITY) private readonly updateSecurity!: ActionFn<PayloadUpdateAccountSecurity>;
...
private save() {
const accountSecurity = new AccountSecurityModel({
accountId: this.accountId,
id: this.id,
securityId: this.securityId,
shares: this.shares,
});
switch(this.id) {
case 0:
this.addSecurity(accountSecurity);
return;
default:
this.updateSecurity(accountSecurity);
return;
}
}
}
</script>
account-module.ts (16:39)
...
export const accountActions: StoreActionTree = {
...
[ACTION_ADD_ACCOUNT_SECURITY](this, { commit }, payload: PayloadAddAccountSecurity) {
const state = this.state[STATE_ACCOUNTS_SECURITIES];
const accountSecurity = state.items.find(
(x) => x.accountId === payload.accountId && x.securityId === payload.securityId,
);
if (typeof accountSecurity === "undefined") {
commit(MUTATION_ADD_ACCOUNT_SECURITY, payload);
} else {
payload.id = accountSecurity.id;
payload.shares += accountSecurity.shares;
commit(MUTATION_UPDATE_ACCOUNT_SECURITY, payload);
}
},
...
};
...
export const accountGetters: StoreGetterTree = {
...
[GETTER_ACCOUNT_SECURITIES]: (storeState, getters: IStoreGetters) => {
return (accountId: number) => {
...
const sorted = sort(accountSecurities, (x) => x.security.symbol);
return sorted;
};
},
...
};
...
store-constants.ts (20:18)
...
export const ACTION_REMOVE_ACCOUNT_SECURITY = "ACTION_REMOVE_ACCOUNT_SECURITY";
...
export const MUTATION_REMOVE_ACCOUNT_SECURITY = "MUTATION_REMOVE_ACCOUNT_SECURITY";
...
account-types.ts
...
export type PayloadRemoveAccountSecurity = AccountSecurityModel;
...
account-module.ts (20:40)
...
import {
...
ACTION_REMOVE_ACCOUNT_SECURITY,
...
MUTATION_REMOVE_ACCOUNT_SECURITY,
...
} from "@/store/store-constants";
...
import {
...
PayloadRemoveAccountSecurity,
...
} from "@/store/account-types";
...
export const accountActions: StoreActionTree = {
...
[ACTION_REMOVE_ACCOUNT_SECURITY](this, { commit }, payload: PayloadRemoveAccountSecurity) {
commit(MUTATION_REMOVE_ACCOUNT_SECURITY, payload);
},
...
[ACTION_UPDATE_ACCOUNT_SECURITY](this, { commit }, payload: PayloadUpdateAccountSecurity) {
const state = this.state[STATE_ACCOUNTS_SECURITIES];
const accountSecurity = state.items.find(
(x) => x.accountId === payload.accountId && x.securityId === payload.securityId,
);
if (typeof accountSecurity !== "undefined" && accountSecurity.id !== payload.id) {
commit(MUTATION_REMOVE_ACCOUNT_SECURITY, payload);
payload.id = accountSecurity.id;
payload.shares += accountSecurity.shares;
}
commit(MUTATION_UPDATE_ACCOUNT_SECURITY, payload);
},
};
...
export const accountMutations: StoreMutationTree = {
...
[MUTATION_REMOVE_ACCOUNT_SECURITY](storeState, payload: PayloadRemoveAccountSecurity) {
storeState[STATE_ACCOUNTS_SECURITIES].items = storeState[STATE_ACCOUNTS_SECURITIES].items.filter(
(x) => x.id !== payload.id,
);
},
...
};
...
Exciton Interactive LLC