From fc6a59f448237f56f71457b18d46797897b75f12 Mon Sep 17 00:00:00 2001 From: Henadzi Arashkevich Date: Tue, 10 Sep 2024 15:14:54 +0300 Subject: [PATCH 1/3] feat: add task 1,2,3 --- src/app.js | 11 ++++++++++- src/index.js | 19 ++++++++++++------- src/store.js | 16 +++++++++++++++- src/utils.js | 17 +++++++++++++++++ 4 files changed, 54 insertions(+), 9 deletions(-) diff --git a/src/app.js b/src/app.js index 8ad1c64b4..54aa307d2 100644 --- a/src/app.js +++ b/src/app.js @@ -1,5 +1,6 @@ import React from 'react'; import { createElement } from './utils.js'; +import { getSize } from './utils.js'; import './styles.css'; /** @@ -27,7 +28,15 @@ function App({ store }) { onClick={() => store.selectItem(item.code)} >
{item.code}
-
{item.title}
+
{`${item.title} ${ + item.counter > 0 + ? `| Выделяли ${item.counter} ${getSize(item.counter, { + one: 'раз', + much: 'раза', + count: 'раз', + })}` + : '' + }`}
diff --git a/src/index.js b/src/index.js index faa61aa77..b305d1fc8 100644 --- a/src/index.js +++ b/src/index.js @@ -6,13 +6,18 @@ import Store from './store.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: 1, id: Math.random(), counter: 0, title: 'Название элемента' }, + { code: 2, id: Math.random(), counter: 0, title: 'Некий объект' }, + { code: 3, id: Math.random(), counter: 0, title: 'Заголовок' }, + { + code: 4, + id: Math.random(), + counter: 0, + title: 'Очень длинное название элемента из семи слов', + }, + { code: 5, id: Math.random(), counter: 0, title: 'Запись' }, + { code: 6, id: Math.random(), counter: 0, title: 'Шестая запись' }, + { code: 7, id: Math.random(), counter: 0, title: 'Седьмая запись' }, ], }); diff --git a/src/store.js b/src/store.js index 9afab7e81..a21387e66 100644 --- a/src/store.js +++ b/src/store.js @@ -44,7 +44,15 @@ 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.list.reduce((acc, curr) => (acc.code > curr.code ? acc : curr)).code + 1, + title: 'Новая запись', + id: Math.random(), + counter: 0, + }, + ], }); } @@ -69,6 +77,12 @@ class Store { list: this.state.list.map(item => { if (item.code === code) { item.selected = !item.selected; + item.counter = item.counter + 1; + } else { + item.selected = false; + } + if (item.selected) { + item.counter = item.counter + 1; } return item; }), diff --git a/src/utils.js b/src/utils.js index d1b605243..991d27210 100644 --- a/src/utils.js +++ b/src/utils.js @@ -26,3 +26,20 @@ export function createElement(name, props = {}, ...children) { return element; } +export const getSize = (quantity, { one, much, count }) => { + while (quantity > 20) { + quantity = quantity.toString().slice(-1); + quantity = parseInt(quantity); + } + if (quantity === 1) { + return one; + } + if (quantity > 1 && quantity <= 4) { + return much; + } + if (quantity >= 5 && quantity <= 20) { + return count; + } + + return count; +}; From 7b92048856ae9c3d47fa7adf20a6779cbe1bd31f Mon Sep 17 00:00:00 2001 From: Henadzi Arashkevich Date: Sun, 15 Sep 2024 00:05:32 +0300 Subject: [PATCH 2/3] fix: mentor's comments --- src/index.js | 17 ++++++++--------- src/store.js | 2 -- src/styles.css | 3 +++ src/utils.js | 5 +++++ 4 files changed, 16 insertions(+), 11 deletions(-) diff --git a/src/index.js b/src/index.js index b305d1fc8..8bbed52f7 100644 --- a/src/index.js +++ b/src/index.js @@ -1,23 +1,22 @@ import React from 'react'; import { createRoot } from 'react-dom/client'; -import { createElement } from './utils.js'; +import { spawnCode } from './utils.js'; import App from './app.js'; import Store from './store.js'; const store = new Store({ list: [ - { code: 1, id: Math.random(), counter: 0, title: 'Название элемента' }, - { code: 2, id: Math.random(), counter: 0, title: 'Некий объект' }, - { code: 3, id: Math.random(), counter: 0, title: 'Заголовок' }, + { code: spawnCode(), counter: 0, title: 'Название элемента' }, + { code: spawnCode(), counter: 0, title: 'Некий объект' }, + { code: spawnCode(), counter: 0, title: 'Заголовок' }, { - code: 4, - id: Math.random(), + code: spawnCode(), counter: 0, title: 'Очень длинное название элемента из семи слов', }, - { code: 5, id: Math.random(), counter: 0, title: 'Запись' }, - { code: 6, id: Math.random(), counter: 0, title: 'Шестая запись' }, - { code: 7, id: Math.random(), counter: 0, title: 'Седьмая запись' }, + { code: spawnCode(), counter: 0, title: 'Запись' }, + { code: spawnCode(), counter: 0, title: 'Шестая запись' }, + { code: spawnCode(), counter: 0, title: 'Седьмая запись' }, ], }); diff --git a/src/store.js b/src/store.js index a21387e66..b4ce06625 100644 --- a/src/store.js +++ b/src/store.js @@ -49,7 +49,6 @@ class Store { { code: this.state.list.reduce((acc, curr) => (acc.code > curr.code ? acc : curr)).code + 1, title: 'Новая запись', - id: Math.random(), counter: 0, }, ], @@ -77,7 +76,6 @@ class Store { list: this.state.list.map(item => { if (item.code === code) { item.selected = !item.selected; - item.counter = item.counter + 1; } else { item.selected = false; } diff --git a/src/styles.css b/src/styles.css index 67e37a9b3..18c1beb24 100644 --- a/src/styles.css +++ b/src/styles.css @@ -22,6 +22,7 @@ body { .App-head h1 { margin: 0; padding: 20px 20px 10px; + cursor: default; } .App-controls { @@ -47,11 +48,13 @@ body { .Item-code { padding: 20px 10px 20px 20px; + cursor: pointer; } .Item-title { width: 100%; padding: 20px 10px; + cursor: pointer; } .Item-actions { diff --git a/src/utils.js b/src/utils.js index 991d27210..44b449f55 100644 --- a/src/utils.js +++ b/src/utils.js @@ -43,3 +43,8 @@ export const getSize = (quantity, { one, much, count }) => { return count; }; + +export const spawnCode = (() => { + let start = 0; + return () => ++start; +})(); From 4e51d4beb43e33a12bad0f114c3f9c596e8efc0e Mon Sep 17 00:00:00 2001 From: Henadzi Arashkevich Date: Sun, 15 Sep 2024 22:02:50 +0300 Subject: [PATCH 3/3] fix: delete count in index.js --- src/index.js | 18 +++++++----------- src/store.js | 6 +++++- 2 files changed, 12 insertions(+), 12 deletions(-) diff --git a/src/index.js b/src/index.js index 8bbed52f7..e3cc91064 100644 --- a/src/index.js +++ b/src/index.js @@ -6,17 +6,13 @@ import Store from './store.js'; const store = new Store({ list: [ - { code: spawnCode(), counter: 0, title: 'Название элемента' }, - { code: spawnCode(), counter: 0, title: 'Некий объект' }, - { code: spawnCode(), counter: 0, title: 'Заголовок' }, - { - code: spawnCode(), - counter: 0, - title: 'Очень длинное название элемента из семи слов', - }, - { code: spawnCode(), counter: 0, title: 'Запись' }, - { code: spawnCode(), counter: 0, title: 'Шестая запись' }, - { code: spawnCode(), counter: 0, title: 'Седьмая запись' }, + { code: 1, title: 'Название элемента' }, + { code: 2, title: 'Некий объект' }, + { code: 3, title: 'Заголовок' }, + { code: 4, title: 'Очень длинное название элемента из семи слов' }, + { code: 5, title: 'Запись' }, + { code: 6, title: 'Шестая запись' }, + { code: 7, title: 'Седьмая запись' }, ], }); diff --git a/src/store.js b/src/store.js index b4ce06625..4626c0314 100644 --- a/src/store.js +++ b/src/store.js @@ -80,7 +80,11 @@ class Store { item.selected = false; } if (item.selected) { - item.counter = item.counter + 1; + if (item.counter) { + item.counter = item.counter + 1; + } else { + item.counter = 1; + } } return item; }),