diff --git a/src/App.js b/src/App.js
index c10859093..a39a0cfa8 100644
--- a/src/App.js
+++ b/src/App.js
@@ -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);
+};
+
+const countLikes = (entries) => {
+ let likes = 0;
+ for (const entry of entries) {
+ if (entry.liked) {
+ likes++;
+ }
+ }
+ return likes;
+};
+
return (
{/* Wave 01: Render one ChatEntry component
Wave 02: Render ChatLog component */}
+ {/* */}
+
+
);
diff --git a/src/components/ChatEntry.js b/src/components/ChatEntry.js
index b92f0b7b2..7b2003cfd 100644
--- a/src/components/ChatEntry.js
+++ b/src/components/ChatEntry.js
@@ -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;
+}
+
const ChatEntry = (props) => {
+ const msgTime = new Date(props.timeStamp);
+ const today = new Date();
+ const daysSinceMsg = convertMsToDays(today-msgTime);
+
+ const toggleLiked= props.likedCallback;
+
return (
-
Replace with name of sender
+
{props.sender}
- Replace with body of ChatEntry
- Replace with TimeStamp component
+ {props.body}
+
+ {daysSinceMsg < 1
+ ? msgTime.toLocaleTimeString([], {
+ hour: '2-digit',
+ minute: '2-digit',
+ })
+ : timeStampMessage(daysSinceMsg)}
+
+
);
};
-
-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;
\ No newline at end of file
diff --git a/src/components/ChatLog.js b/src/components/ChatLog.js
new file mode 100644
index 000000000..803aa5696
--- /dev/null
+++ b/src/components/ChatLog.js
@@ -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) => {
+ return (
+
+ );
+ });
+ return {entryLog}
;
+};
+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;
+
+
+