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

const App = () => {
return (
<div id="App">
<header>
<h1>Application title</h1>
</header>
<main>
{/* Wave 01: Render one ChatEntry component
Wave 02: Render ChatLog component */}
</main>
</div>
);
function App() {
const[messageData,setMessageData] = useState(chatMessages);
const getCountLikes = () => {
let likes = 0;
for (const message of messageData){
if (message.liked){
likes += 1;
}
};
return likes
};
const updateLikes = (updatedData) => {
const messages = messageData.map((message) => {
if (message.id === updatedData.id) {
return updatedData
}
else return message
});
setMessageData(messages);
}
return (
<div id="App">
<header>
<h1>Application title</h1>
<h2>{getCountLikes()}</h2>
</header>
<main>
{/* Wave 01: Render one ChatEntry component
Wave 02: Render ChatLog component */}
{/* <ChatLog chatMessages={[chatMessages]} */}
<ChatLog messageData={messageData} updateLikes={updateLikes}></ChatLog>
</main>
</div>
);
};

export default App;
2 changes: 1 addition & 1 deletion src/components/ChatEntry.css
Original file line number Diff line number Diff line change
Expand Up @@ -97,4 +97,4 @@ button {

.chat-entry.remote .entry-bubble:hover::before {
background-color: #a9f6f6;
}
}
38 changes: 34 additions & 4 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';
import { useState} from 'react';

const ChatEntry = (props) => {
const[isLiked, setLiked] = useState(false);

//Function to toggle Liked Heart
const toggleLiked = () => {
setLiked(!isLiked);
}
// Tenary Operator for Liked Heart change color
const like = isLiked ? '❤️': ' 🤍 ';

const likedMessage = () => {
const updatedMessage = {
id: props.id,
sender: props.sender,
body: props.body,
timeStamp: props.timeStamp,
liked: !props.liked,
};
props.updateLikes(updatedMessage);
setLiked((prevLike) => ! prevLike);
}

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>
<p className="entry-time"><TimeStamp time = {props.timeStamp}/></p>
<h2 onClick={likedMessage}>
{like}
</h2>
</section>
</div>
);
};

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

};

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 = (props) => {
const entryComponents = props.messageData.map((entries) => {
// console.log(entries)
return (
<ChatEntry
id = {entries.id}
sender = {entries.sender}
body = {entries.body}
time = {entries.timeStamp}
liked = {entries.liked}
updateLikes = {props.updateLikes}
></ChatEntry>
);
});
return entryComponents
};

ChatLog.propTypes = {
//Fill with correct proptypes
messageData: PropTypes.arrayOf(PropTypes.object),
};

export default ChatLog;