Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 24 additions & 2 deletions src/App.jsx
Original file line number Diff line number Diff line change
@@ -1,14 +1,36 @@
import './App.css';
import ChatLog from './components/ChatLog.jsx';
import messages from './data/messages.json';
import { useState } from 'react';

const App = () => {
const[messageData, setMessageData] = useState(messages);

const likeFeature = (messageId) => {
const messages = messageData.map(message => {
if (message.id === messageId) {
return { ...message, liked: !message.liked,
};
} else {
return message;
}
});
setMessageData(messages);
};

const getTotalLikes = () => messageData.filter(message => message.liked).length;

return (
<div id="App">
<header>
<h1>Application title</h1>
<h2>Total ❤️: {getTotalLikes()} ❤️s</h2>
</header>
<main>
{/* Wave 01: Render one ChatEntry component
Wave 02: Render ChatLog component */}
<ChatLog
entries={messageData}
likeClick={likeFeature}
/>
</main>
</div>
);
Expand Down
45 changes: 28 additions & 17 deletions src/components/ChatEntry.jsx
Original file line number Diff line number Diff line change
@@ -1,21 +1,32 @@
import './ChatEntry.css';
import PropTypes from 'prop-types';
import TimeStamp from './TimeStamp.jsx';

const ChatEntry = () => {
return (
// Replace the outer tag name with a semantic element that fits our use case
<replace-with-relevant-semantic-element className="chat-entry local">
<h2 className="entry-name">Replace with name of sender</h2>
<section className="entry-bubble">
<p>Replace with body of ChatEntry</p>
<p className="entry-time">Replace with TimeStamp component</p>
<button className="like">🤍</button>
</section>
</replace-with-relevant-semantic-element>
);
};
const ChatEntry = (props ) => {

ChatEntry.propTypes = {
// Fill with correct proptypes
};
const clickLike = () => {
props.likeClick(props.id);
};

export default ChatEntry;
return (
<article className="chat-entry local">
<h2 className="entry-name">{props.sender}</h2>
<section className="entry-bubble">
<p>{props.body}</p>
<TimeStamp time={props.timeStamp} className="entry-time"/>
<button className="like" onClick={clickLike}>{props.liked ? '❤️' : '🤍'}</button>
</section>
</article>
);
};

ChatEntry.propTypes = {
sender: PropTypes.string.isRequired,
body: PropTypes.string.isRequired,
timeStamp: PropTypes.string.isRequired,
id: PropTypes.number.isRequired,
liked: PropTypes.bool.isRequired,
likeClick: PropTypes.func,
};

export default ChatEntry;
43 changes: 43 additions & 0 deletions src/components/ChatLog.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import './ChatLog.css';
import PropTypes from 'prop-types';
import ChatEntry from './ChatEntry.jsx';

const ChatLog = ( {entries, likeClick } ) => {

const chatEntries = entries.map((entry) => {
return (
<ChatEntry
key={entry.id}
id={entry.id}
sender={entry.sender}
body={entry.body}
timeStamp={entry.timeStamp}
liked={entry.liked}
likeCount={entry.likeCount}
likeClick={likeClick}
/>
);
});
return(
<article className="chat-log">
{chatEntries}
</article>

);
};

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,
likeClick: PropTypes.func,
};

export default ChatLog;

1 change: 1 addition & 0 deletions src/data/messages.json
Original file line number Diff line number Diff line change
Expand Up @@ -189,3 +189,4 @@
"liked": false
}
]