diff --git a/src/App.js b/src/App.js index c10859093..9f7c79724 100644 --- a/src/App.js +++ b/src/App.js @@ -1,19 +1,53 @@ import React from 'react'; import './App.css'; import chatMessages from './data/messages.json'; +import ChatLog from './components/ChatLog'; +import { useState } from 'react'; const App = () => { - return ( -
-
-

Application title

-
-
- {/* Wave 01: Render one ChatEntry component - Wave 02: Render ChatLog component */} -
-
- ); + // const [likesCount, setLikesCount] = useState(0); + const [entries, setEntries] = useState(chatMessages); + + const sumHearts = () => { + let total = 0; + + entries.forEach((entry) => { + if (entry.liked) { + total++; + } + }); + return total; + }; + + const switchHeart = (updatedEntry) => { + const updatedEntries = entries.map((entry) => { + if (entry.id === updatedEntry.id) { + // entry.liked = !entry.liked; + + return { ...entry, liked: !entry.liked }; + } + return entry; + }); + setEntries(updatedEntries); + }; + + return ( +
+
+

Allie's Chat App

+
+ {sumHearts()} ❤️s +
+
+ +
+ {/* Wave 01: Render one ChatEntry component */} + + {/* Wave 02: Render ChatLog component */} + +
+
+ ); }; export default App; diff --git a/src/components/ChatEntry.js b/src/components/ChatEntry.js index b92f0b7b2..7f58ead61 100644 --- a/src/components/ChatEntry.js +++ b/src/components/ChatEntry.js @@ -1,22 +1,52 @@ import React from 'react'; import './ChatEntry.css'; import PropTypes from 'prop-types'; +import TimeStamp from './TimeStamp'; +import { useState } from 'react'; const ChatEntry = (props) => { - return ( -
-

Replace with name of sender

-
-

Replace with body of ChatEntry

-

Replace with TimeStamp component

- -
-
- ); + const [likePost, setLikePost] = useState(props.liked); + + const toggleLike = () => { + const updatedEntry = { + id: props.id, + sender: props.sender, + body: props.body, + timeStamp: props.timeStamp, + liked: !props.liked, + }; + setLikePost(!likePost); + props.onUpdate(updatedEntry); + }; + + const heartState = props.liked ? '❤️' : '🤍'; + + return ( +
+ {/*

Replace with name of sender

*/} +

{props.sender}

+
+ {/*

Replace with body of ChatEntry

*/} +

{props.body}

+ {/*

Replace with TimeStamp component

*/} +

+ +

+ +
+
+ ); }; ChatEntry.propTypes = { - //Fill with correct proptypes + //Fill with correct proptypes + id: PropTypes.number, + sender: PropTypes.string, + body: PropTypes.string, + timeStamp: PropTypes.string, + liked: PropTypes.bool, }; export default ChatEntry; diff --git a/src/components/ChatLog.js b/src/components/ChatLog.js new file mode 100644 index 000000000..8f1d6e7c2 --- /dev/null +++ b/src/components/ChatLog.js @@ -0,0 +1,38 @@ +import React from 'react'; +import ChatEntry from './ChatEntry'; +import PropTypes from 'prop-types'; + +const ChatLog = (props) => { + const entries = props.entries; + + const chatComponents = entries.map((message, index) => { + return ( +
+ +
+ ); + }); + return chatComponents; +}; + +ChatLog.propTypes = { + entries: PropTypes.arrayOf( + PropTypes.shape({ + id: PropTypes.number, + sender: PropTypes.string, + body: PropTypes.string, + timeStamp: PropTypes.string, + liked: PropTypes.bool, + }) + ), + onUpdateHeart: PropTypes.func, +}; + +export default ChatLog;