{"size": 3256, "ext": "js", "max_stars_repo_path": "e2e/src/app.js", "max_stars_repo_name": "vermontJC/redux-chat-back", "max_stars_repo_stars_event_min_datetime": "2018-08-06T21:46:57.000Z", "max_stars_repo_stars_event_max_datetime": "2019-01-23T20:16:21.000Z", "max_issues_repo_path": "e2e/src/app.js", "max_issues_repo_name": "vermontJC/redux-chat-back", "max_issues_repo_issues_event_min_datetime": "2018-08-05T14:00:07.000Z", "max_issues_repo_issues_event_max_datetime": "2018-08-16T20:29:03.000Z", "max_forks_repo_path": "e2e/src/app.js", "max_forks_repo_name": "vermontJC/redux-chat-back", "max_forks_repo_forks_event_min_datetime": "2019-01-23T20:16:17.000Z", "max_forks_repo_forks_event_max_datetime": "2022-03-18T14:51:40.000Z"}
{"max_stars_count": 8, "max_issues_count": 5, "max_forks_count": 27, "avg_line_length": 31.6116504854, "max_line_length": 73, "alphanum_fraction": 0.5408476658}
import { createSocket, messageFactory } from './services/channel';
import {
    clearMessageText,
    addMessageText,
    populateRooms,
    changeSelectedRoom,
    userInputValue,
    selectedRoomValue
} from './services/uiHelpers';
import { fetchRooms, canenrollRoom } from './services/api';
import { baseUrl } from './config';

const socketEventsHandler = (socket) => {
    socket.on('connect', () => {
        console.log(socket.id);
        socket.emit('messages');
    });
    socket.on('error', (err) => console.log(err));
    socket.on('disconnect', () => console.log('disconnected'))

    socket.on('message', (msg) => {
        addMessageText('messages', 'p', msg);
        const textElement = document.getElementById('message');
        clearMessageText(textElement);
    });
    socket.on('messages', (msgs) => {
        // NOTE: Timestamp it's created on server not used on client yet.
        console.log('messages', msgs);
        msgs.map((ms) => ({
            user: ms.userId,
            text: ms.text
        })).forEach((mc) => {
            addMessageText('messages', 'p', mc);
        });
    });
};

const handleSelectedRoom = () => {
    // NOTE: Bear on mind to disconnect from socket channel.
    document.getElementById('rooms')
        .addEventListener('click', (evt) => {
            evt.stopPropagation();
            changeSelectedRoom('selectedroom', evt.target.innerHTML);
        });
};

const handleEnrolment = (callback) => {
    // NOTE: Check selected room.
    // NOTE: Check input user.
    document.getElementById('enrolmentsubmit')
        .addEventListener('click', (evt) => {
            evt.stopPropagation();
            evt.preventDefault();
            const room = selectedRoomValue('selectedroom');
            const userId = userInputValue('userid');
            canenrollRoom(
                room,
                userId,
            ).then((canenroll) => {
                if (canenroll) {
                    callback(null, room, userId);
                } else {
                    callback('Can not enroll to this channel');
                }
            }
            ).catch(e => console.log(e));
        });
};

const handleSender = (socket, room, user) => {
    const _messageFactory = messageFactory(room, user);
    document.getElementById('sendbutton')
        .addEventListener('click', (evt) => {
            evt.stopPropagation();
            const textElement = document.getElementById('message');
            const message = _messageFactory.compose(textElement.value)
            socket.emit('message', message);
        });
};

document.addEventListener('DOMContentLoaded', () => {
    fetchRooms()
        .then((rooms) => {
            populateRooms(rooms, 'rooms');
            handleSelectedRoom();
        })
        .catch(e => console.log(e));

    handleEnrolment((err, room, userId) => {
        if (!err) {
            const socketParams = {
                url: baseUrl,
                channel: room,
                options: {
                    query: `user=${userId}`
                },
            };
            const socket = createSocket(socketParams);
            socketEventsHandler(socket);
            handleSender(socket, room, userId);
        }
    });
});