Skip to content
Open
Show file tree
Hide file tree
Changes from all 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
52 changes: 48 additions & 4 deletions src/App.jsx
Original file line number Diff line number Diff line change
@@ -1,17 +1,61 @@
import './App.css';
import ChatLog from './components/ChatLog';
import ColorChoice from './components/ColorChoice';
import messagesData from './data/messages.json';
import { useState } from 'react';

const App = () => {
const [messages, setMessages] = useState(messagesData);
const [colors, setColors] = useState({
Vladimir: '#ffffe0',
Estragon: '#e0ffff'
});

const participants = [...new Set(messages.map(msg => msg.sender))];

const updateLikeStatus = (messageId, newLikedStatus) => {
setMessages(messages.map(message => {
if (message.id === messageId) {
return { ...message, liked: newLikedStatus };
}
return message;
}));
};

const handleColorChange = (user, newColor) => {
setColors(prevColors => ({
...prevColors,
[user]: newColor
}));
};

const totalLikes = messages.filter(message => message.liked).length;

return (
<div id="App">
<header>
<h1>Application title</h1>
<h1>Chat between {participants.join(' and ')}</h1>
<p>{totalLikes} ❤️s</p>
<div className="color-controls">
{participants.map(user => (
<ColorChoice
key={user}
user={user}
currentColor={colors[user]}
onColorChange={handleColorChange}
/>
))}
</div>
</header>
<main>
{/* Wave 01: Render one ChatEntry component
Wave 02: Render ChatLog component */}
<ChatLog
entries={messages}
onUpdateLike={updateLikeStatus}
colors={colors}
/>
</main>
</div>
);
};

export default App;
export default App;
35 changes: 26 additions & 9 deletions src/components/ChatEntry.jsx
Original file line number Diff line number Diff line change
@@ -1,20 +1,37 @@
import './ChatEntry.css';
import TimeStamp from './TimeStamp.jsx';
import PropTypes from 'prop-types';

const ChatEntry = ({ id, sender, body, timeStamp, liked, onUpdateLike, color = undefined }) => {
const messagesClass = sender === 'Vladimir' ? 'local' : 'remote';

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={`chat-entry ${messagesClass}`}>
<h2 className="entry-name">{sender}</h2>
<section
className="entry-bubble"
style={color ? { backgroundColor: color } : {}}
>
<p>{body}</p>
<p className="entry-time">
<TimeStamp time={timeStamp} />
</p>
<button onClick={() => onUpdateLike(id, !liked)} className="like">
{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,
onUpdateLike: PropTypes.func.isRequired,
color: PropTypes.string
};

export default ChatEntry;
export default ChatEntry;
2 changes: 2 additions & 0 deletions src/components/ChatEntry.test.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { render, screen } from '@testing-library/react';
import '@testing-library/jest-dom';

describe('Wave 01: ChatEntry', () => {
const mockOnUpdateLike = () => { };
beforeEach(() => {
render(
<ChatEntry
Expand All @@ -11,6 +12,7 @@ describe('Wave 01: ChatEntry', () => {
body="Get out by 8am. I'll count the silverware"
timeStamp="2018-05-18T22:12:03Z"
liked={false}
onUpdateLike={mockOnUpdateLike}
/>
);
});
Expand Down
38 changes: 38 additions & 0 deletions src/components/ChatLog.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import './ChatLog.css';
import ChatEntry from './ChatEntry';
import PropTypes from 'prop-types';

const ChatLog = ({ entries, onUpdateLike, colors = {} }) => {
return (
<div className="chat-log">
{entries.map((message) => (
<ChatEntry
key={message.id}
id={message.id}
sender={message.sender}
body={message.body}
timeStamp={message.timeStamp}
liked={message.liked}
onUpdateLike={onUpdateLike}
color={colors?.[message.sender]}
/>
))}
</div>
);
};

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

export default ChatLog;
6 changes: 4 additions & 2 deletions src/components/ChatLog.test.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,10 @@ const LOG = [
];

describe('Wave 02: ChatLog', () => {
const mockOnUpdateLike = () => { };

beforeEach(() => {
render(<ChatLog entries={LOG} />);
render(<ChatLog entries={LOG} onUpdateLike={mockOnUpdateLike} />);
});

test('renders without crashing and shows all the names', () => {
Expand All @@ -66,7 +68,7 @@ describe('Wave 02: ChatLog', () => {
});

test('renders an empty list without crashing', () => {
const element = render(<ChatLog entries={[]} />);
const element = render(<ChatLog entries={[]} onUpdateLike={mockOnUpdateLike} />);
expect(element).not.toBeNull();
});
});
23 changes: 23 additions & 0 deletions src/components/ColorChoice.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import PropTypes from 'prop-types';

const ColorChoice = ({ user, currentColor, onColorChange }) => {
return (
<div className="color-choice">
<label htmlFor={`color-${user}`}>{user}'s messages: </label>
<input
type="color"
id={`color-${user}`}
value={currentColor}
onChange={(e) => onColorChange(user, e.target.value)}
/>
</div>
);
};

ColorChoice.propTypes = {
user: PropTypes.string.isRequired,
currentColor: PropTypes.string.isRequired,
onColorChange: PropTypes.func.isRequired
};

export default ColorChoice;