@@ -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