From 910618e0c698ce4063ac6db63583412c72bf691f Mon Sep 17 00:00:00 2001 From: Joanna Date: Mon, 27 Jun 2022 00:16:53 -0700 Subject: [PATCH 1/2] Wave 3 --- src/App.js | 39 +++++++++++++++++++++++++++- src/components/ChatEntry.js | 51 ++++++++++++++++++++++++++++++++----- src/components/ChatLog.js | 39 ++++++++++++++++++++++++++++ 3 files changed, 121 insertions(+), 8 deletions(-) create mode 100644 src/components/ChatLog.js diff --git a/src/App.js b/src/App.js index c1085909..56830a89 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 (
-

Application title

+

Fun React Chat

+

{countLikes(entries)} ❤️s

{/* Wave 01: Render one ChatEntry component Wave 02: Render ChatLog component */} + + +
); diff --git a/src/components/ChatEntry.js b/src/components/ChatEntry.js index b92f0b7b..7b2003cf 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 00000000..803aa569 --- /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; + + + From 84de5cf55f652cde269d9485830782717bbfd3d8 Mon Sep 17 00:00:00 2001 From: Joanna Date: Mon, 27 Jun 2022 00:35:14 -0700 Subject: [PATCH 2/2] like issue fixed --- src/App.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/App.js b/src/App.js index 56830a89..a39a0cfa 100644 --- a/src/App.js +++ b/src/App.js @@ -45,8 +45,8 @@ const countLikes = (entries) => {
{/* Wave 01: Render one ChatEntry component Wave 02: Render ChatLog component */} - - + {/* */} +