/
index.js
211 lines (185 loc) · 6.01 KB
/
index.js
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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
import React, { Component } from "react";
import PropTypes from "prop-types";
import Layout from "frontend/components/layout";
import ProjectList from "frontend/components/project-list";
import Utility from "global/components/utility";
import connectAndFetch from "utils/connectAndFetch";
import { entityStoreActions } from "actions";
import { select, meta } from "utils/entityUtils";
import { projectsAPI, requests } from "api";
import get from "lodash/get";
import queryString from "query-string";
import omitBy from "lodash/omitBy";
import debounce from "lodash/debounce";
import withSettings from "hoc/with-settings";
const { request } = entityStoreActions;
const defaultPage = 1;
const perPage = 20;
export class ProjectsContainer extends Component {
static fetchData = (getState, dispatch, location) => {
const search = queryString.parse(location.search);
const baseFilters = {
standaloneModeEnforced: false
};
const { page, ...filters } = search;
const pagination = {
number: page || defaultPage,
size: perPage
};
const projectsFetch = projectsAPI.index(
Object.assign(baseFilters, filters),
pagination
);
const projectsAction = request(projectsFetch, requests.feProjectsFiltered);
const { promise: one } = dispatch(projectsAction);
const promises = [one];
return Promise.all(promises);
};
static mapStateToProps = state => {
return {
projects: select(requests.feProjectsFiltered, state.entityStore),
subjects: select(requests.feSubjects, state.entityStore),
projectsMeta: meta(requests.feProjectsFiltered, state.entityStore),
authentication: state.authentication
};
};
static propTypes = {
authentication: PropTypes.object,
projects: PropTypes.array,
location: PropTypes.object,
history: PropTypes.object,
dispatch: PropTypes.func,
fetchData: PropTypes.func.isRequired,
subjects: PropTypes.array,
projectsMeta: PropTypes.object,
settings: PropTypes.object
};
static defaultProps = {
location: {}
};
constructor(props) {
super(props);
this.state = this.initialState(queryString.parse(props.location.search));
this.updateResults = debounce(this.updateResults.bind(this), 250);
}
componentDidUpdate(prevProps) {
if (
this.props.authentication.currentUser !==
prevProps.authentication.currentUser
) {
this.props.fetchData(this.props);
}
}
get hasVisibleProjects() {
return get(this.props.settings, "attributes.calculated.hasVisibleProjects");
}
initialState(init) {
const filter = omitBy(init, (vIgnored, k) => k === "page");
return {
filter: { ...filter },
pagination: {
number: init.page || defaultPage,
size: perPage
}
};
}
updateUrl() {
const pathname = this.props.location.pathname;
const filters = this.state.filter;
const pageParam = this.state.pagination.number;
const params = { ...filters };
if (pageParam !== 1) params.page = pageParam;
const search = queryString.stringify(params);
this.props.history.push({ pathname, search });
}
updateResults(filter = this.state.filter) {
const action = request(
projectsAPI.index(filter, this.state.pagination),
requests.feProjectsFiltered
);
this.props.dispatch(action);
}
doUpdate() {
this.updateResults();
this.updateUrl();
}
filterChangeHandler = filter => {
this.setState({ filter }, this.doUpdate);
};
handlePageChange = pageParam => {
const pagination = { ...this.state.pagination, number: pageParam };
this.setState({ pagination }, this.doUpdate);
};
pageChangeHandlerCreator = pageParam => {
return event => {
event.preventDefault();
this.handlePageChange(pageParam);
};
};
showPlaceholder() {
const { location, projects } = this.props;
if (location.search) return false; // There are search filters applied, skip the check
if (!projects || projects.length === 0) return true;
}
renderProjectLibrary() {
if (this.showPlaceholder()) return <ProjectList.Placeholder />;
return (
<section className="bg-neutral05">
<div className="entity-section-wrapper container">
<header className="entity-section-wrapper__heading section-heading">
<div className="main">
<Utility.IconComposer size={56} icon="projects64" />
<div className="body">
<h2 className="title">{"All Projects"}</h2>
</div>
</div>
</header>
<ProjectList.Filters
filterChangeHandler={this.filterChangeHandler}
initialFilterState={this.state.filter}
subjects={this.props.subjects}
/>
<div className="entity-section-wrapper__details">
<Utility.EntityCount
pagination={get(this.props.projectsMeta, "pagination")}
singularUnit="project"
pluralUnit="projects"
countOnly
/>
</div>
<ProjectList.Grid
authenticated={this.props.authentication.authenticated}
favorites={get(this.props.authentication, "currentUser.favorites")}
dispatch={this.props.dispatch}
projects={this.props.projects}
pagination={get(this.props.projectsMeta, "pagination")}
paginationClickHandler={this.pageChangeHandlerCreator}
limit={perPage}
/>
</div>
</section>
);
}
render() {
if (!this.props.projectsMeta) return null;
return (
<div
style={{
overflowX: "hidden"
}}
>
<h1 className="screen-reader-text">All Projects</h1>
{this.renderProjectLibrary()}
{this.hasVisibleProjects && (
<Layout.ButtonNavigation
showProjectCollections
showFollowing={false}
showProjects={false}
grayBg={false}
/>
)}
</div>
);
}
}
export default connectAndFetch(withSettings(ProjectsContainer));