Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
20 commits
Select commit Hold shift + click to select a range
13e4537
Design a set of components and their relationships to the data
linakl19 Jun 11, 2025
08b7a57
Update the ChatEntry and App components to display a single chat mess…
linakl19 Jun 11, 2025
fbb4f12
Implement ChatLog component which takes one prop named entries
linakl19 Jun 14, 2025
b5626bd
Update the App component to display an entire chat log and comment si…
linakl19 Jun 14, 2025
d6db016
Lift state up to App component; implement and pass updateEntryLikedSt…
linakl19 Jun 14, 2025
aa13a28
Read and pass onToggleHeart from ChatLog to ChatEntry as a prop.
linakl19 Jun 14, 2025
ed72cde
Implement event handler to invoke onToggleLike() when an entry is liked.
linakl19 Jun 14, 2025
a195f16
Add total likes counter
linakl19 Jun 14, 2025
253e6fb
Add and remove white spaces when needed.
linakl19 Jun 14, 2025
7a08768
Designate ChatEntry as local or remote adding className.
linakl19 Jun 15, 2025
0dac9b0
Add colorData state and handleColorChange function to manage color ch…
linakl19 Jun 16, 2025
36cd833
Refactor style. Change bg color, colors, and font-size
linakl19 Jun 16, 2025
781f61e
Implement ColorChoice component and Add onChange event handler.
linakl19 Jun 16, 2025
39cc8a6
Refactor App to remove colorData array and use separate state for loc…
linakl19 Jun 16, 2025
8e0b9a1
Refactor ChatLog to pass local and remote color to ChatEntry
linakl19 Jun 16, 2025
3fded1c
Refactor ChatEntry to determine if entry is local or remote and apply…
linakl19 Jun 16, 2025
d0c6ae2
Remove wave 1 comment lines.
linakl19 Jun 16, 2025
35952ed
Refactor ChatEntry to use an isLocal prop instead of checking if id i…
linakl19 Jun 16, 2025
44d8477
Refactor ChatLog to determine isLocal using sender and pass chatColor…
linakl19 Jun 16, 2025
e733a5c
Refactor App to pass localSender as a prop to ChatLog.
linakl19 Jun 16, 2025
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
Binary file added images/chatLog-components.png

Choose a reason for hiding this comment

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

🤩👍

Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
22 changes: 15 additions & 7 deletions src/App.css
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
#App {
background-color: #87cefa;
background-color: #3d4852;
}

#App header {
background-color: #222;
color: #fff;
padding-bottom: 0.5rem;
background-color: #f7f8fa;
color: #3d4852;
position: fixed;
width: 100%;
z-index: 100;
Expand All @@ -27,7 +26,12 @@
}

#App header section {
background-color: #e0ffff;
color: #3d4852;
display: flex;
flex-direction: row;
justify-content: space-around;
align-items: center;
border: 3px solid #3d4852;
}

#App .widget {
Expand All @@ -49,6 +53,10 @@
display: inline-block
}

.black {
color:#222
}

.red {
color: #b22222
}
Expand All @@ -57,8 +65,8 @@
color: #e6ac00
}

