-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathindex.mjs
112 lines (89 loc) · 2.43 KB
/
index.mjs
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
import express from 'express';
import cors from 'cors';
const PORT = process.env.port || 8000;
const PROMPTS = [
'Issues with', 'How to build with', 'How to use',
'Trouble integrating', 'Need help fixing',
]
const TOPICS = [
'react-query', 'react-table', 'global filters',
'table sorting', 'expanding rows',
];
const USES = [
'for BI solution', 'for stats aggregation',
'for leaderboard', 'in reporting dashboard',
'in ecommerce app',
];
const STATUSES = ['open', 'resolved', 'locked']
const ONE = 1;
const TWO = 2;
const THREE = 3;
const FIVE = 5;
const TWELVE = 12;
const TEN = 10;
const TWENTY = 20;
const FIFTY = 50;
const ONE_SECOND = 1000;
let mockData = [];
let mockId = 0;
const app = express();
app.use(express.static('./build'));
app.use(cors());
app.listen(PORT, () => {
console.log(`[ index.mjs ] Listening on port ${PORT}`)
});
const getUniqueId = () => ++mockId;
const randomItem = list => list[Math.floor((Math.random() * list.length))];
const randomIndex = list => Math.floor((Math.random() * list.length));
const randomInt = max => Math.floor(Math.random() * (Math.floor(max) - TWO)) + ONE;
const getInitialMockData = () => {
const output = [];
for (const count of Array(TEN).keys()) {
output.push(
{
id: getUniqueId(),
name: `${randomItem(PROMPTS)} ${randomItem(TOPICS)} ${randomItem(USES)}`,
active: randomInt(FIFTY),
status: randomItem(STATUSES),
upvotes: randomInt(FIFTY)
}
)
}
return output;
}
const updateData = data => {
let output = [...data];
for (const count of Array(randomInt(THREE)).keys()) {
randomItem(output).active = randomInt(FIFTY);
randomItem(output).status = randomItem(STATUSES);
randomItem(output).upvotes += randomInt(TEN);
}
return output;
}
const generateRows = () => {
const output = [];
for (const count of Array(randomInt(FIVE)).keys()) {
output.push(
{
id: getUniqueId(),
name: `${randomItem(PROMPTS)} ${randomItem(TOPICS)} ${randomItem(USES)}`,
active: randomInt(FIFTY),
status: randomItem(STATUSES),
upvotes: randomInt(FIFTY)
}
)
}
return output;
}
mockData = getInitialMockData();
app.get('/api', (req, res, next) => {
setTimeout(() => {
mockData = updateData(mockData);
res.json(mockData);
}, ONE_SECOND);
});
app.get('/api/child', (req, res, next) => {
setTimeout(() => {
res.json(generateRows());
}, ONE_SECOND);
});