diff --git a/run.sh b/run.sh new file mode 100755 index 0000000..34b336e --- /dev/null +++ b/run.sh @@ -0,0 +1,2 @@ +sudo sysctl fs.inotify.max_user_watches=524288 +gatsby develop \ No newline at end of file diff --git a/src/components/chat/chatConnect.js b/src/components/chat/chatConnect.js new file mode 100644 index 0000000..0861e83 --- /dev/null +++ b/src/components/chat/chatConnect.js @@ -0,0 +1,67 @@ +import React from 'react' +import { connect } from 'react-redux' + +import { chatOnOpen, chatOnClose, getChatRooms } from '../../stores/chat/duck/operations' + +import '../../styles/indexChat.scss' + +const ChatConnect = ({ chat, chatOnOpen, chatOnClose, getChatRooms }) => { + + const inputLobbyName = React.createRef() + + const setLobbyName = (event) => { + event.preventDefault() + if ( inputLobbyName.current.value !== '' ){ + chatOnOpen(inputLobbyName.current.value) + } + else + inputLobbyName.current.value = 'Get a lobby name!' + } + + const disconnect = (event) => { + event.preventDefault() + try{ + chat.webSocket.close() + chatOnClose() + getChatRooms() + }catch { + chatOnClose() + getChatRooms() + } + } + + if (chat.connected === false) + return ( +
+ ) + else + return ( + + ) +} + +const mapStateToProps = state => ({ + chat: state.chat + }) + + const mapDispatchToProps = dispatch => ({ + chatOnOpen: chat => dispatch( chatOnOpen(chat) ), + chatOnClose: chat => dispatch( chatOnClose(chat) ), + getChatRooms: chat => dispatch( getChatRooms(chat) ) + }) + +export default connect(mapStateToProps, mapDispatchToProps)(ChatConnect) \ No newline at end of file diff --git a/src/components/chat/indexChat.js b/src/components/chat/indexChat.js index ccbe02c..d03c537 100644 --- a/src/components/chat/indexChat.js +++ b/src/components/chat/indexChat.js @@ -1,14 +1,88 @@ import React, { useState, useEffect } from 'react' import { connect } from 'react-redux' +import { chatOnMessage, getChatRooms } from '../../stores/chat/duck/operations' + +import ChatConnect from './chatConnect' + import '../../styles/indexChat.scss' -const IndexChat = ({ user }) => { +import imageIco from '../../images/imageIco.png' +const IndexChat = ({ user, chat, chatOnMessage, getChatRooms }) => { + const [chatVisible, setChatVisible] = useState(false) + const [chatImageVisible, setChatImageVisible] = useState( { visible: false, image: ''} ) + + const inputMessageText = React.createRef() + + useEffect( () => { getChatRooms() }, [] ) + + if ( chat.host !== '' ) + chat.webSocket.onmessage = (event) => { + chatOnMessage(JSON.parse(event.data)) + } + + const sendMessage = (event) => { + event.preventDefault() + + let checkUploadValue = document.getElementById('fileItem').value + + if ( inputMessageText.current.value !== '' && checkUploadValue !== '' && chat.host !== '' ) { + + let inputMessageImage = document.getElementById('fileItem').files[0] + + let message = { + userID: user.id, + userName: user.login, + avatar: user.avatar, + text: inputMessageText.current.value, + textOnly: false, + imageOnly: false + } + inputMessageText.current.value = '' + document.getElementById('fileItem').value = '' + sendBase64Image(inputMessageImage, message) + } + else if (inputMessageText.current.value !== '' && chat.host !== '' ) { + let message = { + userID: user.id, + userName: user.login, + avatar: user.avatar, + text: inputMessageText.current.value, + textOnly: true, + imageOnly: false + } + inputMessageText.current.value = '' + chat.webSocket.send(JSON.stringify(message)) + } + else if ( checkUploadValue !== '' ) { + let inputMessageImage = document.getElementById('fileItem').files[0] + let message = { + userID: user.id, + userName: user.login, + avatar: user.avatar, + textOnly: false, + imageOnly: true + } + document.getElementById('fileItem').value = '' + sendBase64Image(inputMessageImage, message) + } + else + inputMessageText.current.value = 'Type message' + } + + const sendBase64Image = (image, message) => { + let reader = new FileReader() + reader.onload = () => { + message.image = reader.result + chat.webSocket.send(JSON.stringify(message)) + } + reader.readAsDataURL(image) + } return ( -{ key + 1 }. Value: { trigger.course_values_for_trigger } PLN, Date: { trigger.date_of_trigger }, Status { trigger.status === 1 ? 'Enabled' : 'Disabled' }
{ key + 1 }. Value: { trigger.course_values_for_trigger } PLN, Date: { trigger.date_of_trigger }, Status { trigger.status === 1 ? 'Enabled' : 'Disabled' }
+