-
Notifications
You must be signed in to change notification settings - Fork 51
Expand file tree
/
Copy pathusers.js
More file actions
98 lines (88 loc) · 2.32 KB
/
users.js
File metadata and controls
98 lines (88 loc) · 2.32 KB
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
/**
* Sidebar related redux actions
*/
import { fetchMemberProjects } from '../services/projects'
import {
LOAD_ALL_USER_PROJECTS_PENDING,
LOAD_ALL_USER_PROJECTS_SUCCESS,
LOAD_ALL_USER_PROJECTS_FAILURE,
SEARCH_USER_PROJECTS_PENDING,
SEARCH_USER_PROJECTS_SUCCESS,
SEARCH_USER_PROJECTS_FAILURE
} from '../config/constants'
import _ from 'lodash'
/**
* Loads projects of the authenticated user
*/
export function loadAllUserProjects (params, isAdmin = true, isManager = true) {
return (dispatch, getState) => {
dispatch({
type: LOAD_ALL_USER_PROJECTS_PENDING
})
const state = getState().users
const filters = {
status: 'active',
sort: 'lastActivityAt desc',
perPage: 20,
...params
}
if (!isAdmin && !isManager) {
filters['memberOnly'] = true
}
fetchMemberProjects(filters).then(({ projects, pagination }) => dispatch({
type: LOAD_ALL_USER_PROJECTS_SUCCESS,
projects: _.uniqBy((filters.page ? state.allUserProjects || [] : []).concat(projects), 'id'),
total: pagination.xTotal,
page: pagination.xPage
})).catch(() => dispatch({
type: LOAD_ALL_USER_PROJECTS_FAILURE
}))
}
}
export function loadNextProjects (isAdmin = true, isManager = true) {
return (dispatch, getState) => {
const { page, total, allUserProjects } = getState().users
if (allUserProjects.length >= total) {
return
}
loadAllUserProjects(_.assign({}, {
perPage: 20,
page: page + 1
}), isAdmin, isManager)(dispatch, getState)
}
}
/**
* Filter projects of the authenticated user
*
* @param {bool} isAdmin is admin
* @param {string} keyword search keyword
*/
export function searchUserProjects (isAdmin = true, keyword) {
return (dispatch) => {
if (!keyword) {
dispatch({
type: SEARCH_USER_PROJECTS_SUCCESS,
projects: []
})
return
}
dispatch({
type: SEARCH_USER_PROJECTS_PENDING
})
const filters = {
sort: 'updatedAt desc',
perPage: 20,
page: 1,
keyword: `"${keyword}"`
}
if (!isAdmin) {
filters['memberOnly'] = true
}
fetchMemberProjects(filters).then(({ projects }) => dispatch({
type: SEARCH_USER_PROJECTS_SUCCESS,
projects
})).catch(() => dispatch({
type: SEARCH_USER_PROJECTS_FAILURE
}))
}
}