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

hw1 #40

Closed
wants to merge 5 commits into from
Closed

hw1 #40

Show file tree
Hide file tree
Changes from all 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
4 changes: 2 additions & 2 deletions src/app.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import React from 'react';
import { createElement } from './utils.js';
import { createElement, formatCount } from './utils.js';
import './styles.css';

/**
Expand Down Expand Up @@ -27,7 +27,7 @@ function App({ store }) {
onClick={() => store.selectItem(item.code)}
>
<div className="Item-code">{item.code}</div>
<div className="Item-title">{item.title}</div>
<div className="Item-title">{item.title} {item.count && `| Выделили ${formatCount(item.count)}`}</div>
<div className="Item-actions">
<button onClick={() => store.deleteItem(item.code)}>Удалить</button>
</div>
Expand Down
15 changes: 13 additions & 2 deletions src/store.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
import { getLengthArray } from "./utils";

/**
* Хранилище состояния приложения
*/
class Store {
constructor(initState = {}) {
this.state = initState;
this.listeners = []; // Слушатели изменений состояния
this.lastCode = getLengthArray(this.state.list);
this.count = 1;
}

/**
Expand Down Expand Up @@ -44,7 +48,7 @@ class Store {
addItem() {
this.setState({
...this.state,
list: [...this.state.list, { code: this.state.list.length + 1, title: 'Новая запись' }],
list: [...this.state.list, { code: this.lastCode += 1, title: 'Новая запись' }],
});
}

Expand All @@ -67,9 +71,16 @@ class Store {
this.setState({
...this.state,
list: this.state.list.map(item => {
if (item.code === code) {
if (item.selected || item.code === code) {
item.selected = !item.selected;
}
if (item.selected) {
if (item.count >= 1) {
item.count++;
} else {
item.count = this.count;
}
}
return item;
}),
});
Expand Down
30 changes: 30 additions & 0 deletions src/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,3 +26,33 @@ export function createElement(name, props = {}, ...children) {

return element;
}

export function formatCount(count) {
const lastNum = count % 10;

if (lastNum === 2 || lastNum === 3 || lastNum === 4) {

if (count % 100 >= 12 && count % 100 <= 14) {
return `${count} раз`;
} else {
return `${count} раза`;
}

} else {
return `${count} раз`;
}

}

export function getLengthArray(array) {

if (array.length === 0) {
return 0;
} else {
const arrayCode = array.map((item) => {
return item.code;
})
return Math.max.apply(null, arrayCode);
}

}
Loading