/
NotificationsContainer.js
170 lines (147 loc) · 4.64 KB
/
NotificationsContainer.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
/*
* Container component
* All data handling & manipulation should be handled here.
*/
import React, { Component } from 'react';
import { connect } from 'react-redux';
import { bindActionCreators } from 'redux';
import PropTypes from 'prop-types';
import {
getNotifications,
mountNotificationsTable,
unmountNotificationsTable,
updateNotifications
} from '../actions/notificationActions';
import NotificationsDetails from './components/NotificationsDetails';
import Modal from '../app/components/Modal';
class NotificationsContainer extends Component {
constructor (props) {
super(props);
this.state = {
fields: {
notifications: []
}
};
this._selectIdForModal = this._selectIdForModal.bind(this);
this._toggleCheck = this._toggleCheck.bind(this);
this._updateNotification = this._updateNotification.bind(this);
this._updateNotifications = this._updateNotifications.bind(this);
}
componentDidMount () {
this.props.getNotifications();
this.props.autoloadNotificationsEnable();
}
componentWillUnmount () {
this.props.autoloadNotificationsDisable();
}
_selectIdForModal (id) {
this.setState({
selectedId: id
});
}
_toggleCheck (key) {
const fieldState = { ...this.state.fields };
const index = fieldState.notifications.findIndex(notification => notification.id === key);
if (index < 0) {
fieldState.notifications.push({
id: key,
value: true
});
} else {
fieldState.notifications[index].value = !fieldState.notifications[index].value;
}
this.setState({
fields: fieldState
});
}
_updateNotification (id, value) {
const data = {
ids: [id],
...value
};
return this._updateNotificationsStatuses(data);
}
_updateNotifications (value) {
const data = {
ids: this.state.fields.notifications
.filter(notification => (notification.value))
.map(notification => notification.id),
...value
};
if (data.ids.length === 0) {
return false;
}
return this._updateNotificationsStatuses(data);
}
_updateNotificationsStatuses (data) {
return this.props.updateNotifications(data).then(() => {
const fieldState = { // reset checkboxes to unchecked
...this.state.fields,
notifications: this.state.fields.notifications.map(notification => ({
...notification,
value: false
}))
};
this.setState({
fields: fieldState
});
});
}
render () {
return ([
<NotificationsDetails
fields={this.state.fields}
isFetching={this.props.isFetching}
items={this.props.items}
key="notification-details"
selectIdForModal={this._selectIdForModal}
toggleCheck={this._toggleCheck}
updateNotification={this._updateNotification}
updateNotifications={this._updateNotifications}
/>,
<Modal
handleSubmit={() => this._updateNotifications({ isArchived: true })}
id="confirmArchive"
key="modal"
title="Confirm Delete"
>
Are you sure you want to delete the selected notifications?
</Modal>,
<Modal
handleSubmit={() => this._updateNotification(this.state.selectedId, { isArchived: true })}
id="confirmArchiveSingle"
key="modal-single"
title="Confirm Delete"
>
Are you sure you want to delete this notification?
</Modal>,
<Modal
handleSubmit={() => this._updateNotifications({ ids: 'all', isRead: true })}
id="confirmReadAll"
key="modal-read-all"
title="Confirm Mark all as Read"
>
Are you sure you want to mark all as read?
</Modal>
]);
}
}
NotificationsContainer.propTypes = {
items: PropTypes.arrayOf(PropTypes.shape()).isRequired,
isFetching: PropTypes.bool.isRequired,
updateNotifications: PropTypes.func.isRequired,
getNotifications: PropTypes.func.isRequired,
autoloadNotificationsEnable: PropTypes.func.isRequired,
autoloadNotificationsDisable: PropTypes.func.isRequired
};
const mapStateToProps = state => ({
isFetching: state.rootReducer.notifications.isFetching,
items: state.rootReducer.notifications.items
});
const mapDispatchToProps = dispatch => ({
updateNotifications: bindActionCreators(updateNotifications, dispatch),
getNotifications: bindActionCreators(getNotifications, dispatch),
autoloadNotificationsEnable: bindActionCreators(mountNotificationsTable, dispatch),
autoloadNotificationsDisable: bindActionCreators(unmountNotificationsTable, dispatch),
});
export default connect(mapStateToProps, mapDispatchToProps)(NotificationsContainer);