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

Kassidy Buslach Sharks #98

Open
wants to merge 7 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
31 changes: 27 additions & 4 deletions src/App.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,39 @@
import React from 'react';
import React, { useState } from 'react';
import './App.css';
import chatMessages from './data/messages.json';
import ChatLog from './components/ChatLog';

const App = () => {
console.log(chatMessages);

Choose a reason for hiding this comment

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

Remove debugging print statements

const [chatsMessages, setChatState] = useState(chatMessages);
const changeLike = (id) => {
const newData = chatsMessages.map((chat) => {
if (chat.id === id) {
chat.liked = !chat.liked;
return chat;
}
return chat;
Comment on lines +13 to +15

Choose a reason for hiding this comment

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

Since you have return chat; inside and outside the if statement, but you need to return chat in both cases, you can delete line 13 and let line 15 do the returning (which will happen regardless of whether line 12 executes or not

});
return setChatState(newData);
};
const sumLikes = () => {
const likedTrue = chatsMessages.filter((chat) => {
return chat.liked === true;
});
return likedTrue.length;
Comment on lines +20 to +23

Choose a reason for hiding this comment

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

I like this logic in a method so that you can invoke the method when line 30 renders

};

return (
<div id="App">
<header>
<h1>Application title</h1>
<h1>Chats</h1>
<h3>{sumLikes()} ❤️'s</h3>
</header>
<main>
{/* Wave 01: Render one ChatEntry component
Wave 02: Render ChatLog component */}
<ChatLog
chatsMessages={chatsMessages}
onChangeLike={changeLike}
></ChatLog>
</main>
</div>
);
Expand Down
35 changes: 30 additions & 5 deletions src/components/ChatEntry.js
Original file line number Diff line number Diff line change
@@ -1,22 +1,47 @@
import React from 'react';
import React, { useState } from 'react';
import './ChatEntry.css';
import PropTypes from 'prop-types';
import TimeStamp from './TimeStamp';

const ChatEntry = (props) => {
const [liked, setLike] = useState(false);
const changeHeart = !liked ? '🤍' : '❤️';
const changeLike = (id) => {
const likeState = setLike(!liked);
props.onChangeLike(id);

return likeState;
};

return (
<div className="chat-entry local">

Choose a reason for hiding this comment

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

Here we have the class chat-entry local hardcoded to this div so when I start up your react app, all the dialogue bubbles are on the left side. We need a way to dynamically change the class between chat-entry local and chat-entry remote.

How would you use a variable to reference the different classes depending on if the ChatEntry belongs to Vladimir or Estragon?

Consider checking to see if the value of sender is 'Vladimir', if it is then the value of the class should be chat-entry local otherwise it should be chat-entry remote. How would you use a ternary to do this?

<h2 className="entry-name">Replace with name of sender</h2>
<h2 className="entry-name">{props.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>{props.body}</p>
<p className="entry-time">
<TimeStamp time={props.timeStamp}> years ago</TimeStamp>
</p>
<button
onClick={() => {
changeLike(props.id);
}}
className="like"
>
{changeHeart}
</button>
</section>
</div>
);
};

ChatEntry.propTypes = {
//Fill with correct proptypes
sender: PropTypes.string.isRequired,
body: PropTypes.string.isRequired,
timeStamp: PropTypes.string.isRequired,
id: PropTypes.number.isRequired,
liked: PropTypes.bool.isRequired,
onChangeLike: PropTypes.func.isRequired,
Comment on lines +39 to +44

Choose a reason for hiding this comment

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

👍

};

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

const ChatLog = (props) => {
const chatMessageArray = props.chatsMessages.map((chat) => {
return (
<ChatEntry
sender={chat.sender}
body={chat.body}
timeStamp={chat.timeStamp}
id={chat.id}
key={chat.id}
liked={chat.liked}
onChangeLike={props.onChangeLike}
/>
);
});
return chatMessageArray;
};

ChatLog.propTypes = {
chatsMessages: PropTypes.arrayOf(PropTypes.object).isRequired,

Choose a reason for hiding this comment

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

Since we know the shape of the object that is in the array, we should explicitly validate the shape of the object here for an added layer of validation to prevent your protect your program from errors.

Also note that you have .isRequired on arrayOf() but you'll also. need to have .isRequired on the elements inside arrayOf()

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

onChangeLike: PropTypes.func.isRequired,
};

export default ChatLog;