Skip to content

Commit 7f7f5e1

Browse files
committed
Setup API and mocks for users and organizations
1 parent 4d1d95a commit 7f7f5e1

File tree

4 files changed

+122
-18
lines changed

4 files changed

+122
-18
lines changed

src/_api/_data/organizationsData.ts

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,17 @@
1+
import _keyBy from 'lodash/keyBy'
12
import Organization from '_types/Organization'
23
import organizationsToUsersData from './organizationsToUsersData'
34

4-
const organizationsDataById: { [key: number]: Organization } = {
5-
1: {
5+
const list: Organization[] = [
6+
{
67
id: 1,
78
name: 'ModularCode',
89
plan: {
910
id: 'silver',
1011
name: 'Silver',
1112
},
12-
// organizationToUsers: organizationsToUsersData.byOrganizationId[1],
1313
},
14-
2: {
14+
{
1515
id: 2,
1616
name: 'Cool LLC',
1717
plan: {
@@ -20,7 +20,7 @@ const organizationsDataById: { [key: number]: Organization } = {
2020
},
2121
// organizationToUsers: organizationsToUsersData.byOrganizationId[2],
2222
},
23-
3: {
23+
{
2424
id: 3,
2525
name: 'Other LLC',
2626
plan: {
@@ -29,10 +29,13 @@ const organizationsDataById: { [key: number]: Organization } = {
2929
},
3030
// organizationToUsers: organizationsToUsersData.byOrganizationId[3],
3131
},
32-
}
32+
]
33+
34+
const byId: { [key: number]: Organization } = _keyBy(list, 'id')
3335

3436
const organizationsData = {
35-
byId: organizationsDataById,
37+
list,
38+
byId,
3639
}
3740

3841
export default organizationsData

src/_api/_mocks/index.ts

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,14 @@
11
import { AxiosInstance } from 'axios'
22
import MockAdapter from 'axios-mock-adapter'
3-
import usersData from '../_data/usersData'
3+
4+
import usersMocks from './usersMocks'
5+
import organizationsMocks from './organizationsMocks'
46

57
const init = (instance: AxiosInstance) => {
68
const mock = new MockAdapter(instance, { delayResponse: 200 })
79

8-
mock.onGet('/users/profile').reply(200, {
9-
...usersData.current,
10-
})
11-
12-
mock.onGet('/users').reply(200, {
13-
users: {
14-
...usersData.list,
15-
},
16-
count: usersData.list.length,
17-
})
10+
usersMocks.init(mock)
11+
organizationsMocks.init(mock)
1812
}
1913

2014
export default {

src/_api/_mocks/organizationsMocks.ts

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
import MockAdapter from 'axios-mock-adapter'
2+
import organizationsData from '../_data/organizationsData'
3+
4+
export default {
5+
init(mock: MockAdapter) {
6+
mock.onGet('/organizations').reply(200, {
7+
organizations: {
8+
...organizationsData.list,
9+
},
10+
count: organizationsData.list.length,
11+
})
12+
13+
//
14+
mock.onGet(/\/organizations\/\d+/).reply((config: any) => {
15+
// console.log(config)
16+
const urlPaths = config.url.split('/')
17+
const organizationId = urlPaths[urlPaths.length - 1]
18+
const organization = organizationsData.byId[organizationId]
19+
20+
if (organization) {
21+
return [200, { ...organization }]
22+
} else {
23+
return [404, { message: 'Organization not found ' }]
24+
}
25+
})
26+
27+
mock.onPut(/\/organizations\/\d+/).reply((config: any) => {
28+
const urlPaths = config.url.split('/')
29+
const organizationId = urlPaths[urlPaths.length - 1]
30+
const organizationData = JSON.parse(config.data)
31+
const organization = organizationsData.byId[organizationId]
32+
33+
if (organization) {
34+
return [200, { ...organization, ...organizationData }]
35+
} else {
36+
return [403, { message: 'Update not permitted' }]
37+
}
38+
})
39+
40+
mock.onPost(/\/organizations/).reply((config: any) => {
41+
const organizationData = JSON.parse(config.data)
42+
43+
return [200, { id: 100, ...organizationData }]
44+
})
45+
46+
mock.onDelete(/\/organizations\/\d+/).reply((config: any) => {
47+
return [200, { message: 'Organization deleted' }]
48+
})
49+
},
50+
}

src/_api/_mocks/usersMocks.ts

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
import MockAdapter from 'axios-mock-adapter'
2+
import usersData from '../_data/usersData'
3+
4+
export default {
5+
init(mock: MockAdapter) {
6+
mock.onGet('/users/profile').reply(200, {
7+
...usersData.current,
8+
})
9+
10+
mock.onGet('/users').reply(200, {
11+
users: {
12+
...usersData.list,
13+
},
14+
count: usersData.list.length,
15+
})
16+
17+
//
18+
mock.onGet(/\/users\/\d+/).reply((config: any) => {
19+
// console.log(config)
20+
const urlPaths = config.url.split('/')
21+
const userId = urlPaths[urlPaths.length - 1]
22+
const user = usersData.byId[userId]
23+
24+
if (user) {
25+
return [200, { ...user }]
26+
} else {
27+
return [404, { message: 'User not found ' }]
28+
}
29+
})
30+
31+
mock.onPut(/\/users\/\d+/).reply((config: any) => {
32+
const urlPaths = config.url.split('/')
33+
const userId = urlPaths[urlPaths.length - 1]
34+
const userData = JSON.parse(config.data)
35+
const user = usersData.byId[userId]
36+
37+
if (user) {
38+
return [200, { ...user, ...userData }]
39+
} else {
40+
return [403, { message: 'Update not permitted' }]
41+
}
42+
})
43+
44+
mock.onPost(/\/users/).reply((config: any) => {
45+
const userData = JSON.parse(config.data)
46+
47+
console.log('config', config)
48+
console.log('userData', userData)
49+
50+
return [200, { id: 100, ...userData }]
51+
})
52+
53+
mock.onDelete(/\/users\/\d+/).reply((config: any) => {
54+
return [200, { message: 'User deleted' }]
55+
})
56+
},
57+
}

0 commit comments

Comments
 (0)