This repository was archived by the owner on May 24, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
Final Project
Pascal edited this page Sep 1, 2017
·
19 revisions
/src/components/stocks/Stock.vue#L20
<div class="pull-right">
<button
class="btn btn-success"
@click="buyStock"
:disabled="insufficientFunds || quantity <= 0 || !Number.isInteger(quantity)"
>{{ insufficientFunds ? 'Insufficient Funds' : 'Buy' }}
</button>
</div>import Vue from 'vue';
import Vuex from 'vuex';
import stocks from './modules/stocks';
import portfolio from './modules/portfolio';
import * as actions from './actions';
Vue.use(Vuex);
export default new Vuex.Store({
actions,
modules: {
stocks,
portfolio
}
});NOTE concerning Namespacing (/src/store/modules/stocks.js) – If you're using Vuex version 2.1 or higher, you may use its auto-namespacing feature to avoid having to set up all the namespaces manually. You may learn more about it here: https://github.com/vuejs/vuex/releases/tag/v2.1.0
Use destructuring to get {stockId, quantity, stockPrice}. This also represents the order in Stock.vue of the buyStock() method; otherwise you couldn't pull it out.
/src/store/modules/portfolio.js#L7
const mutations = {
'BUY_STOCK'(state, {stockId, quantity, stockPrice}) {
// Check if there is a record;
// loop through array and check if element id is present
const record = state.stocks.find(element => element.id == stockId);
// if record is set (array not empty); update quantity
// else add new portfolio object to array
if (record) {
record.quantity += quantity;
} else {
state.stocks.push({
id: stockId,
quantity: quantity
});
}
// Update user's funds
state.funds -= stockPrice * quantity;
},
// Other Code
};/src/components/stocks/Stock.vue#L56
methods: {
buyStock() {
const order = {
stockId: this.stock.id,
stockPrice: this.stock.price,
quantity: this.quantity
};
this.$store.dispatch('buyStock', order);
this.quantity = 0;
}
}/src/store/modules/portfolio.js#L41
const getters = {
stockPortfolio (state, getters) {
// Problem: only ids in stocks array, I need to access the stocks module;
// Solution: .map() allows to transform each element in the array.
return state.stocks.map(stock => {
// 'getters' refers to global store.js (vuex)
// 'stock' refers to stock of current .map()
// 'element' refers to stock array of _all_ the stocks in the global store
const record = getters.stocks.find(element => element.id == stock.id);
return {
id: stock.id,
quantity: stock.quantity,
name: record.name,
price: record.price
}
});
},
// Get the user's funds
funds (state) {
return state.funds;
}
};- 00 Home
-
01 DOM Interactions
- 01 Events and Methods
- 02 HTML Binding
- 03 Computed Props
- 04_conditional_and_lists
- 05_filters
- 06 Components
- 07_disable_rerendering
- 08_raw_html
- 09_events
- 10_two_way_binding
- 11_computed_counts
- 12_shorthand
- 13 Dynamic CSS
- 14_using_style
- Exercise 1
- Exercise 2
- Exercise 3
- Exercise 4
-
02 Conditionals and Lists
- 01_v_if
- 02 v-for
- Exercise
- 03_monster_slayer
-
04_vue_instance
- 01_instance_vue
- 02_vue_lifecycle_in_practice
- 05_vue_cli
- 06_components
- 07_component_assignment
- 08 Communication between Components
-
09_advanced_components
- Assignment_8
- advanced_components
- 10_Wonderful_quoute_project
-
11 Forms
- form_assignments
- form_input
-
12_directives
- Assignment_directives
- custom_directives
-
13_filters_and_mixins
- assignment_filters_and_mixins
- filters_and_mixins
- 14 Animations and Transitions
-
15_http
- vue-resource
-
16 Routing
- routes
-
17 Vuex
- State Management
- vuex_master
-
18 Final Project
- the_stock_trader_app
- Assignment_7
- VueImages
- vue-mindspace