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

Домашнее задание 1 #32

Open
wants to merge 4 commits into
base: lecture-1
Choose a base branch
from
Open
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
7 changes: 6 additions & 1 deletion src/app.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import React from 'react';
import { createElement } from './utils.js';
import './styles.css';
import { getPlural } from './utils.js';

/**
* Приложение
Expand All @@ -27,7 +28,11 @@ 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.selectionCount &&
` | Выделяли ${item.selectionCount} ${getPlural(item.selectionCount)}`}
</div>
<div className="Item-actions">
<button onClick={() => store.deleteItem(item.code)}>Удалить</button>
</div>
Expand Down
9 changes: 8 additions & 1 deletion src/store.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
class Store {
constructor(initState = {}) {
this.state = initState;
this.state.nextCode = Math.max(...this.state.list.map(el => el.code)) + 1;
this.listeners = []; // Слушатели изменений состояния
}

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

Expand All @@ -68,7 +70,12 @@ class Store {
...this.state,
list: this.state.list.map(item => {
if (item.code === code) {
if (!item.selected) {
item.selectionCount = item.selectionCount ? item.selectionCount + 1 : 1;
}
item.selected = !item.selected;
} else if (item.selected) {
item.selected = false;
}
return item;
}),
Expand Down
15 changes: 15 additions & 0 deletions src/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,3 +26,18 @@ export function createElement(name, props = {}, ...children) {

return element;
}

/**
* Получение слово "раз" в правильной форме в зависимости от числительного
* @param count {Number} Количество выделений
* @returns {String}
*/
export function getPlural(count) {
const lastDigit = count % 10;
const preLastDigit = Math.trunc(count / 10) % 10;
console.log(preLastDigit);
if([2, 3, 4].indexOf(lastDigit) !== -1 && preLastDigit !== 1) {
return "раза";
}
return "раз";
}
Loading