Skip to content

C22 Sphinx - Aleida Vieyra #37

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

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
14 changes: 13 additions & 1 deletion src/App.css
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,21 @@
#App header {
background-color: #222;
color: #fff;
padding-bottom: 0.5rem;
padding: 5 rem;
position: fixed;
width: 100%;
z-index: 100;
text-align: center;
align-items: center;
}

#App .likes-background-color {
background-color: #87cefa;
margin: 0;
border-style: dotted;
border-color: whitesmoke;
}

#App main {
padding-left: 2em;
padding-right: 2em;
Expand All @@ -30,6 +37,11 @@
background-color: #e0ffff;
}

#App .header-fonts {
font-family: system-ui, -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Oxygen, Ubuntu, Cantarell, 'Open Sans', 'Helvetica Neue', sans-serif;
font-style:italic;
}

#App .widget {
display: inline-block;
line-height: 0.5em;
Expand Down
56 changes: 50 additions & 6 deletions src/App.jsx
Original file line number Diff line number Diff line change
@@ -1,17 +1,61 @@
import './App.css';
import ChatLog from '/src/components/ChatLog';
import DATA from '/src/data/messages.json';
import { useState } from 'react';


function App() {
const [entries, setEntries] = useState(DATA);
const [colors, setTextColor] = useState(DATA);

const toggleLikeBtnClick= (id) => {
// Refactoring to incorporate func passing state update
setEntries(entries => entries.map(entry=>{
if (entry.id == id){
return {...entry, liked: !entry.liked};
}
else{
return entry;
}
Comment on lines +13 to +19

Choose a reason for hiding this comment

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

Suggested change
setEntries(entries => entries.map(entry=>{
if (entry.id == id){
return {...entry, liked: !entry.liked};
}
else{
return entry;
}
setEntries(entries => entries.map(entry => {
if (entry.id == id) {
return {...entry, liked: !entry.liked};
} else {
return entry;
}

}));
};
Comment on lines +11 to +21

Choose a reason for hiding this comment

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

Nice work on this function! By passing this down to your individual chat messages you are now able to have a single source of truth!


const totalLikesCount = entries.reduce((count, entry) => (entry.liked? count+1: count), 0);

Choose a reason for hiding this comment

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

Perfect use of the reduce function!


const identifyAllChatMembers = () => {
const senders = [...new Set(entries.map(entry => entry.sender))];
let participant1 = '';
let participant2 = '';
for (const sender of senders) {
if (!participant1) {
participant1 = sender;
}
else if (participant1 && !participant2) {
participant2 = sender;
}
};
return `${participant1} and ${participant2}`;
};
Comment on lines +25 to +38
Copy link

@mikellewade mikellewade Jan 4, 2025

Choose a reason for hiding this comment

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

Essentially we are just getting the sender values of the first and second message objects in the list of messages. Instead of looping through the messages and making a set from their senders we could simply access them and then store in a string like so:

const participant1 = entries[0].sender;
const participant2 = entries[1].sender;
const chatTitle = `${participant1} and ${participant2}`;

EDIT: I see now that your method would work in cases where the maybe the first two messages are from a single user. Double texting, I could never! haha



const App = () => {
return (
<div id="App">
<header>
<h1>Application title</h1>
<header className='header-fonts'>
<h1 > Chat Between {identifyAllChatMembers()}</h1>
<h2 className='likes-background-color'> {totalLikesCount} ❤️s</h2>
</header>
<main>
{/* Wave 01: Render one ChatEntry component
Wave 02: Render ChatLog component */}
{/* // Wave 01: Render one ChatEntry component */}
{/* Wave 02: Render ChatLog component */}

<ChatLog
entries={entries}
onLikeBtnToggle={toggleLikeBtnClick}
/>
Comment on lines +51 to +54

Choose a reason for hiding this comment

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

perf!



</main>
</div>
);
};

export default App;
32 changes: 25 additions & 7 deletions src/components/ChatEntry.jsx
Original file line number Diff line number Diff line change
@@ -1,20 +1,38 @@
import PropTypes from 'prop-types';
import TimeStamp from './TimeStamp';
import './ChatEntry.css';

const ChatEntry = () => {

const ChatEntry = ({id, sender, body, timeStamp, liked, onLikeToggle}) => {
const likeButtonClicked = () => {
onLikeToggle(id);
};

const locateSender = () => {
return (sender === 'Estragon')? 'remote': 'local';
};

return (
<div className="chat-entry local">
<h2 className="entry-name">Replace with name of sender</h2>
<div className={`chat-entry ${locateSender()}`}>
<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={likeButtonClicked}>{liked? '❤️': '🤍'}</button>

Choose a reason for hiding this comment

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

Suggested change
<button className='like' onClick={likeButtonClicked}>{liked? '❤️': '🤍'}</button>
<button className='like' onClick={likeButtonClicked}>{liked ? '❤️': '🤍'}</button>

Copy link

@mikellewade mikellewade Jan 4, 2025

Choose a reason for hiding this comment

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

The button function could also be implemented like so:

<button className='like' onClick={() => likeButtonClicked(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,
onLikeToggle: PropTypes.func.isRequired,
Comment on lines +30 to +35

Choose a reason for hiding this comment

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

};

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

const ChatLog = ({entries, onLikeBtnToggle}) =>{
const chatComponents = entries.map((entry) => {
return(
<ChatEntry
{...entry}
onLikeToggle={onLikeBtnToggle}
key={entry.id}
/>
);
});
Comment on lines +6 to +14

Choose a reason for hiding this comment

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

okay spread syntax! 😍



return (
<>
<ul className='chat-log'>
{chatComponents}
</ul>
</>
);
};

ChatLog.propTypes = {
// Implement
entries: PropTypes.arrayOf(PropTypes.shape({
sender: PropTypes.string.isRequired,
body: PropTypes.string.isRequired,
timeStamp: PropTypes.string.isRequired,
liked: PropTypes.bool.isRequired
})).isRequired,
onLikeBtnToggle: PropTypes.func.isRequired,
};
Comment on lines +26 to +35

Choose a reason for hiding this comment

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

⭐️


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,4 +1,4 @@
import ChatLog from './ChatLog';
import ChatLog from '/src/components/ChatLog';
import { render, screen } from '@testing-library/react';
import '@testing-library/jest-dom';

Expand Down
1 change: 0 additions & 1 deletion src/components/TimeStamp.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ const TimeStamp = (props) => {
const time = DateTime.fromISO(props.time);
const absolute = time.toFormat('MMMM Do YYYY, h:mm:ss a');
const relative = time.toRelative();

return <span title={absolute}>{relative}</span>;
};

Expand Down