diff --git a/cypress/integration/homework_spec.js b/cypress/integration/homework_spec.js index 2731e30..14b2981 100644 --- a/cypress/integration/homework_spec.js +++ b/cypress/integration/homework_spec.js @@ -140,6 +140,7 @@ describe('Домашняя работа', () => { }); describe('Сценарий создания заказа в магазине', () => { before(() => { + cy.visit('/'); cy.get('.new-orders__create-button').click(); }); it('Сумма заказов отражается в строке «Всего получено денег»', () => { @@ -178,6 +179,7 @@ describe('Домашняя работа', () => { }); describe('Сценарий отправки заказа на производство на ферму', () => { before(() => { + cy.visit('/'); cy.get('.new-orders__create-button').click(); cy .get('button') @@ -224,8 +226,9 @@ describe('Домашняя работа', () => { }); }); }); - describe.only('Сценарий отправки заказа клиенту', () => { + describe('Сценарий отправки заказа клиенту', () => { before(() => { + cy.visit('/'); cy.get('.new-orders__create-button').click(); cy.get('.new-orders__create-button').click(); cy.get('.new-orders__create-button').click(); diff --git a/src/actions/farmActions.js b/src/actions/farmActions.js index 3b6b510..100d634 100644 --- a/src/actions/farmActions.js +++ b/src/actions/farmActions.js @@ -1 +1,20 @@ -import { MOVE_ORDER_TO_CUSTOMER } from './farmTypes'; +import { MOVE_ORDER_TO_CUSTOMER, ADD_ORDER_TO_FARM } from './farmTypes'; + +export const moveOrderToCustomer = id => { + return { + type: MOVE_ORDER_TO_CUSTOMER, + payload: id + }; +}; + +export const addOrderToFarm = (id, name, price, createdAt) => { + return { + type: ADD_ORDER_TO_FARM, + payload: { + id, + name, + price, + createdAt + } + }; +}; diff --git a/src/actions/farmTypes.js b/src/actions/farmTypes.js index 03cf0f7..d3e3394 100644 --- a/src/actions/farmTypes.js +++ b/src/actions/farmTypes.js @@ -1 +1,2 @@ export const MOVE_ORDER_TO_CUSTOMER = 'MOVE_ORDER_TO_CUSTOMER'; +export const ADD_ORDER_TO_FARM = 'ADD_ORDER_TO_FARM'; diff --git a/src/actions/marketActions.js b/src/actions/marketActions.js index db9a159..a02926c 100644 --- a/src/actions/marketActions.js +++ b/src/actions/marketActions.js @@ -1 +1,23 @@ -import { CREATE_ORDER, MOVE_ORDER_TO_FARM } from './marketTypes'; +import { + CREATE_ORDER, + DELETE_ORDER_FROM_MARKET +} from './marketTypes'; + +export const createOrder = (id, name, price, createdAt) => { + return { + type: CREATE_ORDER, + payload: { + id, + name, + price, + createdAt + } + }; +}; + +export const deleteOrderFromMarket = (id) => { + return { + type: DELETE_ORDER_FROM_MARKET, + payload: id + }; +}; diff --git a/src/actions/marketTypes.js b/src/actions/marketTypes.js index 57fd24f..1e81427 100644 --- a/src/actions/marketTypes.js +++ b/src/actions/marketTypes.js @@ -1,5 +1,5 @@ export const CREATE_ORDER = 'CREATE_ORDER'; -export const MOVE_ORDER_TO_FARM = 'MOVE_ORDER_TO_FARM'; +//export const MOVE_ORDER_TO_FARM = 'MOVE_ORDER_TO_FARM'; export const DELETE_ORDER_FROM_MARKET = 'DELETE_ORDER_FROM_MARKET'; diff --git a/src/components/App/App.js b/src/components/App/App.js index c95dd15..c5e4b60 100644 --- a/src/components/App/App.js +++ b/src/components/App/App.js @@ -6,7 +6,13 @@ import Budget from '../Budget'; export class App extends Component { render() { - return
; + return( +
+ + + +
+ ) } } diff --git a/src/components/Budget/Budget.js b/src/components/Budget/Budget.js index 7a92bed..8a3eb70 100644 --- a/src/components/Budget/Budget.js +++ b/src/components/Budget/Budget.js @@ -1,6 +1,41 @@ import React from 'react'; import './Budget.css'; +import { connect } from 'react-redux'; +import { getBudget } from 'reducers/budget'; -export const Budget = () =>
; +const mapStateToProps = state => ({ + budget: getBudget(state) +}); -export default Budget; +export const Budget = props => { + const { + income, + sellersExpences, + farmExpences, + deliveryExpences + } = props.budget; + const total = income + (sellersExpences + farmExpences + deliveryExpences); + return ( +
+

Бюджет

+

+ Всего получено денег: {income} +

+

+ Расходы продавцов: {sellersExpences} +

+

+ Расходы на ферме: {farmExpences} +

+

+ Расходы на доставку:{' '} + {deliveryExpences} +

+

+ Итого: {total} +

+
+ ); +}; + +export default connect(mapStateToProps)(Budget); diff --git a/src/components/Farm/Farm.js b/src/components/Farm/Farm.js index 36450dd..dccc1b7 100644 --- a/src/components/Farm/Farm.js +++ b/src/components/Farm/Farm.js @@ -1,10 +1,39 @@ import React, { Component } from 'react'; import './Farm.css'; +import { connect } from 'react-redux'; +import Order from 'components/Order'; +import { getFarmOrders } from 'reducers/farm'; +import { moveOrderToCustomer } from 'actions/farmActions'; + +const mapStateToProps = state => ({ + farm: getFarmOrders(state) +}); + +const mapDispatchToProps = { + moveOrderToCustomer +}; export class Farm extends Component { render() { - return
; + const { moveOrderToCustomer } = this.props; + const { farm } = this.props; + return ( +
+

Производство на ферме

+ +
{farm.map(order => )}
+
+ ); } } -export default Farm; +export default connect( + mapStateToProps, + mapDispatchToProps +)(Farm); diff --git a/src/components/Market/Market.js b/src/components/Market/Market.js index 488c865..55065ca 100644 --- a/src/components/Market/Market.js +++ b/src/components/Market/Market.js @@ -1,5 +1,11 @@ import React, { Component } from 'react'; import './Market.css'; +import { connect } from 'react-redux'; +import Order from 'components/Order'; +import { createOrder, deleteOrderFromMarket } from 'actions/marketActions'; +import { addOrderToFarm } from 'actions/farmActions'; +import { getMarketOrders } from 'reducers/market'; +import { getFarmOrders } from 'reducers/farm'; let id = 0; @@ -20,7 +26,7 @@ export const vegetables = [ 'Лук', 'Перец', 'Картофель', - 'Редька', + 'Редька' ]; const getNewOrder = () => { @@ -28,14 +34,65 @@ const getNewOrder = () => { id: getId(), name: vegetables[Math.floor(Math.random() * vegetables.length)], price: 100 + Math.floor(Math.random() * 100), - createdAt: new Date(), + createdAt: new Date() }; }; +const mapStateToProps = state => ({ + market: getMarketOrders(state), + farm: getFarmOrders(state) +}); + +const mapDispatchToProps = { + createOrder, + deleteOrderFromMarket, + addOrderToFarm +}; + export class Market extends Component { render() { - return
; + const { createOrder, deleteOrderFromMarket, addOrderToFarm } = this.props; + const { market } = this.props; + return ( +
+

Новые заказы в магазине

+ + +
+ {market.map(order => )} +
+
+ ); } } -export default Market; +export default connect( + mapStateToProps, + mapDispatchToProps +)(Market); diff --git a/src/components/Order/Order.js b/src/components/Order/Order.js index 6525143..2f58a36 100644 --- a/src/components/Order/Order.js +++ b/src/components/Order/Order.js @@ -1,8 +1,21 @@ import React from 'react'; import './Order.css'; -const Order = ({ name, price, createdAt }) => ( -
+const Order = (order) => ( +
+
+

Название: {order.name}

+

+ Цена: {order.price} +

+
+
+

+ Создан: {order.createdAt.toISOString().slice(0,10)} +

+
+
); + export default Order; diff --git a/src/reducers/budget.js b/src/reducers/budget.js index e69de29..2a2e1ea 100644 --- a/src/reducers/budget.js +++ b/src/reducers/budget.js @@ -0,0 +1,41 @@ +import { CREATE_ORDER } from 'actions/marketTypes'; +import { ADD_ORDER_TO_FARM, MOVE_ORDER_TO_CUSTOMER } from 'actions/farmTypes'; + +const initialState = { + income: 0, + sellersExpences: 0, + farmExpences: 0, + deliveryExpences: 0 +}; + +export default (state = initialState, action) => { + switch (action.type) { + case CREATE_ORDER: + const income = state.income + action.payload.price; + const sellersExpences = state.sellersExpences - 20; + return { + ...state, + income, + sellersExpences + }; + + case ADD_ORDER_TO_FARM: + const farmExpences = state.farmExpences - 100; + return { + ...state, + farmExpences + }; + + case MOVE_ORDER_TO_CUSTOMER: + const deliveryExpences = state.deliveryExpences - 20; + return { + ...state, + deliveryExpences + }; + + default: + return state; + } +}; + +export const getBudget = state => state.budget; diff --git a/src/reducers/farm.js b/src/reducers/farm.js index e69de29..12f3669 100644 --- a/src/reducers/farm.js +++ b/src/reducers/farm.js @@ -0,0 +1,23 @@ +import { combineReducers } from 'redux'; +import { MOVE_ORDER_TO_CUSTOMER, ADD_ORDER_TO_FARM } from 'actions/farmTypes'; + +const orders = (state = [], action) => { + switch (action.type) { + case ADD_ORDER_TO_FARM: + const { payload } = action; + return [...state, payload]; + + case MOVE_ORDER_TO_CUSTOMER: + const id = action.payload; + return state.filter(order => order.id !== id); + + default: + return state; + } +}; + +export default combineReducers({ + orders +}); + +export const getFarmOrders = state => state.farm.orders; diff --git a/src/reducers/market.js b/src/reducers/market.js index e69de29..8aaf3ca 100644 --- a/src/reducers/market.js +++ b/src/reducers/market.js @@ -0,0 +1,26 @@ +import { combineReducers } from 'redux'; +import { CREATE_ORDER, DELETE_ORDER_FROM_MARKET } from 'actions/marketTypes'; + +const orders = (state = [], action) => { + switch (action.type) { + case CREATE_ORDER: + const { payload } = action; + return [...state, payload]; + + case DELETE_ORDER_FROM_MARKET: + const id = action.payload; + return state.filter(order => order.id !== id); + + // case DELETE_ORDER_FROM_MARKET: + // return state.filter(order => order.id !== action.payload); + + default: + return state; + } +}; + +export default combineReducers({ + orders +}); + +export const getMarketOrders = state => state.market.orders;