Visitar URL original
Fix errors · modularcode/modular-admin-react@d0b631e · GitHub
Skip to content

Commit d0b631e

Browse files
committed
Fix errors
1 parent ad80488 commit d0b631e

3 files changed

Lines changed: 46 additions & 19 deletions

File tree

‎src/_api/README.md‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,8 @@
33

44
Refs:
55
https://medium.com/@mranjan215/client-side-databases-34e3ec912015
6+
7+
8+
https://github.com/techfort/LokiJS
9+
https://github.com/typicode/lowdb
10+
https://github.com/louischatriot/nedb

‎src/_api/_mocksSW/db.ts‎

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import _ from 'lodash'
21
import Dexie from 'dexie'
32

43
import { User } from '../_types/User'
@@ -13,6 +12,8 @@ const usersData = createUsersData({ includeOrganizations: false })
1312
const usersToOrganizationsData = createUsersToOrganizationsData()
1413
const organizationsData = createOrganizationsData({ includeUsers: false })
1514

15+
console.log('usersData', usersData)
16+
1617
//
1718
// Declare Database
1819
//
@@ -24,10 +25,10 @@ class ModularAdminDatabase extends Dexie {
2425
public constructor() {
2526
super('ModularAdminDatabase')
2627
this.version(1).stores({
27-
organizations: '++id,name,username',
28+
organizations: '++id,name,username,updatedAt,createdAt',
2829
users:
29-
'++id,firstName,lastName,displayName,username,email,password,avatarUrl,globalRole,status',
30-
usersToOrganizations: '++id,organizationId,userId,role',
30+
'++id,firstName,lastName,displayName,username,email,password,avatarUrl,globalRole,status,updatedAt,createdAt',
31+
usersToOrganizations: '++id,organizationId,userId,role,updatedAt,createdAt',
3132
})
3233
this.organizations = this.table('organizations')
3334
this.users = this.table('users')
@@ -46,6 +47,7 @@ export const init = async function() {
4647
await db.users.clear()
4748
await db.usersToOrganizations.clear()
4849

50+
// Add the data
4951
await db.users.bulkAdd(usersData.list)
5052
await db.organizations.bulkAdd(organizationsData.list)
5153
await db.usersToOrganizations.bulkAdd(usersToOrganizationsData.list)

‎src/_api/_mocksSW/usersMocks.ts‎

Lines changed: 35 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import _ from 'lodash'
22
import { rest } from 'msw'
33

44
import config from '_config'
5+
import db from './db'
56
import { create as createUsersData } from '../_data/usersData'
67

78
const apiUrl = config.api.url
@@ -17,23 +18,42 @@ export default [
1718
let offset = parseInt(req.query.get('offset') || '10')
1819
let order = JSON.parse(req.query.get('order') || '{}')
1920

20-
const usersAll = order
21-
? _.orderBy(usersData.list, [order.orderBy], [order.order])
22-
: usersData.list
21+
try {
22+
let table = db.users
23+
let selection = table.toCollection()
2324

24-
const result = {
25-
users: usersAll.slice(offset, offset + limit),
26-
count: usersAll.length,
27-
}
25+
if (order && order.order === 'desc') {
26+
selection = table.orderBy(order.orderBy).reverse()
27+
} else if (order && order.order !== 'desc') {
28+
selection = table.orderBy(order.orderBy)
29+
}
2830

29-
return res(
30-
// Set custom status
31-
ctx.status(200),
32-
// Delay the response
33-
ctx.delay(500),
34-
// send JSON response body
35-
ctx.json(result),
36-
)
31+
selection = selection.limit(limit).offset(offset)
32+
33+
const users = await selection.toArray()
34+
const count = await selection.count()
35+
36+
return res(
37+
// Set custom status
38+
ctx.status(200),
39+
// Delay the response
40+
ctx.delay(500),
41+
// send JSON response body
42+
ctx.json({
43+
users,
44+
count,
45+
}),
46+
)
47+
} catch (err) {
48+
console.error(err)
49+
50+
return res(
51+
ctx.status(400),
52+
ctx.json({
53+
message: err.message,
54+
}),
55+
)
56+
}
3757
}),
3858

3959
rest.get(`${apiUrl}/users/:userId`, (req, res, ctx) => {

0 commit comments

Comments
 (0)