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

Erina P. - Kunzite #98

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
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
3 changes: 3 additions & 0 deletions src/App.css
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,9 @@

#App header section {
background-color: #e0ffff;
color: #222;
font-size:1.0em;
font-weight: bold;
}

#App .widget {
Expand Down
34 changes: 30 additions & 4 deletions src/App.js
Original file line number Diff line number Diff line change
@@ -1,19 +1,45 @@
import React from 'react';
import './App.css';
import chatMessages from './data/messages.json';
import ChatLog from './components/ChatLog.js';
import { useState } from 'react';

Choose a reason for hiding this comment

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

Since we've already got a line that imports React, we want to avoid adding another line that duplicates it, so better to add to line 1:

import React, { useState } from 'react';


const App = () => {
const [entries, setEntriesData] = useState(chatMessages);
const updateLikedCount = (id) => {
setEntriesData((prev) => {
return prev.map((entry) => {
if(id === entry.id) {
return {
...entry,
liked: !entry.liked,
}; } else {

Choose a reason for hiding this comment

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

this else should be on a new line:

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

return entry;
}
});
});
}
const totalLikesTally = entries.reduce((total, entry) => {

Choose a reason for hiding this comment

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

Nice use of reduce() here!

if (entry.liked === true) {

Choose a reason for hiding this comment

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

It's ok to compare entry.liked to a boolean but it would be more common to just check for truthinees:

if (entry.liked) {

total += 1;
}
return total;
}, 0);

return (
<div id="App">
<header>
<h1>Application title</h1>
<h1>🤖 Chatterbot 🤖</h1>
<section className='header section'>{totalLikesTally} ❤️s</section>
</header>
<main>
{/* Wave 01: Render one ChatEntry component
Wave 02: Render ChatLog component */}
<ChatLog
className='chat-log'

Choose a reason for hiding this comment

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

Small thing but these props should be indented two spaces to the right

entries={entries}
updateLikedCount={updateLikedCount}/>
</main>
</div>
);
};

export default App;
export default App;
2 changes: 1 addition & 1 deletion src/components/ChatEntry.css
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ button {
}

.chat-entry .entry-bubble:hover {
background-color: #fefea2;
background-color: hsl(60, 98%, 82%);
}

.chat-entry .entry-name {
Expand Down
42 changes: 28 additions & 14 deletions src/components/ChatEntry.js
Original file line number Diff line number Diff line change
@@ -1,22 +1,36 @@
import React from 'react';
import './ChatEntry.css';
import PropTypes from 'prop-types';
import TimeStamp from './TimeStamp';
import PropTypes from 'prop-types';

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 onLikeButtonClick = () => {
props.updateLikedCount(props.id);
}

const formatMessages = props.sender === 'Vladimir' ? 'chat-entry local' : 'chat-entry remote';
return (
<div className={formatMessages}>
<h2 className="entry-name">{props.sender}</h2>
<section className="entry-bubble">
<p>{props.body}</p>
<p className="entry-time">
<TimeStamp time={props.timeStamp}/>
</p>
<button className='like'onClick={onLikeButtonClick}>
{props.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,
onUpdate: PropTypes.func
};

export default ChatEntry;
export default ChatEntry;
2 changes: 2 additions & 0 deletions src/components/ChatLog.css
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,5 @@
margin: auto;
max-width: 50rem;
}


36 changes: 36 additions & 0 deletions src/components/ChatLog.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import React from 'react';
import './ChatLog.css';
import PropTypes from 'prop-types';
import ChatEntry from './ChatEntry.js';

const ChatLog = (props) => {
const chatComponents = props.entries.map(entry => {

Choose a reason for hiding this comment

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

Learn showed this pattern of creating an array of React elements and then referencing it in a JSX expression, so it's definitely okay to do here, but I should just add that I have not found it to be common pattern in practice - most people would just return the array in the component's return statement, e.g.

return (
  <div>
    {props.entries.map(entry => (
      <ChatEntry
        key={entry.id} 
        id={entry.id}  
        sender={entry.sender}
        body={entry.body}
        liked={entry.liked}
        timeStamp={entry.timeStamp}
        updateLikedCount={props.updateLikedCount}
      />
    )}
  </div>
);

return (
<ChatEntry
key={entry.id}
id={entry.id}
sender={entry.sender}
body={entry.body}
liked={entry.liked}
timeStamp={entry.timeStamp}
updateLikedCount={props.updateLikedCount}/>
);
});
return (
<div>
{chatComponents}
</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
}))
};

export default ChatLog;