import config from 'config';
import { getLogger } from 'log4js';
import { create, update }from '../../actions/Board';
import { push }from '../../actions/Shape';
import * as BOARD from '../../constants/actions/Board';

const logger = getLogger('[BOARD]');

const joinToBoard = (socket, id) => {
    socket.leave();
    socket.join(id);
    socket.boardId = id;
    logger.info(socket.id, 'joined into', id);
};

const onJoin = ({dispatch, getState}, next, action) => {
    const socket = action.socket;

    joinToBoard(socket, action.id);

    if (action.id in getState().boards) {
        dispatch({
            ...update(getState().boards[action.id]),
            broadcast: false,
            sync: true,
            socket,
        });
    } else {
        dispatch({
            ...create({
                ...config.get('board'),
                id: action.id,
                owner: action.socket.id,
            }),
            socket,
        });
    }

    const shapes = getState().shapes[action.id];

    if (shapes) {
        socket.emit('action', {
            ...push(shapes),
            broadcast: false,
            sync: false,
        });
    }

    return next(action);
};

export const board = ({dispatch, getState}) => (next) => (action) => {
    switch(action.type) {
        case BOARD.JOIN: {
            return onJoin({dispatch, getState}, next, action);
        } case BOARD.UPDATE:
            if (action.socket) {
                dispatch(update(action.data));
            }
            break;
    }

    return next(action);
};
@@ -1309,19 +1309,17 @@
 RD.JOIN:
- %7B
 %0A
+
         
@@ -1380,18 +1380,16 @@
 %0A       
- %7D
  case BO
@@ -1416,27 +1416,20 @@
     
-if (action.socket) 
+return next(
 %7B%0A  
@@ -1442,25 +1442,19 @@
         
-dispatch(
+...
 update(a
@@ -1464,18 +1464,17 @@
 on.data)
-);
+,
 %0A       
@@ -1482,27 +1482,78 @@
     
-%7D%0A            break
+    sync: false,%0A                socket: action.socket,%0A            %7D)
 ;%0A  

Fix board updating