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

final chatlog #105

Open
wants to merge 4 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
33 changes: 28 additions & 5 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 entries from './data/messages.json';
import ChatLog from './components/ChatLog';
import ChatEntry from './components/ChatEntry';



const App = () => {
const [messages, setMessages] = useState(entries)

const numLikes = messages.filter((message) => message.liked).length;

// going to write a helper function for messages
const setLikes = (id) => {
const newMessages = messages.map((message) => {
if (id === message.id){
return {...message, liked: !message.liked};

}

return message
});

setMessages(newMessages)

}

return (
<div id="App">
<header>
<h1>Application title</h1>
<h1>chit chat</h1>
<p>number of likes {numLikes} ❤️s</p>
</header>
<main>
{/* Wave 01: Render one ChatEntry component
Wave 02: Render ChatLog component */}
<ChatLog entries={messages} setLikes={setLikes} />
</main>
</div>
);
Expand Down
27 changes: 18 additions & 9 deletions src/components/ChatEntry.js
Original file line number Diff line number Diff line change
@@ -1,22 +1,31 @@
import React from 'react';
import React, { useState } from 'react';
import './ChatEntry.css';
import PropTypes from 'prop-types';
import TimeStamp from './TimeStamp';

const ChatEntry = (props) => {
return (
<div className="chat-entry local">
<h2 className="entry-name">Replace with name of sender</h2>


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


return(
<div className = "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>
<TimeStamp time={props.timeStamp} />
<button className='like'onClick={()=>props.setLikes(props.id)}>{heart}</button>
</section>
</div>
</div >
);
};

ChatEntry.propTypes = {
//Fill with correct proptypes
body: PropTypes.string.isRequired,
sender: PropTypes.string.isRequired,
timeStamp: PropTypes.string.isRequired,
id: PropTypes.number.isRequired,
};

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


const ChatLog = (props) => {
const data = props.entries.map((entry, index) => {
return (
<li key={index}>
<ChatEntry
body={entry.body}
sender={entry.sender}
timeStamp={entry.timeStamp}
liked={entry.liked}
setLikes={props.setLikes}
id={entry.id}
></ChatEntry>
</li>
);
});
return (
<div>
<ul>
{data}
</ul>
</div>

);
};
ChatLog.propTypes = {
entries: PropTypes.arrayOf(PropTypes.shape({
body: PropTypes.string.isRequired,
sender: PropTypes.string.isRequired,
timeStamp: PropTypes.string.isRequired,
liked: PropTypes.bool.isRequired
}))
};

export default ChatLog;
8 changes: 4 additions & 4 deletions src/components/TimeStamp.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@ import { DateTime } from 'luxon';
import PropTypes from 'prop-types';

const TimeStamp = (props) => {
const time = DateTime.fromISO(props.time);
const absolute = time.toFormat('MMMM Do YYYY, h:mm:ss a');
const relative = time.toRelative();
const compTime = DateTime.fromISO(props.time);
const absolute = compTime.toFormat('MMMM Do YYYY, h:mm:ss a');
const relative = compTime.toRelative();

return <span title={absolute}>{relative}</span>;
return <span className="entry-time" title={absolute}>{relative}</span>;
};

TimeStamp.propTypes = {
Expand Down