From 80ce967b08ea86d9eaed51851c6fb5cad445d9a5 Mon Sep 17 00:00:00 2001 From: Haitao Yue Date: Sat, 29 Jun 2019 00:12:48 +0800 Subject: [PATCH] [CE-626] Add mock data for users list api For users list api, add mock data. Change-Id: I634b3642392046dd5d818a071271e339779ff08e Signed-off-by: Haitao Yue --- src/dashboard/mock/user.js | 50 ++++++++++++++++++++++++++++++++++++++ src/dashboard/package.json | 3 ++- 2 files changed, 52 insertions(+), 1 deletion(-) diff --git a/src/dashboard/mock/user.js b/src/dashboard/mock/user.js index 7e4f6fb49..25757d655 100644 --- a/src/dashboard/mock/user.js +++ b/src/dashboard/mock/user.js @@ -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) { @@ -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') { diff --git a/src/dashboard/package.json b/src/dashboard/package.json index 5393cddf3..01837a8c6 100644 --- a/src/dashboard/package.json +++ b/src/dashboard/package.json @@ -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"