Skip to content

Commit df93103

Browse files
committed
by id fix
1 parent 4c7d787 commit df93103

File tree

4 files changed

+49
-24
lines changed

4 files changed

+49
-24
lines changed

client/src/NoteComponents/ModalNoteEdit.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,10 @@ function calcMaxRows() {
2525
*/
2626
function ModalNoteEdit() {
2727
/**получение контекста */
28-
const { removeNote, changeNoteColor, unsetEditNoteId, editNoteContent, getNoteByIndex, editNoteId } = React.useContext(NotesContext)
28+
const { removeNote, changeNoteColor, unsetEditNoteId, editNoteContent, getNoteById, editNoteId } = React.useContext(NotesContext)
2929

3030
/** обьект заметки */
31-
const note = getNoteByIndex(editNoteId)
31+
const note = getNoteById(editNoteId)
3232
React.useEffect(() => { if (note !== null) open() }, [note])
3333

3434
/**хук состояния формы */

client/src/NoteComponents/NoteItem.js

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
* @file NoteItem.js
33
*/
44
import React, { useContext } from 'react'
5-
import PropTypes from 'prop-types'
65
import NotesContext from '../Context/NotesContext'
76
import Note, { PropTypeNote } from '../Shared/noteType/Note'
87
import ReactMarkdown from 'react-markdown'
@@ -18,7 +17,7 @@ function fixLineBreaks(mdStr) {
1817
* @param {*} param0
1918
*
2019
*/
21-
function NoteItem({ note = new Note(), index }) {
20+
function NoteItem({ note = new Note() }) {
2221
/**Подключение контекста */
2322
const { setEditNoteId, editNoteOrder } = useContext(NotesContext)
2423

@@ -39,7 +38,7 @@ function NoteItem({ note = new Note(), index }) {
3938
<div className="p-1" >
4039
<div className="card" style={{ backgroundColor: bgColor }} >
4140
{/**Заголовок и текст заметки с обработчиками отображения markdown*/}
42-
<div className="card-body" onClick={() => setEditNoteId(index)} >
41+
<div className="card-body" onClick={() => setEditNoteId(note.id)} >
4342
<div
4443
className="card-title h5"
4544
style={{ overflow: "hidden", display: "-webkit-box", WebkitLineClamp: String(lineClip / 2), WebkitBoxOrient: "vertical" }} >
@@ -56,14 +55,14 @@ function NoteItem({ note = new Note(), index }) {
5655
<button
5756
className={footerBtn.className}
5857
style={footerBtn.style}
59-
onClick={() => editNoteOrder(index, false)}
58+
onClick={() => editNoteOrder(note.id, false)}
6059
>
6160
<i className="bi bi-chevron-compact-right"></i>
6261
</button>
6362
<button
6463
className={footerBtn.className}
6564
style={footerBtn.style}
66-
onClick={() => editNoteOrder(index, true)}
65+
onClick={() => editNoteOrder(note.id, true)}
6766
>
6867
<i className="bi bi-chevron-compact-left"></i>
6968
</button>
@@ -75,8 +74,7 @@ function NoteItem({ note = new Note(), index }) {
7574

7675
// Валидация
7776
NoteItem.propTypes = {
78-
note: PropTypeNote.isRequired,
79-
index: PropTypes.number
77+
note: PropTypeNote.isRequired
8078
}
8179

8280
export default NoteItem

client/src/NoteComponents/NoteList.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,9 +54,9 @@ function NoteList(props) {
5454
{/**Отзывчивая сетка карточек */}
5555
<StackGrid className="container p-0" {...gridSettings}>
5656
{/**Рендер каждой карточки из массива */}
57-
{Array.isArray(props.notes) ? (props.notes.sort(sortByOrder).reverse().map((note, index) => {
57+
{Array.isArray(props.notes) ? (props.notes.sort(sortByOrder).reverse().map((note) => {
5858
return (
59-
<NoteItem note={note} key={note.id} index={index} />
59+
<NoteItem note={note} key={note.id} />
6060
)
6161
})) : null}
6262
</StackGrid>

client/src/Pages/NotesPage.js

Lines changed: 40 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -86,9 +86,10 @@ function NotesPage() {
8686

8787
/**
8888
* удаление карточки
89-
* @param {*} index
89+
* @param {*} id
9090
*/
91-
function removeNote(index) {
91+
function removeNote(id) {
92+
const index = getNoteIndexById(id)
9293
const toDelete = notesArr.splice(index, 1)[0]
9394
setNotesArr([...notesArr])
9495
loadDataToServer(toDelete, "delete")
@@ -107,21 +108,20 @@ function NotesPage() {
107108
text: noteData.text,
108109
order: calcOrder(notesArr)
109110
})
110-
//console.log(newId, newNote.id);
111-
const newIndex = (notesArr != null) ? notesArr.length : 0
112111
setNotesArr(
113112
(notesArr != null) ? notesArr.concat([newNote]) : [newNote]
114113
)
115114
loadDataToServer(newNote, "set")
116-
setEditNoteId(newIndex)
115+
setEditNoteId(newId)
117116
}
118117

119118
/**
120119
* Изменение цвета карточки
121120
* @param {*} index
122121
* @param {*} color
123122
*/
124-
function changeNoteColor(index, color) {
123+
function changeNoteColor(id, color) {
124+
const index = getNoteIndexById(id)
125125
notesArr[index].color = color
126126
setNotesArr([...notesArr])
127127
loadDataToServer(notesArr[index], "set")
@@ -133,8 +133,9 @@ function NotesPage() {
133133
* @param {*} name
134134
* @param {*} text
135135
*/
136-
function editNoteContent(index, name, text) {
137-
if (notesArr[index]) {
136+
function editNoteContent(id, name, text) {
137+
const index = getNoteIndexById(id)
138+
if (index !== null) {
138139
let note = new Note(notesArr[index])
139140
note.name = name
140141
note.text = text
@@ -149,8 +150,9 @@ function NotesPage() {
149150
* @param {number} index
150151
* @param {boolean} orderOperationFlag
151152
*/
152-
function editNoteOrder(index, orderOperationFlag) {
153-
if (notesArr[index]) {
153+
function editNoteOrder(id, orderOperationFlag) {
154+
const index = getNoteIndexById(id)
155+
if (index !== null) {
154156
notesArr[index].order += orderOperationFlag ? 1 : -1
155157
let fixedArr = fixOrders(notesArr)
156158
setNotesArr(fixedArr)
@@ -160,9 +162,34 @@ function NotesPage() {
160162
}
161163
}
162164

165+
///////////
166+
163167
/**функция получения карточки по id */
164-
function getNoteByIndex(index) {
165-
return index !== null ? notesArr[index] : null
168+
function getNoteById(id) {
169+
const byId = () => {
170+
let note = null
171+
if (Array.isArray(notesArr)) {
172+
notesArr.forEach((val, index) => {
173+
if (val.id === id) note = val
174+
})
175+
}
176+
return note
177+
}
178+
return id !== null ? byId() : null
179+
}
180+
181+
/**функция получения индекса карточки по id */
182+
function getNoteIndexById(id) {
183+
const byId = () => {
184+
let index = null
185+
if (Array.isArray(notesArr)) {
186+
notesArr.forEach((val, ind) => {
187+
if (val.id === id) index = ind
188+
})
189+
}
190+
return index
191+
}
192+
return id !== null ? byId() : null
166193
}
167194

168195
///////////
@@ -186,7 +213,7 @@ function NotesPage() {
186213
/**рендер */
187214
return (
188215
/**Здесь отрисовываются меню добавления и редактирования заметок и сам перечнь заметок в виде динамичной отзывчивой сетки */
189-
<NotesContext.Provider value={{ addNote, removeNote, changeNoteColor, editNoteContent, editNoteOrder, setEditNoteId, unsetEditNoteId, editNoteId, getNoteByIndex }}>
216+
<NotesContext.Provider value={{ addNote, removeNote, changeNoteColor, editNoteContent, editNoteOrder, setEditNoteId, unsetEditNoteId, editNoteId, getNoteById }}>
190217
<div className="NotesPage">
191218
<main className="p-1 pb-3 mb-3">
192219
{/**Компонент добавления карточки и модальное окно редактирования */}

0 commit comments

Comments
 (0)