Skip to content
31 changes: 30 additions & 1 deletion src/App.css
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
line-height: 0.5em;
border-radius: 10px;
color: black;
font-size:0.8em;
font-size: 0.8em;
padding-left: 1em;
padding-right: 1em;
}
Expand Down Expand Up @@ -71,4 +71,33 @@

.purple {
color: purple
}

.color-selection-container {
display: flex;
flex-direction: row;
justify-content: center;
align-items: center;
gap: 100px;
}

.color-selection-group {
display: flex;
flex-direction: column;
align-items: center;
gap: 5px;
}

.color-container {
display: flex;
flex-direction: row;
gap: 5px;
justify-content: center;
}

.test {
border: 1px solid white;
height: 25px;
width: 25px;
border-radius: 100%;
}
87 changes: 84 additions & 3 deletions src/App.jsx
Original file line number Diff line number Diff line change
@@ -1,14 +1,95 @@
import './App.css';
// import ChatEntry from './components/ChatEntry';
import ChatLog from './components/ChatLog';
import data from './data/messages.json';
import { useState } from 'react';

const App = () => {
const [chatData, setChatData] = useState(data);
const [fontColor, setFontColor] = useState(null);

const senderList = [];
const sendersList = (chatData) => {
for (const chat of chatData) {
if (!senderList.includes(chat.sender)) {
senderList.push(chat.sender);
}
}
let title = '';
for (const sender of senderList) {
title += `${sender} and `;
}
return title.slice(0, -5);
};

Comment on lines +11 to +24

Choose a reason for hiding this comment

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

Oh, so this function can include names from 2+ users! Nice!

const sendersTitle = sendersList(chatData);
const local = senderList[0];

const handleLike = (id) => {
setChatData((chatData) =>
chatData.map((chat) => {
if (chat.id === id) {
return { ...chat, liked: !chat.liked };
} else {
return chat;
}
})
);
};
Comment on lines +28 to +38

Choose a reason for hiding this comment

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

Nice work on this function! By passing this down to your individual chat messages you are now able to have a single source of truth!


const calculateLikes = (chatData) => {
let total = 0;
for (const chat of chatData) {
if (chat.liked) {
total += chat.liked;
}
}
return total;
};
Comment on lines +40 to +48

Choose a reason for hiding this comment

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

You could also do this with the reduce method like so:

const totalLikes = chatMessages.reduce((count, message) => count + (message.liked ? 1 : 0), 0);


const totalLikes = calculateLikes(chatData);

const colors = ['red', 'orange', 'yellow', 'green', 'blue', 'purple'];
const onFontColor = (color) => {
setFontColor(color);
};
const Colors = () => {
const divs = [];
for (let i = 0; i < colors.length; i++) {
const divClass = `${colors[i]} test`;
divs.push(
<div
key={i}
className={divClass}
style={{
backgroundColor: colors[i],
}}
onClick={() => onFontColor(colors[i])}
></div>
);
}
return <div className='color-container'>{divs}</div>;
};
Comment on lines +56 to +72

Choose a reason for hiding this comment

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

All the colors of the rainbow! 🌈


return (
<div id="App">
<header>
<h1>Application title</h1>
<h1>Chat Between {sendersTitle}</h1>
<div className="color-selection-container">
<h2>{totalLikes} ❤️s</h2>
<div className="color-selection-group">
<h2>Select Color:</h2>
<Colors />
</div>
</div>
</header>
<main>
{/* Wave 01: Render one ChatEntry component
Wave 02: Render ChatLog component */}
<ChatLog
entries={chatData}
onLike={handleLike}
localSender={local}
fontColor={fontColor}
></ChatLog>
Comment on lines +87 to +92

Choose a reason for hiding this comment

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

Parents passing on things to their children, we love it!

</main>
</div>
);
Expand Down
1 change: 1 addition & 0 deletions src/App.test.jsx
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
// import Chatlog from './Chatlog'
import App from './App';
import { render, screen, fireEvent } from '@testing-library/react';

Expand Down
4 changes: 4 additions & 0 deletions src/components/ChatEntry.css
Original file line number Diff line number Diff line change
Expand Up @@ -97,4 +97,8 @@ button {

.chat-entry.remote .entry-bubble:hover::before {
background-color: #a9f6f6;
}

.like {
color: red;
}
39 changes: 31 additions & 8 deletions src/components/ChatEntry.jsx
Original file line number Diff line number Diff line change
@@ -1,20 +1,43 @@
import './ChatEntry.css';
import TimeStamp from './TimeStamp';
import PropTypes from 'prop-types';

const ChatEntry = (props) => {
const onClickLike = () => {
props.onLike(props.id);
};
Comment on lines +6 to +8

Choose a reason for hiding this comment

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

The button function could also be implemented like so:

<button className='like' onClick={() => likeButtonClicked(id)}>{liked ? '❤️': '🤍'}</button>


const heart = props.liked ? '❤️' : '🤍';
const senderPlacement =
props.localSender == props.sender
? 'chat-entry local'
: 'chat-entry remote';
Comment on lines +11 to +14

Choose a reason for hiding this comment

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

⭐️


const ChatEntry = () => {
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>
<div className={senderPlacement}>
<h2 className='entry-name'>{props.sender}</h2>
<section className='entry-bubble'>
<p className={props.fontColor}>{props.body}</p>
<p className='entry-time'>
<TimeStamp time={props.timeStamp}></TimeStamp>
</p>
<button className='like' onClick={onClickLike}>
{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,
onLike: PropTypes.func,
localSender: PropTypes.string,
fontColor: PropTypes.string,
};

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

const ChatLog = ({ entries, onLike, localSender, fontColor }) => {
const message = entries.map((chat) => {
return (
<ChatEntry
key={chat.id}
id={chat.id}
sender={chat.sender}
body={chat.body}
timeStamp={chat.timeStamp}
liked={chat.liked}
onLike={onLike}
localSender={localSender}
fontColor={fontColor}
></ChatEntry>
Comment on lines +7 to +17

Choose a reason for hiding this comment

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

Since the names of the keys on entry are the same as the names your are setting on ChatEntry attributes/you are using all of the values in entry you could do something something like this to save you a few keystrokes:

  const chatComponents = entries.map((entry) => {
    return(
      <ChatEntry
        {...entry}
        onLikeToggle={onLikeBtnToggle}
        key={entry.id}
      />
    );
  });

Just recognize that it is not as explicit as what you have. Something to consider!

);
});

return <section>{message}</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,
})
).isRequired,
onLike: PropTypes.func,
localSender: PropTypes.string,
fontColor: PropTypes.string,
Comment on lines +25 to +36

Choose a reason for hiding this comment

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

};

export default ChatLog;
Loading