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
24 changes: 21 additions & 3 deletions src/App.jsx
Original file line number Diff line number Diff line change
@@ -1,14 +1,32 @@
import { useState } from 'react';
import data from './data/messages.json';
import ChatLog from './components/ChatLog';
import './App.css';


const countLikes = (entries) => {
let counts = 0;
for( let index = 0; index < entries.length; index++){
if(entries[index].liked){
counts++;
}
}
return counts;
}

const App = () => {
const [entries, setEntries ] = useState(data);
const handleClick = (id) => {
const updatedEntries = entries.map((entry) => entry.id === id ? {...entry, liked: !entry.liked} : entry);
setEntries(updatedEntries);
};
return (
<div id="App">
<header>
<h1>Application title</h1>
<h1>{`${countLikes(entries)} ❤️s`}</h1>
</header>
<main>
{/* Wave 01: Render one ChatEntry component
Wave 02: Render ChatLog component */}
<ChatLog entries={entries} handleClick={handleClick}/>
</main>
</div>
);
Expand Down
27 changes: 22 additions & 5 deletions src/components/ChatEntry.jsx
Original file line number Diff line number Diff line change
@@ -1,20 +1,37 @@
import PropTypes from 'prop-types';
import TimeStamp from './TimeStamp';

import './ChatEntry.css';

const ChatEntry = () => {
const ChatEntry = (props) => {
const{
sender,
body,
id,
liked,
timeStamp,
handleClick,
} = props;
return (
<div className="chat-entry local">
<h2 className="entry-name">Replace with name of sender</h2>
<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={() => handleClick(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,
handleClick: PropTypes.func.isRequired,
};

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

import './ChatLog.css';

const ChatLog= (props) => {
const{
entries,
handleClick
} = props;

return (
<div className='chat-log'>
{
entries.length === 0 ? [] : entries.map((entry) => <ChatEntry key={entry.id} {...entry} handleClick={handleClick}/>)
}
</div>
);
};

ChatLog.propTypes = {
// Fill with correct proptypes
entries: PropTypes.array.isRequired,
handleClick: PropTypes.func.isRequired,
};

export default ChatLog;
2 changes: 1 addition & 1 deletion src/components/ChatLog.test.jsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import ChatLog from './ChatLog';
import { render, screen } from '@testing-library/react';
import '@testing-library/jest-dom';
import ChatLog from './ChatLog';

const LOG = [
{
Expand Down