Skip to content
Open
Show file tree
Hide file tree
Changes from 5 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
32 changes: 29 additions & 3 deletions src/App.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,42 @@
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 = () => {
const [messages, setMessages] = useState(chatMessages);

Choose a reason for hiding this comment

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

Nice, adding useState




const updateLike = (updatedmessage)=>{
const updatedmessages = messages.map(message =>{
if (message.id === updatedmessage.id){
return updatedmessage;
}else{
return message;
}
})
setMessages(updatedmessages);
}

const getHeartCount = () =>{

Choose a reason for hiding this comment

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

Excellent, dynamic updating!

let heartCount = 0;
for (const message of messages){
if (message.liked){
heartCount ++;
}
}
return heartCount;
}

return (
<div id="App">
<header>
<h1>Application title</h1>
<section><span className='widget' id='heartWidget'>{getHeartCount()} ❤️s</span></section>
</header>
<main>
{/* Wave 01: Render one ChatEntry component
Wave 02: Render ChatLog component */}
<ChatLog entries={messages} onupdate={updateLike}></ChatLog>
</main>
</div>
);
Expand Down
39 changes: 33 additions & 6 deletions src/components/ChatEntry.js
Original file line number Diff line number Diff line change
@@ -1,22 +1,49 @@
import React from 'react';
import React, { useState } from 'react';
import './ChatEntry.css';
import PropTypes from 'prop-types';
import { DateTime } from 'luxon';
import TimeStamp from './TimeStamp';

const ChatEntry = (props) => {
const [heart, setHeart] = useState('🤍');

const updateLike = () =>{
const updatedmessage = {
id:props.id,
sender:props.sender,
body:props.body,
timeStamp:props.timeStamp,
liked:!props.liked,
}
props.onupdate(updatedmessage)

if (!props.liked){
setHeart('❤️');
}
else{
setHeart('🤍');
}
};




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>
<button className="like" onClick={()=>{updateLike()}}>{heart}</button>
</section>
</div>
);
};

ChatEntry.propTypes = {
//Fill with correct proptypes
sender:PropTypes.string.isRequired,
body:PropTypes.string.isRequired,
timeStamp:PropTypes.string.isRequired,
};

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

const ChatLog = ({entries, onupdate})=>{


const chatentryComponents = entries.map(message =>{
return <ChatEntry onupdate={onupdate} liked={message.liked} id={message.id}sender={message.sender} body={message.body} timeStamp={message.timeStamp}/>;
});

return <section className='chat-log'>{chatentryComponents}</section>;
}

export default ChatLog;