Skip to content

Commit

Permalink
lesson2
Browse files Browse the repository at this point in the history
  • Loading branch information
IvanEr-ui committed Sep 11, 2024
1 parent 1a3af94 commit c3635eb
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 8 deletions.
4 changes: 2 additions & 2 deletions src/app.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import React from 'react';
import { createElement } from './utils.js';
import './styles.css';

/**
Expand All @@ -8,8 +7,8 @@ import './styles.css';
* @returns {React.ReactElement}
*/
function App({ store }) {
console.log(store.getState())
const list = store.getState().list;

return (
<div className="App">
<div className="App-head">
Expand All @@ -28,6 +27,7 @@ function App({ store }) {
>
<div className="Item-code">{item.code}</div>
<div className="Item-title">{item.title}</div>
{item.countClick && <div className="Item-click">{`Выделяли ${item.countClick} раз`}</div>}
<div className="Item-actions">
<button onClick={() => store.deleteItem(item.code)}>Удалить</button>
</div>
Expand Down
1 change: 0 additions & 1 deletion src/index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import React from 'react';
import { createRoot } from 'react-dom/client';
import { createElement } from './utils.js';
import App from './app.js';
import Store from './store.js';

Expand Down
17 changes: 13 additions & 4 deletions src/store.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,10 @@
*/
class Store {
constructor(initState = {}) {
this.state = initState;
this.state = {
...initState,
lastCodeList: initState.list.at(-1).code
};
this.listeners = []; // Слушатели изменений состояния
}

Expand Down Expand Up @@ -44,7 +47,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.lastCodeList + 1, title: `Новая запись ${this.state.lastCodeList + 1}` }],
lastCodeList: this.state.lastCodeList + 1
});
}

Expand All @@ -68,9 +72,14 @@ class Store {
...this.state,
list: this.state.list.map(item => {
if (item.code === code) {
item.selected = !item.selected;
return {
...item,
selected: !item.selected,
countClick: item.countClick === undefined ? 1
: !item.selected ? item.countClick + 1 : item.countClick
}
}
return item;
return { ...item, selected: false };
}),
});
}
Expand Down
7 changes: 6 additions & 1 deletion src/styles.css
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,13 @@ body {
padding: 20px 10px;
}

.Item-click {
width: 100%;
padding: 20px 10px;
}

.Item-actions {
display: flex;
justify-content: flex-end;
padding: 20px 20px 20px 10px;
}
}

0 comments on commit c3635eb

Please sign in to comment.