Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ivan3008/homework 6 - черновик #181

Open
wants to merge 3 commits into
base: Ivan3008/homework_6
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion cypress/integration/homework_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,7 @@ describe('Домашняя работа', () => {
});
describe('Сценарий создания заказа в магазине', () => {
before(() => {
cy.visit('/');
cy.get('.new-orders__create-button').click();
});
it('Сумма заказов отражается в строке «Всего получено денег»', () => {
Expand Down Expand Up @@ -178,6 +179,7 @@ describe('Домашняя работа', () => {
});
describe('Сценарий отправки заказа на производство на ферму', () => {
before(() => {
cy.visit('/');
cy.get('.new-orders__create-button').click();
cy
.get('button')
Expand Down Expand Up @@ -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();
Expand Down
7 changes: 7 additions & 0 deletions src/actions/farmActions.js
Original file line number Diff line number Diff line change
@@ -1 +1,8 @@
import { MOVE_ORDER_TO_CUSTOMER } from './farmTypes';

export const moveOrderToCustomer = id => {
return {
type: MOVE_ORDER_TO_CUSTOMER,
payload: id
};
};
37 changes: 36 additions & 1 deletion src/actions/marketActions.js
Original file line number Diff line number Diff line change
@@ -1 +1,36 @@
import { CREATE_ORDER, MOVE_ORDER_TO_FARM } from './marketTypes';
import {
CREATE_ORDER,
MOVE_ORDER_TO_FARM,
DELETE_ORDER_FROM_MARKET
} from './marketTypes';

export const createOrder = (id, name, price, createdAt) => {
return {
type: CREATE_ORDER,
payload: {
id,
name,
price,
createdAt
}
};
};

export const moveOrderToFarm = (id, name, price, createdAt) => {
return {
type: MOVE_ORDER_TO_FARM,
payload: {
id,
name,
price,
createdAt
}
};
};

export const deleteOrderFromMarket = id => {
return {
type: DELETE_ORDER_FROM_MARKET,
payload: id
};
};
7 changes: 6 additions & 1 deletion src/components/App/App.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,12 @@ import Budget from '../Budget';

export class App extends Component {
render() {
return <div className="app" />;
return(
<div className="app">
<Market />
<Farm />
</div>
)
}
}

Expand Down
26 changes: 24 additions & 2 deletions src/components/Farm/Farm.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,32 @@
import React, { Component } from 'react';
import './Farm.css';
import { connect } from 'react-redux';
import Order from 'components/Order';
import {
moveOrderToCustomer
} from 'actions/farmActions';

const mapStateToProps = state => ({
farm: state.farm
});

const mapDispatchToProps = {
moveOrderToCustomer
};

export class Farm extends Component {
render() {
return <div className="farm" />;
return (
<div className="farm">
<h2>Производство на ферме</h2>
<button>Отправить урожай клиенту</button>
<div />
</div>
);
}
}

export default Farm;
export default connect(
mapStateToProps,
mapDispatchToProps
)(Farm);
60 changes: 56 additions & 4 deletions src/components/Market/Market.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
import React, { Component } from 'react';
import './Market.css';
import { connect } from 'react-redux';
import Order from 'components/Order';
import {
createOrder,
moveOrderToFarm,
deleteOrderFromMarket
} from 'actions/marketActions';

let id = 0;

Expand All @@ -20,22 +27,67 @@ export const vegetables = [
'Лук',
'Перец',
'Картофель',
'Редька',
'Редька'
];

const getNewOrder = () => {
return {
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: state.market
});

const mapDispatchToProps = {
createOrder,
moveOrderToFarm,
deleteOrderFromMarket
};

export class Market extends Component {
render() {
return <div className="market" />;
const { createOrder, moveOrderToFarm, deleteOrderFromMarket } = this.props;
const { market } = this.props;
return (
<div className="market">
<h2>Новые заказы в магазине</h2>
<button
onClick={() => {
const orderData = getNewOrder();
createOrder(
orderData.id,
orderData.name,
orderData.price,
orderData.createdAt
);
}}
className="new-orders__create-button"
>
Создать заказ
</button>
<button
onClick={() => {
Object.keys(market).length > 0 &&
moveOrderToFarm(market[0].id, market[0].name, market[0].price, market[0].createdAt) &&
deleteOrderFromMarket(market[0].id);
}}
>
Отправить заказ на ферму
</button>
<div className="order-list">
{market.map(order => <Order key={order.id} {...order} />)}
</div>
</div>
);
}
}

export default Market;
export default connect(
mapStateToProps,
mapDispatchToProps
)(Market);
17 changes: 15 additions & 2 deletions src/components/Order/Order.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,21 @@
import React from 'react';
import './Order.css';

const Order = ({ name, price, createdAt }) => (
<div className="order" />
const Order = (order) => (
<div className="order">
<div className="order__upper">
<p className="p--order">Название: {order.name}</p>
<p className="p--order">
Цена: <span className="order-price">{order.price}</span>
</p>
</div>
<div className="order__lower">
<p className="p--order">
Создан: {order.createdAt.toISOString().slice(0,10)}
</p>
</div>
</div>
);


export default Order;
1 change: 1 addition & 0 deletions src/reducers/farm.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@

21 changes: 21 additions & 0 deletions src/reducers/market.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import {
CREATE_ORDER,
MOVE_ORDER_TO_FARM,
DELETE_ORDER_FROM_MARKET
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

этот экшен тебе не нужен в этом месте.

} from 'actions/marketTypes';

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Нужно добавить
import { combineReducers } from 'redux';

export default (state = [], action) => {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Перепиши эту строчку так
const orders = (state = [], action) => {

switch (action.type) {
case CREATE_ORDER:
return [action.payload, ...state];

case MOVE_ORDER_TO_FARM:
return [action.payload, ...state];

case DELETE_ORDER_FROM_MARKET:
return state.filter(order => order.id !== action.payload);

default:
return state;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Код свитча сверь с этим:

    switch (action.type) {
        case CREATE_ORDER:
            const { payload } = action;
            return [...state, payload];
        case MOVE_ORDER_TO_FARM:
            const { id } = action.payload;
            return state.filter(order => order.id !== id);
        default:
            return state;
    }

}
};
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Потребуется вот это

export default combineReducers({
    orders
});

export const getMarketOrders = state => state.market.orders;