Skip to content

Commit bfd0349

Browse files
authored
Merge pull request #15 from maxchistt/order
Order
2 parents fe33b73 + 7a583bf commit bfd0349

File tree

9 files changed

+110
-14
lines changed

9 files changed

+110
-14
lines changed

client/src/NoteComponents/ModalNoteEdit.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -140,23 +140,26 @@ function ModalNoteEdit() {
140140
<div>
141141
<Palette
142142
className="btn btn-light mx-1"
143+
style={{ boxShadow: "none" }}
143144
disabled={!note}
144145
setColor={tryChangeColor}
145146
></Palette>
146147
<button
147148
className="btn btn-light"
149+
style={{ boxShadow: "none" }}
148150
disabled={!note}
149151
onClick={tryRemove}
150152
><i className="bi bi-trash text-dark"></i></button>
151153
</div>
152154
{/**Индикатор номера заметки */}
153155
<div className="mx-auto">
154-
<span style={{ color: "lightgray", fontWeight: "400" }}>Id {editNoteId}</span>
156+
<span style={{ color: "lightgray", fontWeight: "400" }}>{note && note.order}</span>
155157
</div>
156158
{/**Зактрытие окна */}
157159
<div>
158160
<button
159161
className="btn btn-light"
162+
style={{ boxShadow: "none" }}
160163
onClick={tryClose}
161164
>Close</button>
162165
</div>

client/src/NoteComponents/NoteItem.js

Lines changed: 23 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,21 @@ 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
2727

28+
const footerBtn = {
29+
className: `btn btn-light p-0 text-secondary item-footer-btn`,
30+
style: {
31+
width: "1.8em", height: "1.8em", float: "right",
32+
borderColor: "transparent",
33+
backgroundColor: "transparent",
34+
boxShadow: "none"
35+
}
36+
}
37+
2838
return (
2939
<div className="p-1" >
3040
<div className="card" style={{ backgroundColor: bgColor }} >
@@ -41,14 +51,21 @@ function NoteItem({ note = new Note(), index }) {
4151
<ReactMarkdown remarkPlugins={[gfm]} children={fixLineBreaks(note.text)} />
4252
</div>
4353
</div>
44-
{/**Кнопка удаления */}
54+
{/**Кнопки изменения порядка */}
4555
<div className="card-body pt-0">
4656
<button
47-
className={`btn btn-light p-0`}
48-
style={{ width: "1.8em", height: "1.8em", float: "right", borderColor: "transparent", backgroundColor: "transparent" }}
49-
onClick={() => removeNote(index)}
57+
className={footerBtn.className}
58+
style={footerBtn.style}
59+
onClick={() => editNoteOrder(index, false)}
60+
>
61+
<i className="bi bi-chevron-compact-right"></i>
62+
</button>
63+
<button
64+
className={footerBtn.className}
65+
style={footerBtn.style}
66+
onClick={() => editNoteOrder(index, true)}
5067
>
51-
&#10007;
68+
<i className="bi bi-chevron-compact-left"></i>
5269
</button>
5370
</div>
5471
</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.css

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,3 +26,11 @@
2626
display: inline-block;
2727
transform: rotate(90deg);
2828
}
29+
30+
.item-footer-btn {
31+
transition: opacity 0.15s !important;
32+
opacity: 0.3 !important;
33+
}
34+
.item-footer-btn:hover {
35+
opacity: 1 !important;
36+
}

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 {number} index
146+
* @param {boolean} orderOperationFlag
147+
*/
148+
function editNoteOrder(index, orderOperationFlag) {
149+
if (notesArr[index]) {
150+
notesArr[index].order += orderOperationFlag ? 2 : -2
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

client/src/Shared/order.js

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
/**@file order.js */
2+
3+
/**
4+
* Callback for Array.Sort
5+
* сортировка карточек по параметру order
6+
* @param {object} a
7+
* @param {object} b
8+
*/
9+
export function sortByOrder(a, b) {
10+
if (a.order > b.order) return -1
11+
if (a.order < b.order) return 1
12+
return 0
13+
}
14+
15+
/**
16+
* Вычисление порядка новой заметки
17+
* @param {Array<object>} notesArr
18+
*/
19+
export function calcOrder(notesArr = []) {
20+
let order = 0
21+
notesArr.forEach((note) => {
22+
if (note.order >= order) order = note.order + 1
23+
})
24+
return order
25+
}
26+
27+
/**
28+
* Исправление параметров order в массиве заметок
29+
* @param {Array<object>} notesArr
30+
*/
31+
export function fixOrders(notesArr = []) {
32+
let fixedArr = notesArr
33+
const sortedArr = Array.isArray(notesArr) ? notesArr.sort(sortByOrder).reverse() : []
34+
sortedArr.forEach((sortedNote, sortedNoteIndex) => {
35+
fixedArr.forEach((note) => {
36+
if (note.id === sortedNote.id) note.order = sortedNoteIndex
37+
})
38+
})
39+
return fixedArr
40+
}

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)