Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Kunzite - Angie Tran #111

Open
wants to merge 11 commits into
base: main
Choose a base branch
from
Open
31 changes: 29 additions & 2 deletions src/App.js
Original file line number Diff line number Diff line change
@@ -1,19 +1,46 @@
import React from 'react';
import './App.css';
import chatMessages from './data/messages.json';
import ChatLog from './components/ChatLog';
import { useState } from 'react';

const App = () => {

const [chatDATA, setchatDATA] = useState(chatMessages);

const onLikeUpdate = (updatedEntry) => {
const entries = chatDATA.map(entry => {
if (entry.id === updatedEntry.id) {
return updatedEntry;
} else {
return entry;
}
});
setchatDATA(entries);
};

const calTotalLikes = (chatDATA) => {
return chatDATA.reduce(
(likesCount, entry) => likesCount + entry.liked, 0);
};

const likesCount = calTotalLikes(chatDATA);

return (
<div id="App">
<header>
<h1>Application title</h1>
<h1>Chat between Vladimir and Estragon</h1>
<section>
<h2 id='heartWidget' className='widget'>{likesCount} ❤️s</h2>
</section>
</header>
<main>
<ChatLog entries={chatDATA} onLikeUpdate={onLikeUpdate}/>
{/* Wave 01: Render one ChatEntry component
Wave 02: Render ChatLog component */}
</main>
</div>
);
};

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

const ChatEntry = ({id, sender, body, timeStamp, liked, onLikeUpdate}) => {

const onLikeBtnClick = () => {
const updatedEntry = {
id: id,
sender: sender,
body: body,
timeStamp: timeStamp,
liked: !liked
}
onLikeUpdate(updatedEntry);
}

const heartStatus = liked ? '❤️' : '🤍';
const remoteStatus = sender === 'Estragon' ? 'remote' : 'local';

const ChatEntry = (props) => {
return (
<div className="chat-entry local">
<h2 className="entry-name">Replace with name of sender</h2>
<div className={`chat-entry ${remoteStatus}`}>
<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={onLikeBtnClick}>{heartStatus}</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,
onLikeUpdate: PropTypes.func
};

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

const ChatLog = ({entries, onLikeUpdate}) => {
return entries.map((entry) => {
return (
<ChatEntry
key={entry.id}
id={entry.id}
sender={entry.sender}
body={entry.body}
liked={entry.liked}
timeStamp={entry.timeStamp}
onLikeUpdate={onLikeUpdate}
/>
);
});
};

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

export default ChatLog;