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

const App = () => {
//Brains
const [updateChatLog, setUpdateChatLog] = useState(chatMessages);
// for every item in updated ChatLog replace existing message with latest message.
const refreshMessageLog = (updatedEntry) => {
const newChatData = updateChatLog.map((entry) => {
if (entry.id === updatedEntry.id) {
return updatedEntry;
} else {
return entry;
}
});
setUpdateChatLog(newChatData);
};

const likeCount = updateChatLog.reduce((total, entry) => {
Copy link

Choose a reason for hiding this comment

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

good use of reduce!

return total + entry.liked;
}, 0);

//Beauty
return (
<div id="App">
<header>
<h1>Application title</h1>
<h1>Hope's React Practice Chat Log</h1>
<h2>{likeCount} ❤️s</h2>
</header>
<main>
{/* Wave 01: Render one ChatEntry component
Wave 02: Render ChatLog component */}
<ChatLog entries={updateChatLog} update={refreshMessageLog} />
</main>
</div>
);
Expand Down
36 changes: 31 additions & 5 deletions src/components/ChatEntry.js
Original file line number Diff line number Diff line change
@@ -1,22 +1,48 @@
import React from 'react';
import './ChatEntry.css';
import TimeStamp from './TimeStamp';
import PropTypes from 'prop-types';

const ChatEntry = (props) => {
const ChatEntry = ({ id, sender, body, timeStamp, liked, onUpdate }) => {
//Brains
const handleChangeHeart = () => {
const updatedEntry = {
id: id,
sender: sender,
body: body,
timeStamp: timeStamp,
liked: !liked,
};
onUpdate(updatedEntry);
};

//Beauty
const renderHeart = () => {
return liked ? '❤️' : '🤍';
};
return (
<div className="chat-entry local">
<h2 className="entry-name">Replace with name of sender</h2>
<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} />
</p>
<button onClick={handleChangeHeart} className="like">
{renderHeart()}
</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,
Comment on lines +42 to +45
Copy link

Choose a reason for hiding this comment

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

Same here. The original prop types that the test is looking for are sender, body, and timeStamp. If you added your own you can either make them not required or update the test to account for the added prop types.

};

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

const ChatLog = ({ entries, update }) => {
return entries.map((entry) => {
//Mapping each entry of chatMessages array to a chatEntry react component
return (
<ChatEntry
key={entry.id} /* //Look up react keyError */
Copy link

Choose a reason for hiding this comment

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

you could use the index from map as the key to see if it fixes the error

return entries.map((entry , i) => ...

<ChatEntry
   key={i}...

id={entry.id}
sender={entry.sender}
body={entry.body}
timeStamp={entry.timeStamp}
liked={entry.liked}
onUpdate={update}
/>
);
});
};

ChatLog.propTypes = {
//Fill with correct proptypes
entries: PropTypes.arrayOf(PropTypes.object).isRequired,
update: PropTypes.func.isRequired,
Copy link

Choose a reason for hiding this comment

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

In Learn, I'm sure that you saw a console error for the update prop. Because your update prop was not in the original project you can handle this error by making the update prop a required prop or you can update the tests to look for update as well as entries.

};
export default ChatLog;