-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
Copy pathapi.dev.js
153 lines (144 loc) · 4.39 KB
/
api.dev.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
let usedCurrencies = {
influence: 100,
goldenInfluence: 100
};
let totalCurrencies = {
influence: 1000,
goldenInfluence: 300
};
let lists = {
todo: {
possibleVotes: [
{
name: "influence",
currency: "influence",
score: 1,
color: "blue"
},
{
name: "golden",
currency: "goldenInfluence",
score: 1,
color: "#bfa203"
}
],
items: [
{ id: "1234", list: "todo", title: "Finish up MVP documentation", description: "Take care for the remaining issues in the webpack.js.org repo which are relevant for the MVP.", influence: 15 },
{ id: "2345", list: "todo", title: "Review whole documentation", description: "Read over **all** of the documentation to find errors.", golden: 20 },
]
}
};
let allItems = {
"1234": lists.todo.items[0],
"2345": lists.todo.items[1],
};
function delay(time) {
return new Promise(function (fulfill) {
setTimeout(fulfill, time);
});
}
function clone(json) {
return JSON.parse(JSON.stringify(json));
}
export function isLoginActive() {
return /^\?login=/.test(window.location.search);
}
export function startLogin(callbackUrl) {
window.location.search = "?login=" + encodeURIComponent(callbackUrl);
return Promise.resolve();
}
export function continueLogin() {
if(/^\?login=/.test(window.location.search)) {
return delay(2000).then(() => {
setTimeout(() => window.location = decodeURIComponent(window.location.search.substr(7), 100));
return "developer";
});
}
return Promise.resolve();
}
export function getSelf(token) {
if(token !== "developer")
return Promise.reject(new Error("Not logged in as developer"));
return delay(500).then(() => ({
login: "dev",
name: "Developer",
avatar: "https://github.com/webpack.png",
currencies: [
{ name: "influence", displayName: "Influence", description: "Some **description**", value: totalCurrencies.influence, used: usedCurrencies.influence, remaining: totalCurrencies.influence - usedCurrencies.influence },
{ name: "goldenInfluence", displayName: "Golden Influence", description: "Some **description**", value: totalCurrencies.goldenInfluence, used: usedCurrencies.goldenInfluence, remaining: totalCurrencies.goldenInfluence - usedCurrencies.goldenInfluence }
]
}));
}
export function getList(token, name) {
const loggedIn = token === "developer";
const listData = lists[name];
return delay(500).then(() => ({
name: name,
displayName: "DEV: " + name,
description: "Some **description**",
lockable: true,
deletable: true,
archivable: true,
isAdmin: true,
possibleVotes: listData.possibleVotes,
items: lists[name].items.map(item => {
const votes = listData.possibleVotes.map(pv => ({
name: pv.name,
votes: (item[pv.name] || 0) + Math.floor(Math.random() * 100)
}));
const score = listData.possibleVotes.map((pv, i) => {
return pv.score * votes[i].votes;
}).reduce((a, b) => a + b, 0);
return {
id: item.id,
list: item.list,
title: item.title,
description: item.description,
votes,
userVotes: loggedIn ? listData.possibleVotes.map(pv => ({
name: pv.name,
votes: item[pv.name] || 0
})) : undefined,
score
};
}).sort((a, b) => b.score - a.score)
}));
}
export function createItem(token, list, title, description) {
if(token !== "developer")
return Promise.reject(new Error("Not logged in as developer"));
let newItem = {
id: Math.random() + "",
list,
title,
description
};
allItems[newItem.id] = newItem;
lists[list].items.push(newItem);
return delay(500).then(() => ({
...newItem,
votes: lists[list].possibleVotes.map(pv => ({
name: pv.name,
votes: 0
})),
userVotes: lists[list].possibleVotes.map(pv => ({
name: pv.name,
votes: 0
})),
score: 0
}));
}
export function vote(token, itemId, voteName, value) {
if(token !== "developer")
return Promise.reject(new Error("Not logged in as developer"));
var listId = allItems[itemId].list;
let listData = lists[listId];
let pv = listData.possibleVotes.filter(pv => pv.name === voteName)[0];
if(pv.currency) {
usedCurrencies[pv.currency] += value;
}
allItems[itemId][voteName] = (allItems[itemId][voteName] || 0) + value;
return delay(500).then(() => ({
ok: true
}));
}