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 - Allie S. #107

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

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>
);
// const [likesCount, setLikesCount] = useState(0);

Choose a reason for hiding this comment

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

Let's remove this line of code since we aren't using it anymore

Suggested change
// const [likesCount, setLikesCount] = useState(0);

const [entries, setEntries] = useState(chatMessages);

const sumHearts = () => {
let total = 0;

entries.forEach((entry) => {
if (entry.liked) {
total++;
}
});
return total;
};

const switchHeart = (updatedEntry) => {
const updatedEntries = entries.map((entry) => {
if (entry.id === updatedEntry.id) {
// entry.liked = !entry.liked;

Choose a reason for hiding this comment

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

Suggested change
// entry.liked = !entry.liked;


return { ...entry, liked: !entry.liked };
}
return entry;
});
setEntries(updatedEntries);
};

return (
<div id="App">
<header>
<h1>Allie's Chat App</h1>
<section className="black" id="heartWidget">
{sumHearts()} ❤️s
</section>
</header>

<main>
{/* Wave 01: Render one ChatEntry component */}

{/* Wave 02: Render ChatLog component */}
Comment on lines +44 to +46

Choose a reason for hiding this comment

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

Suggested change
{/* Wave 01: Render one ChatEntry component */}
{/* Wave 02: Render ChatLog component */}

<ChatLog entries={entries} onUpdateHeart={switchHeart}></ChatLog>
</main>
</div>
);
};

export default App;
52 changes: 41 additions & 11 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) => {
return (
<div className="chat-entry local">
<h2 className="entry-name">Replace with name of 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>
</section>
</div>
);
const [likePost, setLikePost] = useState(props.liked);

const toggleLike = () => {
const updatedEntry = {
id: props.id,
sender: props.sender,
body: props.body,
timeStamp: props.timeStamp,
liked: !props.liked,
};
setLikePost(!likePost);
Comment on lines +16 to +18

Choose a reason for hiding this comment

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

Looks like we are updating the chat message data from entries state using the switchHeart. Because of this, we aren't really using likePost or displaying it in our component.

Consider removing lines 8 and 18 completely

props.onUpdate(updatedEntry);
};

const heartState = props.liked ? '❤️' : '🤍';

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>{props.body}</p>
{/* <p className="entry-time">Replace with TimeStamp component</p> */}
Comment on lines +26 to +31

Choose a reason for hiding this comment

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

Suggested change
{/* <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>{props.body}</p>
{/* <p className="entry-time">Replace with TimeStamp component</p> */}
<h2 className="entry-name">{props.sender}</h2>
<section className="entry-bubble">
<p>{props.body}</p>

<p className="entry-time">
<TimeStamp time={props.timeStamp}></TimeStamp>
</p>
<button onClick={toggleLike} className="like">
{heartState}
</button>
</section>
</div>
);
};

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

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 ChatEntry from './ChatEntry';
import PropTypes from 'prop-types';

const ChatLog = (props) => {
const entries = props.entries;

const chatComponents = entries.map((message, index) => {
return (
<div className="chat-log" key={message.id}>
<ChatEntry
id={message.id}
sender={message.sender}
body={message.body}
timeStamp={message.timeStamp}
liked={message.liked}
onUpdate={props.onUpdateHeart}
></ChatEntry>
</div>
);
});
return chatComponents;
Comment on lines +9 to +22

Choose a reason for hiding this comment

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

This works! HTML-wise, it is creating a lot of extra div elements that we could clean up by doing the following:

const chatComponents = entries.map((message, index) => {
    return (
        <ChatEntry
            key={message.id}
            id={message.id}
            sender={message.sender}
            body={message.body}
            timeStamp={message.timeStamp}
            liked={message.liked}
            onUpdate={props.onUpdateHeart}
        />
    );
});
return (
    <div className="chat-log">
        {chatComponents}
    </div>
);

Now there is only one div parent surrounding all the ChatEntry components

};

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

export default ChatLog;