diff --git a/src/App.jsx b/src/App.jsx index 14a7f684..27389b76 100644 --- a/src/App.jsx +++ b/src/App.jsx @@ -1,14 +1,86 @@ +import { useState } from 'react'; import './App.css'; +import ChatLog from './components/ChatLog'; +import messageData from './data/messages.json'; const App = () => { + const [messages, setMessages] = useState(messageData); + const [likedCount, setLikedCount] = useState(0); + const [remoteColor, setRemoteColor] = useState(); + const [localColor, setLocalColor] = useState(); + + const toggleLikedMessage = (messageId) => { + const updatedMessages = messages.map((message) => { + return message.id === messageId + ? { ...message, liked: !message.liked } + : message; + }); + + const likedList = updatedMessages.filter((message) => message.liked); + + setLikedCount(likedList.length); + setMessages(updatedMessages); + }; + + const participantList = []; + messages.forEach((message) => { + if (!participantList.includes(message.sender)) { + participantList.push(message.sender); + } + }); + + const colorBtns = (senderLocation) => { + const colors = [ + { '🔴': 'red' }, + { '🟠': 'orange' }, + { '🟡': 'yellow' }, + { '🟢': 'green' }, + { '🔵': 'blue' }, + { '🟣': 'purple' } + ]; + return colors.map((color, index) => { + return Object.entries(color).map(([colorIcon, colorName]) => { + return ( + + ); + }); + }); + }; + return (
-

Application title

+ +

Chat between

{' '} +

{participantList[0]}

{' '} +

and

{' '} +

{participantList[1]}

+
+
+ +

{participantList[0]}'s color:

+ {colorBtns('local')} +
+

{likedCount} ❤️s

+ +

{participantList[1]}'s color:

+ {colorBtns('remote')} +
+
- {/* Wave 01: Render one ChatEntry component - Wave 02: Render ChatLog component */} +
); diff --git a/src/components/ChatEntry.jsx b/src/components/ChatEntry.jsx index 15c56f96..7f7c996e 100644 --- a/src/components/ChatEntry.jsx +++ b/src/components/ChatEntry.jsx @@ -1,20 +1,44 @@ import './ChatEntry.css'; +import TimeStamp from './TimeStamp'; +import PropTypes from 'prop-types'; + +const ChatEntry = (props) => { + const isRemote = props.sender === 'Estragon' ? true : false; + const senderLocation = isRemote + ? 'chat-entry remote' + : 'chat-entry local'; + const senderColor = isRemote + ? props.remoteColor + : props.localColor; -const ChatEntry = () => { return ( -
-

Replace with name of sender

-
-

Replace with body of ChatEntry

-

Replace with TimeStamp component

- +
+
+

{props.sender}

+
+

{props.body}

+

+ +
); }; ChatEntry.propTypes = { - // Fill with correct proptypes + id: PropTypes.number.isRequired, + sender: PropTypes.string.isRequired, + body: PropTypes.string.isRequired, + timeStamp: PropTypes.string.isRequired, + liked: PropTypes.bool.isRequired, + onMessageLiked: PropTypes.func.isRequired, + localColor: PropTypes.string, + remoteColor: PropTypes.string, }; export default ChatEntry; diff --git a/src/components/ChatLog.jsx b/src/components/ChatLog.jsx new file mode 100644 index 00000000..31882e63 --- /dev/null +++ b/src/components/ChatLog.jsx @@ -0,0 +1,40 @@ +import './ChatLog.css'; +import PropTypes from 'prop-types'; +import ChatEntry from './ChatEntry'; + +function ChatLog(props) { + const chatMessages = props.entries.map((message) => { + return ( +
+ +
+ ); + }); + + return chatMessages; +} + +ChatLog.propTypes = { + entries: PropTypes.arrayOf( + PropTypes.shape({ + id: PropTypes.number.isRequired, + sender: PropTypes.string.isRequired, + body: PropTypes.string.isRequired, + timeStamp: PropTypes.string.isRequired, + liked: PropTypes.bool.isRequired, + })).isRequired, + onMessageLiked: PropTypes.func.isRequired, + localColor: PropTypes.string, + remoteColor: PropTypes.string, +}; + +export default ChatLog;