-
Notifications
You must be signed in to change notification settings - Fork 75
/
index.jsx
165 lines (150 loc) · 5.06 KB
/
index.jsx
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
import PropTypes from 'prop-types';
import React from 'react';
import counterpart from 'counterpart';
import Translate from 'react-translate-component';
import talkClient from 'panoptes-client/lib/talk-client';
import { Helmet } from 'react-helmet';
import Loading from '../../components/loading-indicator';
import NotificationSection from '../notifications/notification-section';
import CollapsableSection from '../../components/collapsable-section';
counterpart.registerTranslations('en', {
notifications: {
header: 'Notifications',
title: 'My Notifications',
signedOut: 'You\'re not signed in.',
noNotifications: 'You have no notifications.',
participation: 'You can receive notifications by participating in Talk, following discussions, and receiving messages.'
}
});
export default class NotificationsPage extends React.Component {
constructor(props) {
super(props);
this.onChildChanged = this.onChildChanged.bind(this);
this.state = {
projNotifications: [],
expanded: false
};
}
componentDidMount() {
if (this.props.user) {
this.getProjectNotifications();
}
}
componentWillReceiveProps(nextProps) {
if (nextProps.user !== null && nextProps.user !== this.props.user) {
this.getProjectNotifications();
}
}
onChildChanged(section) {
this.setState({ expanded: section });
}
getProjectNotifications() {
talkClient.type('notifications').get({ page: 1, page_size: 50 })
.then((projNotifications) => {
this.groupNotifications(projNotifications);
})
.then(() => {
if (this.props.project) this.setState({ expanded: `project-${this.props.project.id}` });
})
.catch((e) => {
console.error('Unable to load notifications', e);
});
}
groupNotifications(notifications) {
const projectSections = [];
const projectNotifications = [];
notifications.forEach((notification) => {
if (projectSections.indexOf(notification.section) < 0) {
if (notification.section === 'zooniverse') {
projectSections.unshift(notification.section);
projectNotifications.unshift(notification);
} else {
projectSections.push(notification.section);
projectNotifications.push(notification);
}
}
});
if (this.props.project && projectSections.indexOf(`project-${this.props.project.id}`) < 0) {
talkClient.type('notifications').get({ page: 1, page_size: 1, section: `project-${this.props.project.id}` })
.then(([notification]) => {
if (notification) {
projectNotifications.push(notification);
this.setState({ projNotifications: projectNotifications });
this.setState({ expanded: `project-${this.props.project.id}` });
}
});
}
this.setState({ projNotifications: projectNotifications });
}
renderNotifications() {
let notificationView;
if (this.state.projNotifications.length > 0) {
notificationView = (
<div>
<div className="list">
{this.state.projNotifications.map((notification, i) => {
const opened = notification.section === this.state.expanded || this.state.projNotifications.length === 1;
return (
<CollapsableSection key={i} callbackParent={this.onChildChanged} expanded={opened} section={notification.section}>
<NotificationSection
key={notification.id}
location={this.props.location}
projectID={notification.project_id}
slug={notification.project_slug}
user={this.props.user}
/>
</CollapsableSection>
);
})}
</div>
</div>
);
} else if (this.state.projNotifications.length === 0) {
notificationView = (
<div className="centering talk-module notifications-title">
<Translate content="notifications.noNotifications" />{' '}
<Translate content="notifications.participation" />
</div>
);
} else {
notificationView = <Loading />;
}
return notificationView;
}
render() {
let signedIn;
const headerStyle = this.props.project ? 'notifications-title talk-module' : 'notifications-title';
if (this.props.user) {
signedIn = this.renderNotifications();
} else {
signedIn = (
<div className="centering talk-module">
<Translate content="notifications.signedOut" />
</div>
);
}
return (
<div className="talk notifications">
<Helmet title={counterpart("notifications.header")} />
<div className="content-container">
<h3 className={headerStyle}>
<Translate content="notifications.title" />
</h3>
{signedIn}
</div>
</div>
);
}
}
NotificationsPage.propTypes = {
location: PropTypes.shape({
query: PropTypes.object
}),
project: PropTypes.shape({
id: PropTypes.string
}),
user: PropTypes.shape({
display_name: PropTypes.string,
login: PropTypes.string
})
};