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

const App = () => {
const [chatData, updateChatData] = useState(chatMessages);

Choose a reason for hiding this comment

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

Nice job bringing in useState

const updateLikes = (id) => {
const chats = chatData.map((chat) => {
if (chat.id === id) {
const newChat = { ...chat };
newChat.liked = !newChat.liked;
return newChat;
}
return chat;
});
updateChatData(chats);
};
const totalLikes = () => {
let total = 0;
for (let chat of chatData) {
if (chat.liked === true) {
total += 1;
}
}
return total;
};
return (
<div id="App">
<header>
<h1>Application title</h1>
<header id="App header">
<h1 id="App h1">
Chat between {chatData[0].sender} and {chatData[1].sender}
</h1>
<section id="App header section">
<h2 id="heartWidget" className="widget">
<span id="App span">{totalLikes()} ❤️s</span>
</h2>
</section>
</header>
<main>
{/* Wave 01: Render one ChatEntry component
Wave 02: Render ChatLog component */}
<main id="App main">
<div>{<ChatLog entries={chatData} onUpdateLikes={updateLikes} />}</div>
</main>
</div>
);
Expand Down
34 changes: 27 additions & 7 deletions src/components/ChatEntry.js
Original file line number Diff line number Diff line change
@@ -1,22 +1,42 @@
import React from 'react';
import './ChatEntry.css';
import PropTypes from 'prop-types';
import TimeStamp from './TimeStamp';

const ChatEntry = ({ id, sender, body, timeStamp, liked, onUpdateLikes }) => {
const chatTime = <TimeStamp time={timeStamp} />;
const localOrRemote = id % 2 === 0 ? 'remote' : 'local';
const updateLikes = (id) => {
onUpdateLikes(id);
};
const heartLiked = liked ? '❤️' : '🤍';

const ChatEntry = (props) => {
return (
<div className="chat-entry local">
<h2 className="entry-name">Replace with name of sender</h2>
<div key={id} className={`chat-entry ${localOrRemote}`}>
<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">{chatTime}</p>
<button
onClick={() => {
updateLikes(id);
}}
className="like"
>
{heartLiked}
</button>
</section>
</div>
);
};

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

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

const ChatLog = ({ entries, onUpdateLikes }) => {
const getChatLogJSX = (entries, onUpdateLikes) => {
return entries.map((chat) => {
return (
<ChatEntry
key={chat.id}
id={chat.id}
sender={chat.sender}
body={chat.body}
timeStamp={chat.timeStamp}
liked={chat.liked}
onUpdateLikes={onUpdateLikes}
/>
);
});
};
return <ul className="chat-log">{getChatLogJSX(entries, onUpdateLikes)}</ul>;
};

ChatLog.propTypes = {
entries: PropTypes.arrayOf(
PropTypes.shape({
id: PropTypes.number,
sender: PropTypes.string.isRequired,
body: PropTypes.string.isRequired,
timeStamp: PropTypes.string.isRequired,
liked: PropTypes.bool,
})
).isRequired,
onUpdateLikes: PropTypes.func,
};

export default ChatLog;