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

Zoisite - Jackie Aponte #104

Open
wants to merge 6 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
4 changes: 4 additions & 0 deletions src/App.css
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,10 @@

#App header section {
background-color: #e0ffff;
color: black;
font-size: 1.5em;
padding-top: 0.5rem;
padding-bottom: 0.5rem;
}

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


const App = () => {
const localUser = chatMessages[0].sender
const remoteUser = chatMessages[1].sender
Comment on lines +8 to +9

Choose a reason for hiding this comment

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

What if textMessage[0] and textMessage[1] were both messages from the same person? How else could you find a way to get the names of the senders?


const [chatData, setChatData] = useState(chatMessages);

Choose a reason for hiding this comment

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

👍 nice job setting up state for chat messages.


const onUpdateChat = (updatedChat) => {
const messages = chatData.map(chat => {
if (chat.id === updatedChat.id) {
return updatedChat;
} else {
return chat;
}
});
Comment on lines +13 to +20

Choose a reason for hiding this comment

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

Rather than receiving a new entry with it's like status already changed here, rewrite this method to accept only an id. The logic about how to copy the message and change the liked status would be better if it was written in App.js, since the state must have all the information necessary to make a new message.

const onUpdateChat = id => {
const messages = chatData.map(chat => {
      if (chat.id === id){
        return {...chat, liked: !chat.liked};
      } 
      else {
        return chat;
      }
    });


setChatData(messages);

Choose a reason for hiding this comment

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

👀 Since the next version of the chat data depends on the current version, prefer using the callback style of setChatMessagData:

setTextMessage(prevEntries => ( 
    // logic to use current chats to make a new copy with the desired liked status flipped (the map logic above) 
    // return the new chats to update the state 
))

You can see an example of how we did it in Flasky here

};

let totalLikes = 0
for (const message of chatData) {
if (message.liked) {
totalLikes +=1
};
};
Comment on lines +25 to +30

Choose a reason for hiding this comment

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

We can use the reduce method to calculate the total likes too:

  const totalLikes = () => { 
    return chatData.reduce((total, text) => {
      return text.liked ? total + 1 : total;
    }, 0)
  };



return (
<div id="App">
<header>
<h1>Application title</h1>
<h1>Chat between {localUser} and {remoteUser}</h1>
<section>{totalLikes} ❤️s</section>
</header>
<main>
{/* Wave 01: Render one ChatEntry component
Wave 02: Render ChatLog component */}
<ChatLog
chatData={chatData}
onUpdateChat={onUpdateChat}>
Comment on lines +41 to +42

Choose a reason for hiding this comment

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

👍

</ChatLog>
</main>
</div>
);
Expand Down
33 changes: 27 additions & 6 deletions src/components/ChatEntry.js
Original file line number Diff line number Diff line change
@@ -1,22 +1,43 @@
import React from 'react';
import './ChatEntry.css';
import TimeStamp from './TimeStamp.js';
import PropTypes from 'prop-types';

const ChatEntry = (props) => {
const timeEntry=props.timeStamp

Choose a reason for hiding this comment

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

Nitpick - there should be whitespace around the equal sign. When we pass props to a component, then by convention we should omit the spaces around the equal sign, but when we're declaring variables we should have them.


const onLikeButtonClick = () => {
const updatedChat = {
id: props.id,
sender: props.sender,
body: props.body,
timeStamp: props.timeStamp,
liked: !props.liked
};

props.onUpdateChat(updatedChat);
};
Comment on lines +9 to +19

Choose a reason for hiding this comment

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

When you just pass in an id to props.onUpdateChat(), then this method can be refactored to just be one line:

const onLikeButtonClick = () => {
    props.onUpdateChat(props.id);
};


const heartColor = props.liked ? '❤️' : '🤍';

return (
<div className="chat-entry local">
<h2 className="entry-name">Replace with name of sender</h2>
<div className={props.chatEntry}>
Comment on lines +10 to +24

Choose a reason for hiding this comment

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

Instead of getting the class 'remote' or 'local' from props, we can calculate it here.

<div className={props.sender === 'Estragon' ? 'chat-entry remote' : 'chat-entry local'}>

<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={timeEntry}></TimeStamp></p>

Choose a reason for hiding this comment

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

Nice job passing the correct prop to the provided TimeStamp component 👍

<button className="like" onClick={onLikeButtonClick}>{heartColor}</button>
</section>
</div>
);
};

ChatEntry.propTypes = {
//Fill with correct proptypes
sender: PropTypes.string.isRequired,
body: PropTypes.string.isRequired,
timeStamp: PropTypes.string.isRequired,
liked: PropTypes.bool,
onUpdateChat: PropTypes.func.isRequired
Comment on lines +36 to +40

Choose a reason for hiding this comment

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

The PropType check is missing id and chatEntry that are passed into each ChatEntry component on lines 14 and 20 of ChatLog.js

};

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

const ChatLog = (props) => {
if (!props.chatData) {
return null
}
Comment on lines +7 to +9

Choose a reason for hiding this comment

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

We don't need this check here. If a value is undefined, React will just render a blank screen

const ChatLogEntries = props.chatData.map((entry) => {
return (
<div key={entry.id}>
<ChatEntry
id={entry.id}
sender={entry.sender}
timeStamp={entry.timeStamp}
body={entry.body}
liked={entry.liked}
onUpdateChat={props.onUpdateChat}
chatEntry={entry.sender===props.chatData[0].sender ? 'chat-entry local' : 'chat-entry remote'}
></ChatEntry>
</div>
);
});
Comment on lines +10 to +24

Choose a reason for hiding this comment

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

Small note that variables don't begin with a capital letter. The prop key should go on each ChatEntry, not on the entire div. When you have an array of components (like you have an array of ChatEntries), each component in the array gets a prop key.

Also, instead of writing a method ChatLogEntries to reference the return value from calling map() on props.chatData, it's more common to directly iterate over the chat entries in your component like this without using a variable:

const ChatLog = (props) => {
    return (
     <div>
        {props.entries.map((message) => (
            <ChatEntry 
                key={entry.id} // the key should go on each ChatEntry
                id={entry.id}
                sender={entry.sender}
                body={entry.body}
                timeStamp={entry.timeStamp}
                liked={entry.liked}
                onUpdateChat={props.onUpdateChat}
            />
        ))}
    </div>
    )
};

The logic on line 20 to determine the class name for remote or local should live in ChatEntry.js I think you can omit line 20 completely and have this logic live in the child component. I think this because each child component should know who its sender is and thus should be in charge of determining the class.


return (
<div>
{ChatLogEntries}
</div>
);
};

ChatLog.propTypes = {
chat: PropTypes.arrayOf(PropTypes.shape({
id: PropTypes.number.isRequired,
senderData: PropTypes.string.isRequired,
bodyData: PropTypes.string.isRequired,
likedData: PropTypes.bool.isRequired
})),
onUpdateChat: PropTypes.func.isRequired
};
Comment on lines +33 to +41

Choose a reason for hiding this comment

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

The PropType check here is just a little off. chat should be called chatData because that's what you named the prop you pass to ChatLog (see App.js line 41). We need to make sure that timeStamp is also included and that chatData has .isRequired too.

Also, prefer to name the keys the same as they appear in messages.json so that senderData is just sender (like it appears in the data file.

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


export default ChatLog