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

let totalLikes = 0;

Choose a reason for hiding this comment

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

totalLikes should have been stored in a useState variable.

When you update state in React it triggers a re-render automatically. Updating a global variable won't. (Also if you instantiated the app twice on the same page you'd be in trouble.)


const updateLikes = (entryData) => {
let likes = 0;
for (const entry of entryData) {
if (entry.liked) {
likes += 1;
}
}
totalLikes = likes;
};

const App = () => {
const [entryData, setEntryData] = useState(chatMessages);
const updateEntryData = (updatedEntry) => {
const entries = entryData.map((chatEntry) => {
if (chatEntry.id === updatedEntry.id) {
return updatedEntry;
} else {
return chatEntry;
}
});
setEntryData(entries);
updateLikes(entries);
};

return (
<div id="App">
<header>
<h1>Application title</h1>
<h1>Chat between Vladimir and Estragon</h1>
<section className="header section">
<h4 id="heartWidget" className="widget">

Choose a reason for hiding this comment

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

Remember, the h1, h2, etc. elements should be used to denote the structure of the page (you should be able to build a table of contents).

If you want to change the size of text you should use CSS.

{totalLikes} ❤️s
</h4>
</section>
</header>
<main>
{/* Wave 01: Render one ChatEntry component
Wave 02: Render ChatLog component */}
<ChatLog entries={entryData} onUpdateEntry={updateEntryData}></ChatLog>
</main>
</div>
);
Expand Down
68 changes: 57 additions & 11 deletions src/components/ChatEntry.js
Original file line number Diff line number Diff line change
@@ -1,22 +1,68 @@
import React from 'react';
import './ChatEntry.css';
import PropTypes from 'prop-types';
import TimeStamp from './TimeStamp';

const ChatEntry = (props) => {
return (
<div 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>
</div>
);
const onLikeButtonClick = () => {
const updatedEntry = {
id: props.id,
sender: props.sender,
body: props.body,
timeStamp: props.timeStamp,
liked: !props.liked,
};
props.onUpdate(updatedEntry);
};

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

if (props.sender === 'Vladimir') {
return (
<div className="chat-entry local">
<h2 className="entry-name"> {props.sender} </h2>
<section className="entry-bubble">
<p> {props.body} </p>
<p className="entry-time">
<TimeStamp
time={props.timeStamp}
className="entry-time"
></TimeStamp>
</p>
<button className="like" onClick={onLikeButtonClick}>
{heart}
</button>
</section>
</div>
);
} else {
return (
<div className="chat-entry remote">
<h2 className="entry-name"> {props.sender} </h2>
<section className="entry-bubble">
<p> {props.body} </p>
<p className="entry-time">
<TimeStamp
time={props.timeStamp}
className="entry-time"
></TimeStamp>
</p>
<button className="like" onClick={onLikeButtonClick}>
{heart}
</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,
onUpdate: PropTypes.func.isRequired,
};

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

const ChatLog = (props) => {
const chatComponents = props.entries.map((chatEntry) => {
return (
<section>
<ChatEntry
id={chatEntry.id}
sender={chatEntry.sender}
body={chatEntry.body}
timeStamp={chatEntry.timeStamp}
liked={chatEntry.liked}
onUpdate={props.onUpdateEntry}
></ChatEntry>
</section>
Comment on lines +9 to +18

Choose a reason for hiding this comment

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

This extra section doesn't hurt anything but isn't required, FYI.

);
});

return (
<section>
<ul className="chat-log">{chatComponents}</ul>
</section>
);
};

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

export default ChatLog;