This repository has been archived by the owner on Sep 11, 2024. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 832
Create GroupSummaryStore for storing group summary stuff #1418
Merged
Merged
Changes from 2 commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -27,6 +27,8 @@ import AccessibleButton from '../views/elements/AccessibleButton'; | |
import Modal from '../../Modal'; | ||
import classnames from 'classnames'; | ||
|
||
import GroupSummaryStore from '../../stores/GroupSummaryStore'; | ||
|
||
const RoomSummaryType = PropTypes.shape({ | ||
room_id: PropTypes.string.isRequired, | ||
profile: PropTypes.shape({ | ||
|
@@ -76,8 +78,8 @@ const CategoryRoomList = React.createClass({ | |
if (!success) return; | ||
const errorList = []; | ||
Promise.all(addrs.map((addr) => { | ||
return MatrixClientPeg.get() | ||
.addRoomToGroupSummary(this.props.groupId, addr.address) | ||
return this.context.groupSummaryStore | ||
.addRoomToGroupSummary(addr.address) | ||
.catch(() => { errorList.push(addr.address); }) | ||
.reflect(); | ||
})).then(() => { | ||
|
@@ -153,8 +155,7 @@ const FeaturedRoom = React.createClass({ | |
onDeleteClicked: function(e) { | ||
e.preventDefault(); | ||
e.stopPropagation(); | ||
MatrixClientPeg.get().removeRoomFromGroupSummary( | ||
this.props.groupId, | ||
this.context.groupSummaryStore.removeRoomFromGroupSummary( | ||
this.props.summaryInfo.room_id, | ||
).catch((err) => { | ||
console.error('Error whilst removing room from group summary', err); | ||
|
@@ -242,8 +243,8 @@ const RoleUserList = React.createClass({ | |
if (!success) return; | ||
const errorList = []; | ||
Promise.all(addrs.map((addr) => { | ||
return MatrixClientPeg.get() | ||
.addUserToGroupSummary(this.props.groupId, addr.address) | ||
return this.context.groupSummaryStore | ||
.addUserToGroupSummary(addr.address) | ||
.catch(() => { errorList.push(addr.address); }) | ||
.reflect(); | ||
})).then(() => { | ||
|
@@ -317,8 +318,7 @@ const FeaturedUser = React.createClass({ | |
onDeleteClicked: function(e) { | ||
e.preventDefault(); | ||
e.stopPropagation(); | ||
MatrixClientPeg.get().removeUserFromGroupSummary( | ||
this.props.groupId, | ||
this.context.groupSummaryStore.removeUserFromGroupSummary( | ||
this.props.summaryInfo.user_id, | ||
).catch((err) => { | ||
console.error('Error whilst removing user from group summary', err); | ||
|
@@ -364,13 +364,32 @@ const FeaturedUser = React.createClass({ | |
}, | ||
}); | ||
|
||
const GroupSummaryContext = { | ||
groupSummaryStore: React.PropTypes.instanceOf(GroupSummaryStore).isRequired, | ||
}; | ||
|
||
CategoryRoomList.contextTypes = GroupSummaryContext; | ||
FeaturedRoom.contextTypes = GroupSummaryContext; | ||
RoleUserList.contextTypes = GroupSummaryContext; | ||
FeaturedUser.contextTypes = GroupSummaryContext; | ||
|
||
export default React.createClass({ | ||
displayName: 'GroupView', | ||
|
||
propTypes: { | ||
groupId: PropTypes.string.isRequired, | ||
}, | ||
|
||
childContextTypes: { | ||
groupSummaryStore: React.PropTypes.instanceOf(GroupSummaryStore), | ||
}, | ||
|
||
getChildContext: function() { | ||
return { | ||
groupSummaryStore: this._groupSummaryStore, | ||
}; | ||
}, | ||
|
||
getInitialState: function() { | ||
return { | ||
summary: null, | ||
|
@@ -411,12 +430,16 @@ export default React.createClass({ | |
}, | ||
|
||
_loadGroupFromServer: function(groupId) { | ||
MatrixClientPeg.get().getGroupSummary(groupId).done((res) => { | ||
this._groupSummaryStore = new GroupSummaryStore( | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Hmm, I'm assuming there's no references retained to the GroupSummaryStore so it will all GC nicely, but might be nice to explicitly remove the listener on the old one anyway. |
||
MatrixClientPeg.get(), this.props.groupId, | ||
); | ||
this._groupSummaryStore.on('update', () => { | ||
this.setState({ | ||
summary: res, | ||
summary: this._groupSummaryStore.getSummary(), | ||
error: null, | ||
}); | ||
}, (err) => { | ||
}); | ||
this._groupSummaryStore.on('error', (err) => { | ||
this.setState({ | ||
summary: null, | ||
error: err, | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
/* | ||
Copyright 2017 New Vector Ltd | ||
|
||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
|
||
http://www.apache.org/licenses/LICENSE-2.0 | ||
|
||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
import EventEmitter from 'events'; | ||
|
||
/** | ||
* Stores the group summary for a room and provides an API to change it | ||
*/ | ||
export default class GroupSummaryStore extends EventEmitter { | ||
constructor(matrixClient, groupId) { | ||
super(); | ||
this._groupId = groupId; | ||
this._matrixClient = matrixClient; | ||
this._summary = {}; | ||
this._fetchSummary(); | ||
} | ||
|
||
_fetchSummary() { | ||
this._matrixClient.getGroupSummary(this._groupId).then((resp) => { | ||
this._summary = resp; | ||
this._notifyListeners(); | ||
}).catch((err) => { | ||
this.emit('error', err); | ||
}); | ||
} | ||
|
||
_notifyListeners() { | ||
this.emit('update'); | ||
} | ||
|
||
getSummary() { | ||
return this._summary; | ||
} | ||
|
||
addRoomToGroupSummary(roomId, categoryId) { | ||
return this._matrixClient | ||
.addRoomToGroupSummary(this._groupId, roomId, categoryId) | ||
.then(this._fetchSummary.bind(this)); | ||
} | ||
|
||
addUserToGroupSummary(userId, roleId) { | ||
return this._matrixClient | ||
.addUserToGroupSummary(this._groupId, userId, roleId) | ||
.then(this._fetchSummary.bind(this)); | ||
} | ||
|
||
removeRoomFromGroupSummary(roomId) { | ||
return this._matrixClient | ||
.removeRoomFromGroupSummary(this._groupId, roomId) | ||
.then(this._fetchSummary.bind(this)); | ||
} | ||
|
||
removeUserFromGroupSummary(userId) { | ||
return this._matrixClient | ||
.removeUserFromGroupSummary(this._groupId, userId) | ||
.then(this._fetchSummary.bind(this)); | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This feels like a bit of misnomer now since what's is mostly doing is initialising the store