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
24 changes: 21 additions & 3 deletions src/App.jsx
Original file line number Diff line number Diff line change
@@ -1,14 +1,32 @@
import './App.css';
import chatMessages from './data/messages.json'
import ChatLog from './components/ChatLog';
import { useState } from 'react';

const App = () => {
const [entries, setEntries] = useState(chatMessages);

const toggleLike = (id) => {
const updatedEntries = entries.map((entry) => {
if(entry.id === id) {
return { ...entry, liked: !entry.liked };
}
return entry;
});
setEntries(updatedEntries);
};

const totalLikes = entries.filter((entry) => entry.liked).length;

return (
<div id="App">
<header>
<h1>Application title</h1>
<h1>{totalLikes} ❤️s</h1>
</header>
<main>
{/* Wave 01: Render one ChatEntry component
Wave 02: Render ChatLog component */}
<div className='App'>
<ChatLog entries={entries} onLikeToggle={toggleLike} />
</div>
</main>
</div>
);
Expand Down
26 changes: 19 additions & 7 deletions src/components/ChatEntry.jsx
Original file line number Diff line number Diff line change
@@ -1,20 +1,32 @@
import './ChatEntry.css';
import PropTypes from 'prop-types';
import TimeStamp from './TimeStamp';

const ChatEntry = ({ id, sender, body, timeStamp, isLocal, liked, onLikeToggle }) => {
const entryClass = isLocal ? 'chat-entry local' : 'chat-entry remote';

const ChatEntry = () => {
return (
<div className="chat-entry local">
<h2 className="entry-name">Replace with name of sender</h2>
<div className={entryClass}>
<h2 className="entry-name">{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>
<p>{body}</p>
<p className="entry-time"> <TimeStamp time={timeStamp}></TimeStamp> </p>
<button className="like" onClick={() => onLikeToggle(id)}>
{liked ? '❤️' : '🤍'}
</button>
</section>
</div>
);
};

ChatEntry.propTypes = {
// Fill with correct proptypes
id: PropTypes.number.isRequired,
sender: PropTypes.string.isRequired,
body: PropTypes.string.isRequired,
timeStamp: PropTypes.string.isRequired,
isLocal: PropTypes.bool.isRequired,
liked: PropTypes.bool.isRequired,
onLikeToggle: PropTypes.func.isRequired,
};

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

const LOCAL_SENDER = 'Vladimir';

const ChatLog = ({ entries, onLikeToggle }) => {
const getChatListJSX = (entries) => {
return entries.map((entry) => {
return (
<ChatEntry
key={entry.id}
id={entry.id}
sender={entry.sender}
body={entry.body}
timeStamp={entry.timeStamp}
isLocal={entry.sender === LOCAL_SENDER}
liked={entry.liked}
onLikeToggle={onLikeToggle}
/>
);
});
};

return (
<div className='chat-log'>
{getChatListJSX(entries)}
</div>
);
};

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

export default ChatLog;