Skip to content

Instantly share code, notes, and snippets.

// @flow
import React, { Component } from 'react';
import moment from 'moment';
import groupBy from 'lodash/groupBy';
import mapKeys from 'lodash/mapKeys';
import { css, StyleSheet } from 'aphrodite';
import debounce from 'lodash/debounce';
import Message from '../Message';
const styles = StyleSheet.create({
const initialState = {
channel: null,
currentRoom: {},
messages: [],
presentUsers: [],
loadingOlderMessages: false, // new line
pagination: { // new line
total_pages: 0,
total_entries: 0,
page_size: 0,
// top of file
import api from '../api';
// bottom of file
export function loadOlderMessages(roomId, params) {
return (dispatch) => {
dispatch({ type: 'FETCH_MESSAGES_REQUEST' });
return api.fetch(`/rooms/${roomId}/messages`, params)
.then((response) => {
dispatch({ type: 'FETCH_MESSAGES_SUCCESS', response });
# ...
resources "/rooms", RoomController, only: [:index, :create] do
resources "/messages", MessageController, only: [:index]
end
# ...rest of file
defmodule Sling.MessageController do
use Sling.Web, :controller
plug Guardian.Plug.EnsureAuthenticated, handler: Sling.SessionController
def index(conn, params) do
last_seen_id = params["last_seen_id"] || 0
room = Repo.get!(Sling.Room, params["room_id"])
page =
// @flow
import React from 'react';
import { css, StyleSheet } from 'aphrodite';
const styles = StyleSheet.create({
roomSidebar: {
color: '#ab9ba9',
background: '#4d394b',
},
@bnhansn
bnhansn / index.js
Last active October 22, 2016 18:29
// @flow
import React, { Component } from 'react';
import { connect } from 'react-redux';
import { connectToChannel, leaveChannel, createMessage } from '../../actions/room';
import MessageList from '../../components/MessageList';
import MessageForm from '../../components/MessageForm';
import RoomNavbar from '../../components/RoomNavbar';
import RoomSidebar from '../../components/RoomSidebar'; // new line
type MessageType = {
const initialState = {
channel: null,
currentRoom: {},
messages: [],
presentUsers: [], // new line
};
export default function (state = initialState, action) {
switch (action.type) {
case 'ROOM_CONNECTED_TO_CHANNEL':
import { reset } from 'redux-form';
import { Presence } from 'phoenix'; // new line
// new function
const syncPresentUsers = (dispatch, presences) => {
const presentUsers = [];
Presence.list(presences, (id, { metas: [first] }) => first.user)
.map(user => presentUsers.push(user));
dispatch({ type: 'ROOM_PRESENCE_UPDATE', presentUsers });
};
defmodule Sling.RoomChannel do
use Sling.Web, :channel
def join("rooms:" <> room_id, _params, socket) do
room = Repo.get!(Sling.Room, room_id)
page =
Sling.Message
|> where([m], m.room_id == ^room.id)
|> order_by([desc: :inserted_at, desc: :id])