Skip to content
This repository has been archived by the owner on Oct 1, 2019. It is now read-only.

Commit

Permalink
temp
Browse files Browse the repository at this point in the history
  • Loading branch information
voidxnull committed Apr 3, 2017
1 parent 043385b commit 2ab53c2
Show file tree
Hide file tree
Showing 6 changed files with 94 additions and 31 deletions.
22 changes: 17 additions & 5 deletions src/actions/user-messages.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,35 +15,47 @@
You should have received a copy of the GNU Affero General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

export const VISIT_USER_MESSAGES = 'VISIT_USER_MESSAGES';
export const SEND_USER_MESSAGE = 'SEND_USER_MESSAGE';
export const SET_USER_MESSAGES = 'SET_USER_MESSAGES';
export const REMOVE_USER_MESSAGE = 'REMOVE_USER_MESSAGE';
export const UPDATE_USER_MESSAGE = 'UPDATE_USER_MESSAGE';
export const UPDATE_USER_MESSAGE_COUNTERS = 'UPDATE_USER_MESSAGE_COUNTERS';

export function visitUserMessages(userId, messages) {
export function setUserMessages(userId, messages) {
return {
type: VISIT_USER_MESSAGES,
type: SET_USER_MESSAGES,
payload: {
userId,
messages
}
};
}

export function sendUserMessage(userId, message) {
return {
type: SEND_USER_MESSAGE,
payload: {
userId,
message
}
};
}

export function removeUserMessage(userId, messageId) {
return {
type: REMOVE_USER_MESSAGE,
payload: {
userId,
messageId
}
};
}

export function updateUserMessage(message) {
export function updateUserMessage(userId, message) {
return {
type: UPDATE_USER_MESSAGE,
payload: {
userId,
message
}
};
Expand Down
4 changes: 2 additions & 2 deletions src/api/client.js
Original file line number Diff line number Diff line change
Expand Up @@ -492,12 +492,12 @@ export default class ApiClient
return await response.json();
}

async updateMessage(messageId: UserMessageId, text: string): Promise<UserMessage> {
async updateUserMessage(messageId: UserMessageId, text: string): Promise<UserMessage> {
const response = await this.postJSON(`/api/v1/user-message/${messageId}`, { text });
return await response.json();
}

async deleteMessage(messageId: UserMessageId): Promise<Success> {
async deleteUserMessage(messageId: UserMessageId): Promise<Success> {
const response = await this.del(`/api/v1/user-message/${messageId}`);
return await response.json();
}
Expand Down
14 changes: 13 additions & 1 deletion src/components/conversation.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,22 @@
You should have received a copy of the GNU Affero General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
import React from 'react';
import React, { PropTypes } from 'react';

import ProfilePost from './bio/post';


export default class Conversation extends React.Component {
static propTypes = {
onDelete: PropTypes.func,
onUpdate: PropTypes.func
};

static defaultProps = {
onDelete: () => { },
onUpdate: () => { }
};

handleSubmit = (e) => {
e.preventDefault();
this.props.onSend(this.props.selectedUser.get('id'), this.form.text.value);
Expand Down Expand Up @@ -48,6 +58,8 @@ export default class Conversation extends React.Component {
hideAvatar={hideAvatar}
post={message}
author={author}
onUpdate={this.props.onUpdate}
onDelete={this.props.onDelete}
/>
</div>
);
Expand Down
15 changes: 13 additions & 2 deletions src/pages/conversations.js
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ class ConversationsPage extends React.Component {

const triggers = new ActionsTrigger(client, store.dispatch);
const firstUserId = users[0].id;
await triggers.updateUserMessages(firstUserId);
await triggers.loadUserMessages(firstUserId);
}
}

Expand Down Expand Up @@ -118,6 +118,15 @@ class ConversationsPage extends React.Component {
this.triggers.sendMessage(this.state.selectedUserId, text);
};

handleUpdateMessage = async (messageId, text) => {
this.triggers.updateMessage('TODO', messageId, text);
};

handleDeleteMessage = async (message, author) => {
console.log(message.toJS(), author.toJS())
this.triggers.deleteMessage(author.get('id'), message.get('id'));
};

updateMessages = async (selectedUserId) => {
clearTimeout(this.timeoutId);

Expand All @@ -138,7 +147,7 @@ class ConversationsPage extends React.Component {

const currentUser = users.get(current_user.get('id'));
const selectedUser = users.get(this.state.selectedUserId);
const selectedUserMessages = user_messages.get(this.state.selectedUserId) || i.List();
const selectedUserMessages = user_messages.getIn(['byUser', this.state.selectedUserId, 'messages']) || i.List();


const followedUsers = conversations_river.map(id => users.get(id));
Expand Down Expand Up @@ -180,6 +189,8 @@ class ConversationsPage extends React.Component {
current_user={current_user}
messages={selectedUserMessages}
users={users}
onUpdate={this.handleUpdateMessage}
onDelete={this.handleDeleteMessage}
/>
<div className="bio__river-item bio__river-item--type_form">
<RiverItemCreateForm
Expand Down
44 changes: 27 additions & 17 deletions src/store/user_messages.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,39 +22,49 @@ import { userMessages } from '../actions';

export const initialState = i.fromJS({
numUnread: 0,
user_messages: {}, // all messages by id
byUser: {} // userId => { numUnread: 0, messagesIds: [ids] }
byUser: {} // userId => { numUnread: 0, messages: [] }
});

export function reducer(state = initialState, action) {
switch (action.type) {
case userMessages.VISIT_USER_MESSAGES: {
case userMessages.SET_USER_MESSAGES: {
state = state.withMutations(state => {
for (const message of action.messages) {
state.setIn(['user_messages', message.id], i.fromJS(message));
}

state.update('byUser', byUser => {
byUser.setIn([action.userId, 'messageIds'], i.List(action.messages.map(m => m.id)));
});
state.setIn(
['byUser', action.payload.userId, 'messages'],
i.fromJS(action.payload.messages)
);
});

break;
}

case userMessages.SEND_USER_MESSAGE: {
state = state.updateIn(['byUser', action.payload.userId, 'messages'], messages => (
(messages || i.List()).push(i.fromJS(action.payload.message))
));

break;
}

case userMessages.UPDATE_USER_MESSAGE: {
state = state.mergeIn(['user_messages', action.message.id], i.fromJS(action.message));
const index = state.getIn(['byUser', action.payload.userId, 'messages'])
.findIndex(m => m.get('id') === action.payload.message.id);

if (index > -1) {
state = state.udpateIn(
['byUser', action.payload.userId, 'messages', index],
message => message.mergeDeep(action.payload.message)
);
}

break;
}

case userMessages.REMOVE_USER_MESSAGE: {
state = state.withMutations(state => {
state.deleteIn(['user_messages', action.message.id]);
state.updateIn(['byUser', action.userId, 'messageIds'], messages => (
messages.filterNot(m => m.get('id') === action.messageId)
));
});
console.log(action, state.getIn(['byUser', action.payload.userId, 'messages']))
state = state.updateIn(['byUser', action.payload.userId, 'messages'], messages => (
messages.filterNot(m => m.get('id') === action.payload.messageId) // fixme: broken
));

break;
}
Expand Down
26 changes: 22 additions & 4 deletions src/triggers/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -837,16 +837,34 @@ export class ActionsTrigger {
}
}

sendMessage = async (userId, text) => {
sendMessage = async (receiverId, text) => {
try {
const message = await this.client.sendMessage(userId, text);
this.dispatch(a.userMessages.addUserMessage(userId, message));
const message = await this.client.sendMessage(receiverId, text);
this.dispatch(a.userMessages.sendUserMessage(receiverId, message));
} catch (e) {
this.dispatch(a.messages.addError(e.message));
}
}

updateUserMessages = async (userId) => {
updateMessage = async (receiverId, messageId, text) => {
try {
const message = await this.client.updateUserMessage(messageId, text);
this.dispatch(a.userMessages.updateUserMessage(messageId, message));
} catch (e) {
this.dispatch(a.messages.addError(e.message));
}
}

deleteMessage = async (receiverId, messageId) => {
try {
await this.client.deleteUserMessage(messageId);
this.dispatch(a.userMessages.removeUserMessage(receiverId, messageId));
} catch (e) {
this.dispatch(a.messages.addError(e.message));
}
}

loadUserMessages = async (userId) => {
try {
const messages = await this.client.userMessages(userId);
this.dispatch(a.userMessages.setUserMessages(userId, messages));
Expand Down

0 comments on commit 2ab53c2

Please sign in to comment.