Skip to content

Instantly share code, notes, and snippets.

defmodule Sling.PaginationHelpers do
def pagination(page) do
%{
page_number: page.page_number,
page_size: page.page_size,
total_pages: page.total_pages,
total_entries: page.total_entries
}
end
end
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])
defmodule Sling.Repo do
use Ecto.Repo, otp_app: :sling
use Scrivener, page_size: 25
end
// @flow
import React, { Component } from 'react';
import { connect } from 'react-redux';
import { connectToChannel, leaveChannel } from '../../actions/room';
type RoomType = {
id: number,
name: string,
}
// ...
import room from './room'; // new line
const appReducer = combineReducers({
form,
session,
rooms,
room, // new line
});
@bnhansn
bnhansn / room.js
Last active October 22, 2016 13:25
const initialState = {
channel: null,
currentRoom: {},
};
export default function (state = initialState, action) {
switch (action.type) {
case 'ROOM_CONNECTED_TO_CHANNEL':
return {
...state,
@bnhansn
bnhansn / room.js
Last active October 22, 2016 13:25
export function connectToChannel(socket, roomId) {
return (dispatch) => {
if (!socket) { return false; }
const channel = socket.channel(`rooms:${roomId}`);
channel.join().receive('ok', (response) => {
dispatch({ type: 'ROOM_CONNECTED_TO_CHANNEL', response, channel });
});
return false;
const initialState = {
willAuthenticate: true,
isAuthenticated: false,
currentUser: {},
socket: null, // new line
};
// ...
case 'LOGOUT':
import { reset } from 'redux-form';
import { Socket } from 'phoenix'; // new line
import api from '../api';
import { fetchUserRooms } from './rooms';
const API_URL = process.env.REACT_APP_API_URL; // new line
const WEBSOCKET_URL = API_URL.replace(/(https|http)/, 'ws').replace('/api', ''); // new line
// new function
function connectToSocket(dispatch) {
defmodule Sling.RoomChannel do
use Sling.Web, :channel
def join("rooms:" <> room_id, _params, socket) do
room = Repo.get!(Sling.Room, room_id)
response = %{
room: Phoenix.View.render_one(room, Sling.RoomView, "room.json"),
}