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
3 changes: 3 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"liveServer.settings.port": 5501
}
36 changes: 32 additions & 4 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 ChatEntry from './components/ChatEntry';
import ChatLog from './components/ChatLog';
import chatMessages from './data/messages.json';

// const message = {
// id: 1,
// sender: 'Vladimir',
// body: 'why are you arguing with me',
// timeStamp: '2018-05-29T22:49:06+00:00',
// liked: false,
// };

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

Choose a reason for hiding this comment

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

Ah, nice use of useState to track changes!

const [numLiked, setNumLiked] = useState(0);

const updateChatHeart = (updatedChat) => {
const chats = chatData.map((chat) => {
if (chat.id === updatedChat.id) {
if (updatedChat.liked) {
setNumLiked(numLiked + 1);
} else {
setNumLiked(numLiked - 1);
}
return updatedChat;
} else {
return chat;
}
});
setChatData(chats);
};
return (
<div id="App">
<header>
<h1>Application title</h1>
<h1>OcelotChat</h1>
<h1>{numLiked} ❤️s</h1>
</header>
<main>
{/* Wave 01: Render one ChatEntry component
Wave 02: Render ChatLog component */}
<ChatLog entries={chatData} onUpdateChatHeart={updateChatHeart} />
</main>
</div>
);
Expand Down
46 changes: 38 additions & 8 deletions src/components/ChatEntry.js
Original file line number Diff line number Diff line change
@@ -1,22 +1,52 @@
import React from 'react';
import './ChatEntry.css';
import PropTypes from 'prop-types';
import TimeStamp from './TimeStamp';

const ChatEntry = ({
id,
sender,
body,
timeStamp,
liked,
onUpdateChatHeart,
}) => {
const updateChatEntry = () => {
const updatedChat = {
id,
sender,
body,
timeStamp,
liked: !liked,
};
onUpdateChatHeart(updatedChat);
};

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

const ChatEntry = (props) => {
return (
<div className="chat-entry local">
<h2 className="entry-name">Replace with name of sender</h2>
<div className="chat-entry local" id={id}>
<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 className="like" onClick={updateChatEntry}>
{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.isRequired,
onUpdateChatHeart: PropTypes.func.isRequired,
};

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

const ChatLog = ({ entries, onUpdateChatHeart }) => {
const chatEntries = entries.map((entry) => {
return (
<ChatEntry
key={entry.id}
id={entry.id}
sender={entry.sender}
body={entry.body}
timeStamp={entry.timeStamp}
liked={entry.liked}
onUpdateChatHeart={onUpdateChatHeart}
/>
);
});
return <section>{chatEntries}</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.isRequired,
})
),
onUpdateChatHeart: PropTypes.func.isRequired,
};

export default ChatLog;