Skip to content

Commit 53f16e8

Browse files
committed
make ordered notes
1 parent ae19bbc commit 53f16e8

File tree

7 files changed

+47
-12
lines changed

7 files changed

+47
-12
lines changed

client/src/NoteComponents/ModalNoteEdit.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,7 @@ function ModalNoteEdit() {
151151
</div>
152152
{/**Индикатор номера заметки */}
153153
<div className="mx-auto">
154-
<span style={{ color: "lightgray", fontWeight: "400" }}>Id {editNoteId}</span>
154+
<span style={{ color: "lightgray", fontWeight: "400" }}>{note && note.order}</span>
155155
</div>
156156
{/**Зактрытие окна */}
157157
<div>

client/src/NoteComponents/NoteItem.js

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ function fixLineBreaks(mdStr) {
2020
*/
2121
function NoteItem({ note = new Note(), index }) {
2222
/**Подключение контекста */
23-
const { removeNote, setEditNoteId } = useContext(NotesContext)
23+
const { setEditNoteId, editNoteOrder } = useContext(NotesContext)
2424

2525
const lineClip = 12
2626
const bgColor = note.color
@@ -41,14 +41,21 @@ function NoteItem({ note = new Note(), index }) {
4141
<ReactMarkdown remarkPlugins={[gfm]} children={fixLineBreaks(note.text)} />
4242
</div>
4343
</div>
44-
{/**Кнопка удаления */}
44+
{/**Кнопки изменения порядка */}
4545
<div className="card-body pt-0">
4646
<button
4747
className={`btn btn-light p-0`}
4848
style={{ width: "1.8em", height: "1.8em", float: "right", borderColor: "transparent", backgroundColor: "transparent" }}
49-
onClick={() => removeNote(index)}
49+
onClick={() => editNoteOrder(index, Number(note.order) - 1)}
5050
>
51-
&#10007;
51+
&#10097;
52+
</button>
53+
<button
54+
className={`btn btn-light p-0`}
55+
style={{ width: "1.8em", height: "1.8em", float: "right", borderColor: "transparent", backgroundColor: "transparent" }}
56+
onClick={() => editNoteOrder(index, Number(note.order) + 1)}
57+
>
58+
&#10096;
5259
</button>
5360
</div>
5461
</div>

client/src/NoteComponents/NoteList.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import PropTypes from 'prop-types'
66
import NoteItem from './NoteItem'
77
import StackGrid, { transitions } from "react-stack-grid"
88
import sizeMe from 'react-sizeme'
9+
import { sortByOrder } from '../Shared/order'
910
const { scaleDown } = transitions
1011

1112
/**расчет ширины столбцов */
@@ -53,7 +54,7 @@ function NoteList(props) {
5354
{/**Отзывчивая сетка карточек */}
5455
<StackGrid className="container p-0" {...gridSettings}>
5556
{/**Рендер каждой карточки из массива */}
56-
{props.notes.map ? (props.notes.map((note, index) => {
57+
{props.notes.map ? (props.notes.sort(sortByOrder).map((note, index) => {
5758
return (
5859
<NoteItem note={note} key={note.id} index={index} />
5960
)

client/src/Pages/NotesPage.js

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import useDataLoadingController from '../Hooks/useDataLoadingController.hook'
1616
import useFetchNotes from '../Hooks/useFetchNotes.hook'
1717
import useEditNoteId from '../Hooks/useEditNoteId.hook'
1818
import useNotesArr from '../Hooks/useNotesArr.hook'
19+
import { calcOrder, fixOrders } from '../Shared/order'
1920

2021
/**
2122
* Страница с заметками
@@ -95,7 +96,13 @@ function NotesPage() {
9596
*/
9697
function addNote(noteData = {}) {
9798
const newId = String(auth.email) + String(Date.now()) + String(Math.random())
98-
const newNote = new Note({ id: newId, name: noteData.name, color: noteData.color, text: noteData.text })
99+
const newNote = new Note({
100+
id: newId,
101+
name: noteData.name,
102+
color: noteData.color,
103+
text: noteData.text,
104+
order: calcOrder(notesArr)
105+
})
99106
//console.log(newId, newNote.id);
100107
const newIndex = (notesArr != null) ? notesArr.length : 0
101108
setNotesArr(
@@ -133,6 +140,22 @@ function NotesPage() {
133140
loadDataToServer(notesArr[index], "set")
134141
}
135142

143+
/**
144+
* Изменение порядка заметки
145+
* @param {*} index
146+
* @param {*} order
147+
*/
148+
function editNoteOrder(index, order) {
149+
if (notesArr[index]) {
150+
notesArr[index].order = order
151+
let fixedArr = fixOrders(notesArr)
152+
setNotesArr(fixedArr)
153+
fixedArr.forEach((note) => {
154+
loadDataToServer(note, "set")
155+
})
156+
}
157+
}
158+
136159
/**функция получения карточки по id */
137160
function getNoteByIndex(index) {
138161
return index !== null ? notesArr[index] : null
@@ -159,7 +182,7 @@ function NotesPage() {
159182
/**рендер */
160183
return (
161184
/**Здесь отрисовываются меню добавления и редактирования заметок и сам перечнь заметок в виде динамичной отзывчивой сетки */
162-
<NotesContext.Provider value={{ addNote, removeNote, changeNoteColor, editNoteContent, setEditNoteId, unsetEditNoteId, editNoteId, getNoteByIndex }}>
185+
<NotesContext.Provider value={{ addNote, removeNote, changeNoteColor, editNoteContent, editNoteOrder, setEditNoteId, unsetEditNoteId, editNoteId, getNoteByIndex }}>
163186
<div className="NotesPage">
164187
<main className="p-1 pb-3 mb-3">
165188
{/**Компонент добавления карточки и модальное окно редактирования */}

client/src/Shared/noteType/Note.js

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ export const PropTypeNote = PropTypes.shape({
99
name: PropTypes.string,
1010
color: PropTypes.string,
1111
text: PropTypes.string,
12+
order: PropTypes.number,
1213
})
1314

1415
/**валидация заметки */
@@ -17,7 +18,8 @@ export function checkNote(note) {
1718
(typeof note.id === "string") &&
1819
typeof note.name === "string" &&
1920
typeof note.color === "string" &&
20-
typeof note.text === "string"
21+
typeof note.text === "string" &&
22+
(typeof note.order === "number" || typeof note.order === "undefined")
2123
)
2224
}
2325

@@ -37,11 +39,12 @@ export function checkNotesArr(notesArr) {
3739

3840
/**класс заметки */
3941
export class Note {
40-
constructor({ id, name, color, text }) {
42+
constructor({ id, name, color, text, order }) {
4143
this.id = String(id)
4244
this.name = String(name)
4345
this.color = String(color)
4446
this.text = String(text)
47+
this.order = Number(order)
4548
}
4649
}
4750

server/models/Note.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ const schema = new Schema({
1212
text: { type: String },
1313
color: { type: String },
1414
image: { type: String },
15+
order: { type: Number },
1516
owner: { type: Types.ObjectId, ref: 'User', required: true }
1617
})
1718

server/validation/NoteCheck.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,14 @@
55
/**
66
* Проверка рбьекта заметки
77
* @param {*} note
8-
*
98
*/
109
function checkNote(note) {
1110
return (
1211
typeof note.id === "string" &&
1312
typeof note.name === "string" &&
1413
typeof note.color === "string" &&
15-
typeof note.text === "string"
14+
typeof note.text === "string" &&
15+
(typeof note.order === "number" || typeof note.order === "undefined")
1616
)
1717
}
1818

0 commit comments

Comments
 (0)