Skip to content
This repository has been archived by the owner on Sep 11, 2024. It is now read-only.

Create GroupSummaryStore for storing group summary stuff #1418

Merged
merged 4 commits into from
Sep 25, 2017
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 34 additions & 11 deletions src/components/structures/GroupView.js
Original file line number Diff line number Diff line change
Expand Up @@ -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({
Expand Down Expand Up @@ -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(() => {
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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(() => {
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -411,12 +430,16 @@ export default React.createClass({
},

_loadGroupFromServer: function(groupId) {
Copy link
Member

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

MatrixClientPeg.get().getGroupSummary(groupId).done((res) => {
this._groupSummaryStore = new GroupSummaryStore(
Copy link
Member

Choose a reason for hiding this comment

The 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,
Expand Down
1 change: 1 addition & 0 deletions src/i18n/strings/en_EN.json
Original file line number Diff line number Diff line change
Expand Up @@ -876,6 +876,7 @@
"Which rooms would you like to add to this summary?": "Which rooms would you like to add to this summary?",
"Room name or alias": "Room name or alias",
"You are an administrator of this group": "You are an administrator of this group",
"Failed to add the following rooms to the summary of %(groupId)s:": "Failed to add the following rooms to the summary of %(groupId)s:",
"Failed to remove the room from the summary of %(groupId)s": "Failed to remove the room from the summary of %(groupId)s",
"The room '%(roomName)' could not be removed from the summary.": "The room '%(roomName)' could not be removed from the summary.",
"Failed to remove a user from the summary of %(groupId)s": "Failed to remove a user from the summary of %(groupId)s",
Expand Down
71 changes: 71 additions & 0 deletions src/stores/GroupSummaryStore.js
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));
}
}