-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathLayoutContainer.js
More file actions
174 lines (155 loc) · 5.98 KB
/
LayoutContainer.js
File metadata and controls
174 lines (155 loc) · 5.98 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
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
import React, { Component } from 'react';
import PropTypes from 'prop-types';
import { connect } from 'react-redux';
import { IntlProvider } from 'react-intl';
import moment from 'moment';
import { setLang } from '../../redux/modules/app.js';
import { getLang, anyPendingFetchOperations } from '../../redux/selectors/app.js';
import { isLoggedIn } from '../../redux/selectors/auth.js';
import { getLoggedInUserSettings, getLoggedInUserUiData } from '../../redux/selectors/users.js';
import { groupsLoggedUserIsMemberSelector, fetchManyGroupsStatus } from '../../redux/selectors/groups.js';
import Layout from '../../components/layout/Layout';
import { messages } from '../../locales';
import { UserUIDataContext, LinksContext, UrlContext } from '../../helpers/contexts.js';
import { buildRoutes, getLinks, pathRelatedGroupSelector } from '../../pages/routes.js';
import withRouter, { withRouterProps } from '../../helpers/withRouter.js';
import { canUseDOM } from '../../helpers/common.js';
import 'admin-lte/dist/css/adminlte.min.css';
const ADDITIONAL_INTL_FORMATS = {
time: {
'24hour': { hour12: false, hour: 'numeric', minute: 'numeric' },
'24hourWithSeconds': {
hour12: false,
hour: 'numeric',
minute: 'numeric',
second: 'numeric',
},
},
};
/**
* Handles the dependency injection of the links.
* Also controls the state of the sidebar - collapsing and showing the sidebar.
*/
class LayoutContainer extends Component {
newPageLoading = false;
pageHeight = 0;
_scrollTargetToView() {
if ((window.location.hash || this.props.location.hash) && this.pageHeight !== document.body.scrollHeight) {
// this will enforce immediate scroll-to-view
window.location.hash = this.props.location.hash;
window.scrollBy({ top: -65, behavior: 'instant' }); // 65 is slightly more than LTE top-bar (which is 57px in height)
this.pageHeight = document.body.scrollHeight; // make sure we scroll only if the render height changes
}
}
setInitialSidebarState = open => {
const bootstrapCompactViewWidth = 991; // this may change with different versions of bootstrap
if (window && window.document.body.clientWidth > bootstrapCompactViewWidth) {
window.document.body.classList.remove(open === false ? 'sidebar-open' : 'sidebar-collapse');
window.document.body.classList.add(open === false ? 'sidebar-collapse' : 'sidebar-open');
}
};
componentDidMount() {
if (canUseDOM) {
this.setInitialSidebarState(this.props.userUIData.openedSidebar);
this.newPageLoading = true;
this.pageHeight = -1;
this._scrollTargetToView();
}
}
componentDidUpdate(prevProps) {
if (canUseDOM) {
if (
(prevProps.userUIData.openedSidebar === undefined && this.props.userUIData.openedSidebar !== undefined) ||
(prevProps.userUIData.openedSidebar !== undefined &&
prevProps.userUIData.openedSidebar !== this.props.userUIData.openedSidebar)
) {
this.setInitialSidebarState(this.props.userUIData.openedSidebar);
}
if (this.newPageLoading) {
this._scrollTargetToView();
}
}
if (!this.props.pendingFetchOperations) {
this.newPageLoading = false;
}
}
/**
* Get messages for the given language or the deafult - English
*/
getDefaultLang = () => {
const { userSettings } = this.props;
return userSettings && userSettings.defaultLanguage ? userSettings.defaultLanguage : 'en';
};
getMessages = lang => messages[lang] || messages[this.getDefaultLang()];
render() {
const {
lang,
location: { pathname, search },
isLoggedIn,
pendingFetchOperations,
userUIData,
setLang,
relatedGroupId,
memberGroups,
fetchManyGroupsStatus,
} = this.props;
moment.locale(lang);
return (
<IntlProvider locale={lang} messages={this.getMessages(lang)} formats={ADDITIONAL_INTL_FORMATS}>
<UserUIDataContext.Provider value={userUIData}>
<LinksContext.Provider value={getLinks()}>
<UrlContext.Provider value={{ lang }}>
<Layout
isLoggedIn={isLoggedIn}
lang={lang}
setLang={setLang}
availableLangs={Object.keys(messages)}
currentUrl={pathname}
pendingFetchOperations={pendingFetchOperations}
relatedGroupId={relatedGroupId}
memberGroups={memberGroups}
fetchManyGroupsStatus={fetchManyGroupsStatus}
colorTheme={userUIData.darkTheme ? 'dark' : 'light' /* TODO: we might want to add a new option for global color theme, 'darkTheme' was originally used only for text editors */}>
{buildRoutes(pathname + search, isLoggedIn)}
</Layout>
</UrlContext.Provider>
</LinksContext.Provider>
</UserUIDataContext.Provider>
</IntlProvider>
);
}
}
LayoutContainer.propTypes = {
lang: PropTypes.string,
setLang: PropTypes.func.isRequired,
pendingFetchOperations: PropTypes.bool,
isLoggedIn: PropTypes.bool,
sidebarIsCollapsed: PropTypes.bool,
sidebarIsOpen: PropTypes.bool,
location: withRouterProps.location,
userSettings: PropTypes.object,
userUIData: PropTypes.object,
relatedGroupId: PropTypes.string,
memberGroups: PropTypes.object.isRequired,
fetchManyGroupsStatus: PropTypes.string,
};
export default withRouter(
connect(
(state, { location: { pathname, search } }) => ({
lang: getLang(state),
isLoggedIn: isLoggedIn(state),
pendingFetchOperations: anyPendingFetchOperations(state),
userSettings: getLoggedInUserSettings(state),
userUIData: getLoggedInUserUiData(state),
relatedGroupId: pathRelatedGroupSelector(state, pathname + search),
memberGroups: groupsLoggedUserIsMemberSelector(state),
fetchManyGroupsStatus: fetchManyGroupsStatus(state),
}),
dispatch => ({
setLang: lang => {
dispatch(setLang(lang));
window.location.reload();
},
})
)(LayoutContainer)
);