-
Notifications
You must be signed in to change notification settings - Fork 0
/
storage.js
51 lines (46 loc) · 877 Bytes
/
storage.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
let counter = 1
const data = [
{
id: 1,
text: 'text',
isDone: true
}
]
export function initCounter() {
if (data.length === 0) {
return
}
data.forEach(element => {
if (element.id >= counter) {
counter = element.id + 1
}
});
}
export function getData() {
return JSON.stringify(data)
}
export function createTask(inputData) {
const item = {
id: counter++,
...inputData
}
data.push(item)
return JSON.stringify(item)
}
export function changeStatus(id) {
const item = data.find(i => i.id === id)
if (item === undefined) {
// TODO 404
}
item.isDone = !item.isDone
return JSON.stringify(item)
}
export function deleteTask(id) {
const idx = data.findIndex(i => i.id === id)
if (idx === -1) {
console.log('Такого id не существует');
return
}
data.splice(idx, 1)
return 1
}