const gruvboxColors = [ "#b8bb26", "#fabd2f", "#83a598", "#d3869b", "#8ec07c", "#458588", "#cc241d", "#d65d0e", "#bdae93", ]; const generateGruvboxFromString = (string) => gruvboxColors[Array.from(string).map((x) => x.charCodeAt(0)).reduce((a, x) => a+x, 0) % gruvboxColors.length]; const RoomChat = (() => { let channel; const connect = (socket, postId) => { channel = socket.channel(`post:${postId}`); channel.join() .receive("ok", resp => { console.log("Joined successfully: ", resp); }) .receive("error", resp => { console.log("Unable to join: ", resp); }); return channel; }; const scrollToBottom = (element) => { element.scrollTop = element.scrollHeight; }; const appendComment = ({user, body, id, user_id, inserted_at}, element) => { const messageElement = document.createElement("div"); messageElement.innerHTML = `
${user.charAt(0)}
${user} ${new Date(inserted_at).toLocaleString()}
${body}
`; element.appendChild(messageElement); scrollToBottom(element); }; const leaveChannel = () => { if (channel) { channel.leave(); console.log(channel); } }; const main = (post_id) => { leaveChannel(); const chatWindow = document.getElementById("chat"); window.userSocket.connect(); channel = connect(window.userSocket, post_id); channel.on('shout', (comment) => { appendComment(comment, chatWindow); }); channel.on('initial-comments', ({comments}) => { comments.forEach((comment) => { appendComment(comment, chatWindow); }); scrollToBottom(chatWindow); }); channel.on('join', ({ user }) => { const joinElement = document.createElement("div"); joinElement.innerHTML = `
join: ${user}
`; chatWindow.appendChild(joinElement); scrollToBottom(chatWindow); }); channel.on('left', ({ user }) => { console.log(user, "left"); }); }; const submitForm = (e) => { e.preventDefault(); let message = e.target.elements.message.value; if (message) { channel.push("send", {body: message}); e.target.elements.message.value = ""; } return false; }; return { main, submitForm }; })(); window.addEventListener('load', () => { window.addEventListener('phx:initial-post', (e) => RoomChat.main(e.detail.id)); }); export default RoomChat;