diff --git a/project-docs/wave-01.md b/project-docs/wave-01.md index cf05e2651..f80827cff 100644 --- a/project-docs/wave-01.md +++ b/project-docs/wave-01.md @@ -2,7 +2,10 @@ **Learn Topics: React Components and Props required for this wave** -Update the `ChatEntry` and `App` components to display a single chat message bubble with the message text and relative timestamp, plus the sender's name above it. +Update the `ChatEntry` and `App` components to display a +single chat message bubble with the message text and +relative timestamp, plus +the sender's name above it. A good way to test this code as you write it would be to take the first chat message from the [JSON data file](../src/data/messages.json) and use it as the data for the `ChatEntry` component. diff --git a/src/App.jsx b/src/App.jsx index 14a7f684d..28f99cbf3 100644 --- a/src/App.jsx +++ b/src/App.jsx @@ -1,14 +1,30 @@ import './App.css'; +import ChatLog from './components/ChatLog'; +import chatData from './data/messages.json'; +import { useState } from 'react'; const App = () => { + const [entries, setEntries] = useState(chatData); + const toggleLike = (id) => { + const updatedEntries = entries.map((entry) => { + if (entry.id === id) { + return { ...entry, liked: !entry.liked }; + } else { + return entry; + } + }); + setEntries(updatedEntries); + }; + const likeCount = entries.reduce((total, entry) => { + return entry.liked ? total + 1 : total; + }, 0); return (
-

Application title

+

{likeCount} ❤️s

- {/* Wave 01: Render one ChatEntry component - Wave 02: Render ChatLog component */} +
); diff --git a/src/components/ChatEntry.jsx b/src/components/ChatEntry.jsx index 15c56f96b..ac1e86db5 100644 --- a/src/components/ChatEntry.jsx +++ b/src/components/ChatEntry.jsx @@ -1,20 +1,32 @@ import './ChatEntry.css'; +import TimeStamp from './TimeStamp'; +import PropTypes from 'prop-types'; +// import { useState } from 'react'; -const ChatEntry = () => { +const ChatEntry = (props) => { + const heart = props.liked ? '❤️' : '🤍'; + const handleLikeClick = () => { + props.onLikeToggle(props.id); + }; return (
-

Replace with name of sender

+

{props.sender}

-

Replace with body of ChatEntry

-

Replace with TimeStamp component

- +

{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, + onLikeToggle: PropTypes.func.isRequired, }; export default ChatEntry; diff --git a/src/components/ChatLog.jsx b/src/components/ChatLog.jsx new file mode 100644 index 000000000..c45b1f286 --- /dev/null +++ b/src/components/ChatLog.jsx @@ -0,0 +1,36 @@ +import PropTypes from 'prop-types'; +import ChatEntry from './ChatEntry'; +import './ChatLog.css'; + +const ChatLog = ({entries, onLikeToggle}) => { + const chatComponents = entries.map((entry) => { + return ( + + ); + }); + + return
{chatComponents}
; +}; + +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, + onLikeToggle: PropTypes.func.isRequired +}; + +export default ChatLog; \ No newline at end of file