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

Seattle Otters - Joanna #108

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



const App = () => {

const [entries, setEntries] = useState(chatMessages);
const toggleLiked = (id) => {
console.log('liking...');
const newEntries = entries.map((entry) => {
return entry.id === id
? {
id: entry.id,
sender: entry.sender,
body: entry.body,
timeStamp: entry.timeStamp,
liked: !entry.liked,
}
: entry;
});
setEntries(newEntries);
};

Choose a reason for hiding this comment

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

Great callback function!


const countLikes = (entries) => {
let likes = 0;
for (const entry of entries) {
if (entry.liked) {
likes++;
}
}
return likes;
};

return (
<div id="App">
<header>
<h1>Application title</h1>
<h1>Fun React Chat</h1>
<p>{countLikes(entries)} ❤️s</p>
</header>
<main>
{/* Wave 01: Render one ChatEntry component
Wave 02: Render ChatLog component */}
{/* <ChatLog entries={chatMessages}></ChatLog> */}
<ChatLog entries={entries} likedCallback={toggleLiked}></ChatLog>

</main>
</div>
);
Expand Down
51 changes: 44 additions & 7 deletions src/components/ChatEntry.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,58 @@ import React from 'react';
import './ChatEntry.css';
import PropTypes from 'prop-types';

const convertMsToDays = (ms) => {
const days = ms / 1000 / 60 / 60 / 24;
return Math.round(days);
};

const timeStampMessage = (days) => {
let msgStamp = 'unable to get timestamp';
if (days < 1) {
msgStamp = 'Today';
} else if (days >= 1 && days < 365) {
msgStamp = 'Today';
} else if (days >= 365) {
const yrs = Math.floor(days / 365);
msgStamp = `${yrs} years ago`;
}
return msgStamp;
}

Choose a reason for hiding this comment

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

I can see that you put a lot of work into this section, but just so you're aware, the Timestamp component that was provided with the project will do all of this work for you and render the correct relative time. It takes the timeStamp from the message data as a prop.


const ChatEntry = (props) => {
const msgTime = new Date(props.timeStamp);
const today = new Date();
const daysSinceMsg = convertMsToDays(today-msgTime);

const toggleLiked= props.likedCallback;

return (
<div className="chat-entry local">
<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>
<p>{props.body}</p>
<p className="entry-time">
{daysSinceMsg < 1
? msgTime.toLocaleTimeString([], {
hour: '2-digit',
minute: '2-digit',
})
: timeStampMessage(daysSinceMsg)}
</p>
<button className="like">🤍</button>

Choose a reason for hiding this comment

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

Removing this line causes all tests to pass.

Suggested change
<button className="like">🤍</button>

<button className="like" onClick={() => toggleLiked(props.id)}>

Choose a reason for hiding this comment

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

💯

{`${props.liked ? '❤️' : '🤍'}`}

Choose a reason for hiding this comment

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

This can be written slightly simpler:

Suggested change
{`${props.liked ? '❤️' : '🤍'}`}
{props.liked ? '❤️' : '🤍'}

Strings inside of {} in JSX will automatically render as text.

</button>
</section>
</div>
);
};

ChatEntry.propTypes = {
//Fill with correct proptypes

ChatEntry.propTypes = {
sender: PropTypes.string.isRequired,
body: PropTypes.string.isRequired,
timeStamp: PropTypes.string.isRequired,
likedCallback: PropTypes.func.isRequired,
};

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

const ChatLog = (props) => {
const entries = props.entries;
console.log(entries);

const entryLog = entries.map((entry) => {

Choose a reason for hiding this comment

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

👍

return (
<ChatEntry
key={entry.id ? entry.id : `${entry.sender}_${entry.timeStamp}`}
id={entry.id}
sender={entry.sender}
body={entry.body}
timeStamp={entry.timeStamp}
liked={entry.liked}
likedCallback={props.likedCallback}
></ChatEntry>
);
});
return <div className ="chat-log">{entryLog}</div>;
};
ChatLog.propTypes = {
entries: PropTypes.arrayOf(
PropTypes.shape({
sender: PropTypes.string.isRequired,
body: PropTypes.string.isRequired,
timeStamp: PropTypes.string.isRequired,
})
),
likedCallback: PropTypes.func.isRequired,
};

export default ChatLog;