Skip to content

Commit

Permalink
[CE-626] Add mock data for users list api
Browse files Browse the repository at this point in the history
For users list api, add mock data.

Change-Id: I634b3642392046dd5d818a071271e339779ff08e
Signed-off-by: Haitao Yue <[email protected]>
  • Loading branch information
hightall committed Jun 28, 2019
1 parent 57f12ab commit 80ce967
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 1 deletion.
50 changes: 50 additions & 0 deletions src/dashboard/mock/user.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,45 @@
import Mock from 'mockjs';
import faker from 'faker';

function paginator(items, page, per_page) {

const offset = (page - 1) * per_page,
paginatedItems = items.slice(offset).slice(0, per_page),
total_pages = Math.ceil(items.length / per_page);

return {
page: page,
per_page: per_page,
pre_page: page - 1 ? page - 1 : null,
next_page: (total_pages > page) ? page + 1 : null,
total: items.length,
total_pages: total_pages,
data: paginatedItems
};
}

const users = Mock.mock({
'data|10': [{
id: function () {
return Mock.Random.guid()
},
username: '@name',
role: function () {
return Mock.Random.pick(['administrator', 'user'])
},
'organization|1': [
{
id: function () {
return Mock.Random.guid()
},
name: function () {
return faker.company.companyName()
}
},
]
}],
});

function tokenVerify(req, res) {
const { token } = req.body;
switch (token) {
Expand Down Expand Up @@ -29,6 +71,14 @@ function tokenVerify(req, res) {
}
export default {
'POST /api/token-verify': tokenVerify,
'/api/users': (req, res) => {
const { page = 1, per_page = 10 } = req.query;
const result = paginator(users.data, parseInt(page), parseInt(per_page));
res.send({
total: result.total,
data: result.data,
});
},
'POST /api/auth': (req, res) => {
const { password, username, type } = req.body;
if (password === 'pass' && username === 'admin') {
Expand Down
3 changes: 2 additions & 1 deletion src/dashboard/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,8 @@
"tslint": "^5.12.1",
"tslint-config-prettier": "^1.17.0",
"tslint-react": "^3.6.0",
"umi-plugin-ga": "^1.1.3"
"umi-plugin-ga": "^1.1.3",
"faker": "^4.1.0"
},
"optionalDependencies": {
"puppeteer": "^1.12.1"
Expand Down

0 comments on commit 80ce967

Please sign in to comment.