.yellow {
color: #e6e600
.brown {
color: #735711
}

.green {
Expand Down
57 changes: 54 additions & 3 deletions src/App.jsx
Original file line number Diff line number Diff line change
@@ -1,14 +1,65 @@
import './App.css';
import ChatLog from './components/ChatLog';
import ColorChoice from './components/ColorChoice';
import DATA from './data/messages.json';
import { useState } from 'react';

const App = () => {
const [entryData, setEntryData] = useState(DATA);
const [localColor, setLocalColor] = useState('green');
const [remoteColor, setRemoteColor] = useState('blue');
Comment on lines +9 to +10

Choose a reason for hiding this comment

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

Instead of creating 2 separate pieces of state to track colors for the local user and the remote user, you could create just one piece of state that can keep track of both pieces of these info.

  const [usersColors, setUsersColors] = useState({
    local: 'green',
    remote: 'blue'
  });


const LOCAL_SENDER = entryData[0].sender;
const REMOTE_SENDER = entryData[1].sender;
Comment on lines +12 to +13

Choose a reason for hiding this comment

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

This works for our application right now! You'd need to come up with a different solution for how identifiying different senders if react-chatlog allowed for messages between more than 2 senders (like a big group chat between many friends).


const handleColorChange = (sender, color) => {
if (sender === LOCAL_SENDER){
setLocalColor(color);
} else if (sender === REMOTE_SENDER) {
setRemoteColor(color);
}
};

const updateEntryLikedState = (entryId) => {
setEntryData(entries => {

Choose a reason for hiding this comment

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

Nice use of the callback setter style. In this application, it doesn't really matter whether we use the callback style or the value style, but it's good practice to get in the habit of using the callback style.

return entries.map(entry => {

Choose a reason for hiding this comment

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

Nice use of map here to both handle making a new list so that React sees the message data has changed, and make new data for the clicked message with its like status toggled.

if (entry.id === entryId) {
return { ...entry, liked: !entry.liked };

Choose a reason for hiding this comment

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

We showed this approach in class, but technically, we're mixing a few responsibilities here. rather than this function needing to know how to change the liked status itself, we could move this update logic to a helper function. This would better mirror how we eventually update records when there's an API call involved.

In this project, our messages are very simple objects, but if we had more involved operations, it could be worthwhile to create an actual class with methods to work with them, or at least have a set of dedicated helper functions to centralize any such mutation logic.

} else {
return entry;
}
});
});
};

const totalLikes = entryData.reduce((sum, entry) => {
return entry.liked ? sum + 1 : sum;

Choose a reason for hiding this comment

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

Nice job using reduce! This is how most JS devs would go about calculating a total amount too.

Instead of using a ternary operator here, we could write something like:

Suggested change
return entry.liked ? sum + 1 : sum;
(totalLikes + message.liked)

Adding a boolean to a number treats true as 1 and 0 as false

}, 0);

return (
<div id="App">
<header>
<h1>Application title</h1>
<h1>Chat Between{' '}
<span className={localColor}>{LOCAL_SENDER}</span>{' '}
and{' '}
<span className={remoteColor}>{REMOTE_SENDER}</span>
Comment on lines +43 to +45

Choose a reason for hiding this comment

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

👍

</h1>

<section>
<ColorChoice sender={LOCAL_SENDER} chatColor={localColor} setColorCallback={handleColorChange} />
<span id='heartWidget' className='widget'>{totalLikes} ❤️s</span>
<ColorChoice sender={REMOTE_SENDER} chatColor={remoteColor} setColorCallback={handleColorChange} />
</section>
</header>

<main>
{/* Wave 01: Render one ChatEntry component
Wave 02: Render ChatLog component */}
<ChatLog
entries={entryData}
onToggleHeart={updateEntryLikedState}
chatLocalColor={localColor}
chatRemoteColor={remoteColor}
localSender={LOCAL_SENDER}
/>
</main>
</div>
);
Expand Down
6 changes: 4 additions & 2 deletions src/components/ChatEntry.css
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ button {

.chat-entry {
margin: 1rem;
font-size: 1.1rem;
}

.chat-entry:last-child {
Expand All @@ -31,13 +32,14 @@ button {
}

.chat-entry .entry-name {
font-size: medium;
font-size: 1.1rem;
margin-bottom: 0.5rem;
color: whitesmoke;
}

.chat-entry .entry-time {
color: #bbb;
font-size: x-small;
font-size: small;
margin-bottom: 0.1rem;
margin-right: 0.5rem;
}
Expand Down
30 changes: 23 additions & 7 deletions src/components/ChatEntry.jsx
Original file line number Diff line number Diff line change
@@ -1,20 +1,36 @@
import './ChatEntry.css';
import TimeStamp from './TimeStamp';
import PropTypes from 'prop-types';

const ChatEntry = ({ id, sender, body, timeStamp, liked, onToggleLike, isLocal, chatColor }) => {
const entryClass = `chat-entry ${isLocal ? 'local' : 'remote'}`;

const ChatEntry = () => {
return (
<div className="chat-entry local">
<h2 className="entry-name">Replace with name of sender</h2>
<div className={entryClass}>
<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 className={chatColor}>{body}</p>
<p className="entry-time"><TimeStamp time={timeStamp} /></p>

Choose a reason for hiding this comment

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

Nice job using the provided TimeStamp component

<button
className="like"
onClick={() => { onToggleLike(id); }}
>
{liked ? '❤️' : '🤍'}
</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,
onToggleLike: PropTypes.func.isRequired,
isLocal: PropTypes.bool.isRequired,
chatColor: PropTypes.string.isRequired
};

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

const ChatLog = ({ entries, onToggleHeart, chatLocalColor, chatRemoteColor, localSender}) => {
const chatEntryComponents = entries.map((entry) => {
const isLocal = entry.sender === localSender;
const chatColor = isLocal ? chatLocalColor : chatRemoteColor;
return (
<ChatEntry
key={entry.id}
id={entry.id}
sender={entry.sender}
body={entry.body}
timeStamp={entry.timeStamp}
liked={entry.liked}
onToggleLike={onToggleHeart}
isLocal={isLocal}
chatColor={chatColor}
></ChatEntry>
);
});

return (
<div className='chat-log'>
{chatEntryComponents}
</div>
);
};

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,
})
),
onToggleHeart: PropTypes.func.isRequired,
chatLocalColor: PropTypes.string.isRequired,
chatRemoteColor: PropTypes.string.isRequired,
localSender: PropTypes.string.isRequired
};

export default ChatLog;
11 changes: 11 additions & 0 deletions src/components/ColorChoice.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
label {
font-size: 1.1rem;
font-weight: 600;
}
select {
margin-left: 1rem;
cursor: pointer;
border-radius: 10px;
font-size: 1rem;
padding: 0.5rem;
}
30 changes: 30 additions & 0 deletions src/components/ColorChoice.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import PropTypes from 'prop-types';
import './ColorChoice.css';

const ColorChoice = ({sender, chatColor, setColorCallback}) => {
const handleColorChange = (event) => {
setColorCallback(sender, event.target.value);
};

return (
<label className={chatColor}>
{sender}&apos;s color:
<select name="colorSelect" defaultValue="chooseColor" onChange={handleColorChange}>
<option value="">Choose a color</option>
<option value="red" >🔴 Red</option>
<option value="orange">🟠 Orange</option>
<option value="brown">🟤 Brown</option>
<option value="green">🟢 Green</option>
<option value="blue">🔵 Blue</option>
<option value="purple">🟣 Purple</option>
</select>
</label>
);
};

ColorChoice.propTypes = {
sender: PropTypes.string.isRequired,
chatColor: PropTypes.string.isRequired,
setColorCallback: PropTypes.func.isRequired
};
export default ColorChoice;