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

finished first home task #19

Open
wants to merge 2 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
12 changes: 12 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
},
"homepage": "https://github.com/ylabio/react-webinar-3#readme",
"dependencies": {
"numeralize-ru": "^2.0.0",
"react": "^18.3.1",
"react-dom": "^18.3.1"
},
Expand All @@ -31,9 +32,9 @@
"css-loader": "^7.1.2",
"html-webpack-plugin": "^5.6.0",
"mini-css-extract-plugin": "^2.9.1",
"prettier": "3.3.3",
"webpack": "^5.94.0",
"webpack-cli": "^5.1.4",
"webpack-dev-server": "^5.1.0",
"prettier": "3.3.3"
"webpack-dev-server": "^5.1.0"
}
}
3 changes: 2 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 { getClicksCount } from './utils.js';

/**
* Приложение
Expand All @@ -27,7 +28,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}{getClicksCount(item)}</div>
<div className="Item-actions">
<button onClick={() => store.deleteItem(item.code)}>Удалить</button>
</div>
Expand Down
15 changes: 8 additions & 7 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,17 @@ import { createRoot } from 'react-dom/client';
import { createElement } from './utils.js';
import App from './app.js';
import Store from './store.js';
import { getRandomNumber } from './utils.js';

const store = new Store({
list: [
{ code: 1, title: 'Название элемента' },
{ code: 2, title: 'Некий объект' },
{ code: 3, title: 'Заголовок' },
{ code: 4, title: 'Очень длинное название элемента из семи слов' },
{ code: 5, title: 'Запись' },
{ code: 6, title: 'Шестая запись' },
{ code: 7, title: 'Седьмая запись' },
{ code: getRandomNumber(), title: 'Название элемента' },
{ code: getRandomNumber(), title: 'Некий объект' },
{ code: getRandomNumber(), title: 'Заголовок' },
{ code: getRandomNumber(), title: 'Очень длинное название элемента из семи слов' },
{ code: getRandomNumber(), title: 'Запись' },
{ code: getRandomNumber(), title: 'Шестая запись' },
{ code: getRandomNumber(), title: 'Седьмая запись' },
],
});

Expand Down
8 changes: 7 additions & 1 deletion src/store.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { getRandomNumber } from "./utils";
/**
* Хранилище состояния приложения
*/
Expand Down Expand Up @@ -44,7 +45,7 @@ class Store {
addItem() {
this.setState({
...this.state,
list: [...this.state.list, { code: this.state.list.length + 1, title: 'Новая запись' }],
list: [...this.state.list, { code: getRandomNumber(), title: 'Новая запись' }],
});
}

Expand All @@ -69,6 +70,11 @@ class Store {
list: this.state.list.map(item => {
if (item.code === code) {
item.selected = !item.selected;
} else {
item.selected = '';
}
if (item.selected) {
item.clickedTimes = item.clickedTimes ? item.clickedTimes + 1 : 1;
}
return item;
}),
Expand Down
18 changes: 18 additions & 0 deletions src/utils.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { pluralize } from "numeralize-ru";

const propNames = new Set(['id', 'className', 'textContent', 'onclick']);

/**
Expand Down Expand Up @@ -26,3 +28,19 @@ export function createElement(name, props = {}, ...children) {

return element;
}

function generateRandomNumber() {
let number = 1;
return function() {
return number++;
};
}

export const getRandomNumber = generateRandomNumber();

export function getClicksCount(item) {
if (!item.clickedTimes) {
return '';
}
return ` | Выделили ${item.clickedTimes} ${pluralize(item.clickedTimes, 'раз', 'раза', 'раз')}`;
}
Loading