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

Single chat room #40

Open
wants to merge 12 commits into
base: master
Choose a base branch
from
Open

Single chat room #40

wants to merge 12 commits into from

Conversation

MathildeCanesse
Copy link

Summary

This is a simple chat room to be able to send messages.

Main steps

• Implementation of redux-saga
• Babel polyfill is added to the webpack config for RuntimeGenerator and so be able to use redux-saga
• The update of the firebase configuration
• New link in the header to access to the chat room
• Chat Module with a main page and some components to display messages.
• New reducer to manage messages
• Watcher to fetch and push messages on the firebase database.

You need to be logged to access to the room, if not, we will be redirect to the authentification.
You will be able to read the last 10 messages, also read messages from other users and finally to write new messages.

I fixed a little problem on the LoadingDots.js by using lodash but i'm not sure is the best way.

How to start

npm start -s

};

sendNewMessage = e => {
e.preventDefault();

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Minor: can be left to the underlying componenent

}

export function createEventChannel() {
const listener = eventChannel(emit => {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍


export function* watchFetchMessages() {
const updateMessages = createEventChannel();
while (true) {
Copy link

@alexstrat alexstrat Nov 28, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Minor: I think you can use takeEvery directly

.set(value);

.limitToLast(limit)
.on('value', handler);
Copy link

@alexstrat alexstrat Nov 28, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When working with list of datas, you could (and should at scale) be more granular with data loading. Otherwise you'll end up loading too much data from the network, it will slow down the interface.

You can check the firebase doc to know how: https://firebase.google.com/docs/database/web/lists-of-data

export default function messagesReducer(state = initialState.messages, action) {
switch (action.type) {
case types.FETCH_MESSAGES_SUCCESS:
return Object.keys(action.payload).map(key => {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In theory, you should merge the payload wit the current state: you add the new messages to the currently loaded messages.

Given the fact you are using the value event from Firebase, it does not change anything but, as mentioned above, you should not really rely on value only.

}
}

export const getMessages = state => state.messages;

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Selectors ;)


updateMessage = e => {
let message = this.state.message;
message[e.target.name] = e.target.value;

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ I think you are implicitly mutating the state here.

Indeed:

const message = this.state.message;
message['foo'] = 'bar';
this.setState({message: message})
this.state.message === message //=> true

https://medium.com/pro-react/a-brief-talk-about-immutability-and-react-s-helpers-70919ab8ae7c

import prop from 'lodash/fp/prop';
import MessageItem from './MessageItem';

const Message = ({ messages }) =>

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Minor: could be renamed MessageList

export default function messagesReducer(state = initialState.messages, action) {
switch (action.type) {
case types.FETCH_MESSAGES_SUCCESS:
return [...state, action.payload];

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would have checked on the key / id of a message like done here to avoid duplicates.

Can be useful: https://redux.js.org/docs/basics/ExampleTodoList.html#reducers

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants