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
30 changes: 27 additions & 3 deletions src/App.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,40 @@
import React from 'react';
import './App.css';
import chatMessages from './data/messages.json';
import ChatLog from './components/ChatLog';
import { useState } from 'react';

const App = () => {
// create chatEntries state with imported chatMessages data
const [chatEntries, setChatEntries] = useState(chatMessages);

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice use of useState to watch changes!


// Function to update chatEntries
const updateEntries = (updatedEntry) => {
const entries = chatEntries.map((entry) => {
if (entry.id === updatedEntry.id) {
return updatedEntry;
} else {
return entry;
}
});
setChatEntries(entries);
};

// Track total number of likes in conversation
let numLikes = 0;
for (const entry of chatEntries) {
if (entry.liked) {
numLikes++;
}
}

return (
<div id="App">
<header>
<h1>Application title</h1>
<h1>{numLikes} ❤️s</h1>
</header>
<main>
{/* Wave 01: Render one ChatEntry component
Wave 02: Render ChatLog component */}
<ChatLog entries={chatEntries} updateEntries={updateEntries}></ChatLog>
</main>
</div>
);
Expand Down
32 changes: 27 additions & 5 deletions src/components/ChatEntry.js
Original file line number Diff line number Diff line change
@@ -1,22 +1,44 @@
import React from 'react';
import './ChatEntry.css';
import PropTypes from 'prop-types';
import TimeStamp from './TimeStamp';

const ChatEntry = (props) => {
// onclick liked event handler
const onLikedButtonClick = () => {
const updatedEntry = {
id: props.id,
sender: props.sender,
body: props.body,
timeStamp: props.timeStamp,
liked: !props.liked,
};
props.updateEntries(updatedEntry);
};

const heartColor = props.liked ? '❤️' : '🤍';

return (
<div className="chat-entry local">
<h2 className="entry-name">Replace with name of sender</h2>
<h2 className="entry-name">{props.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>{props.body}</p>
<TimeStamp className="entry-time" time={props.timeStamp}></TimeStamp>
<button className="like" onClick={onLikedButtonClick}>
{heartColor}
</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,
liked: PropTypes.bool.isRequired,
updateEntries: PropTypes.func,
};

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

const ChatLog = (props) => {
const messages = props.entries.map((entry) => {
return (
<ChatEntry
id={entry.id}
sender={entry.sender}
body={entry.body}
timeStamp={entry.timeStamp}
liked={entry.liked}
updateEntries={props.updateEntries}
></ChatEntry>
);
});
return messages;
};

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,
})
),
updateEntries: PropTypes.func,
};
export default ChatLog;