/
AppStore.js
184 lines (154 loc) · 5.44 KB
/
AppStore.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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
import { observable, action } from "mobx";
import * as api from '../app/api/api';
import { prepareFromArr } from '../helpers/utils';
export const screens = {
MAIN: "main"
};
export class AppStore {
@observable currentScreen;
@observable items;
@observable voteId;
@observable isAddNewItem;
@observable currentBalance;
@observable currentAccount;
constructor() {
this.currentScreen = screens.MAIN;
this.items = null;
this.voteId = null;
this.isAddNewItem = null;
this.currentBalance = null;
}
@action("set currentScreen")
setCurrentScreen(currentScreen) {
this.currentScreen = currentScreen;
}
@action("put itemData")
putItems(itemObject = []) {
api.getItemIds()
.then((ids) => {
if (Array.isArray(ids) && ids.length > 0) {
ids.map((id) => id.toString()).forEach(id => {
api.getItem(id)
.then((item) => {
item = { ...prepareFromArr(item, 'item'), id };
api.getCurrentRank(item.id)
.then(rank => {
item.rank = rank.toString();
if (item.votingId !== '0') {
api.getVoting(item.votingId)
.then((vote) => {
vote = prepareFromArr(vote, 'voting');
const ids = [];
const promises = [];
item.movingsIds.forEach(movId => {
ids.push(movId);
promises.push(api.getMoving(movId));
});
Promise.all(promises)
.then((moving) => {
moving = moving.map((mov) => prepareFromArr(mov, 'mov'))
const itemObject = { ...item, id, vote, moving };
if (!this.items) {
this.items = [];
}
const index = this.items.findIndex((item) => item.id === id);
if (index !== -1) {
this.items[index] = { ...this.items[index], ...itemObject }
} else {
if (itemObject.length === 0) {
this.items = [];
} else {
this.items.push(itemObject)
}
}
// AppStore.putItems({ ...item, id, vote, moving });
})
.catch((error) => {
console.error(error);
});
})
.catch((error) => {
console.error(error);
});
} else {
const ids = [];
const promises = [];
item.movingsIds.forEach(movId => {
ids.push(movId);
promises.push(api.getMoving(movId));
});
Promise.all(promises)
.then((moving) => {
moving = moving.map((mov) => prepareFromArr(mov, 'mov'))
const itemObject = { ...item, id, moving };
if (!this.items) {
this.items = [];
}
const index = this.items.findIndex((item) => item.id === id);
if (index !== -1) {
this.items[index] = { ...this.items[index], ...itemObject }
} else {
if (itemObject.length === 0) {
this.items = [];
} else {
this.items.push(itemObject)
}
}
})
.catch((error) => {
console.error(error);
});
}
});
})
.catch((error) => {
console.error(error);
});
});
} else {
const itemObject = [];
if (!this.items) {
this.items = [];
}
const index = this.items.findIndex((item) => item.id === id);
if (index !== -1) {
this.items[index] = { ...this.items[index], ...itemObject }
} else {
if (itemObject.length === 0) {
this.items = [];
} else {
this.items.push(itemObject)
}
}
}
})
.catch((error) => {
console.error(error);
})
};
@action("open modal vote")
vote(voteId) {
this.voteId = voteId;
}
@action("open modal add new item")
addNewItem() {
this.isAddNewItem = true;
}
@action("close modal add new item")
closeModalAddNewItem() {
this.isAddNewItem = null;
}
@action("close modal vote")
closeModalVote() {
this.voteId = null;
}
@action("set balance")
setBalance(balance) {
this.currentBalance = balance;
}
@action("set account")
setAccount(account) {
this.currentAccount = account;
}
}
export default new AppStore();