diff --git a/src/App.js b/src/App.js index c1085909..52a516d6 100644 --- a/src/App.js +++ b/src/App.js @@ -1,16 +1,42 @@ -import React from 'react'; +import React, { useState } from 'react'; import './App.css'; -import chatMessages from './data/messages.json'; +// import ChatEntry from './components/ChatEntry'; +import ChatLog from './components/ChatLog'; +import TotalHearts from './components/TotalHearts'; +import chatMessagesJSON from './data/messages.json'; + +const calcTotalLikes = (chatMessages) => { + return chatMessages.reduce((total, message) => { + return total + message.liked; + }, 0); +}; const App = () => { + const [chatMessages, setMessages] = useState(chatMessagesJSON); + + const onLike = (id) => { + const newMessages = chatMessages.map((message) => { + if (message.id === id) { + return { ...message, liked: !message.liked }; + } else { + return message; + } + }); + setMessages(newMessages); + }; + + const totalLikes = calcTotalLikes(chatMessages); + return (
-

Application title

+

Chat Log

+

+ +

- {/* 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..897a77b5 100644 --- a/src/components/ChatEntry.js +++ b/src/components/ChatEntry.js @@ -1,22 +1,51 @@ import React from 'react'; import './ChatEntry.css'; import PropTypes from 'prop-types'; +// import chatMessages from '../data/messages.json'; +// import { DateTime } from 'luxon'; +import TimeStamp from './TimeStamp'; + +// const messageIndex = 0; + +// const timeSince = (messageIndex) => { +// // return message time or how long ago message was sent if more than 1 year ago? +// const timeNow = DateTime.now(); +// const messageDate = new Date(chatMessages[messageIndex].timeStamp); +// // 31557600000 ms / year, returns the number of years since message was sent, rounded down. +// const resultTime = Math.floor((timeNow - messageDate) / 31557600000); +// console.log(resultTime); +// return resultTime; +// }; const ChatEntry = (props) => { + const handleLike = () => { + props.onLike(props.id); + }; + return (
-

Replace with name of sender

+

{props.sender}

-

Replace with body of ChatEntry

-

Replace with TimeStamp component

- +

{props.body}

+ {/*

{timeSince(messageIndex)} years ago

*/} +

+ +

+
); }; 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, + onLike: PropTypes.func, }; export default ChatEntry; diff --git a/src/components/ChatLog.js b/src/components/ChatLog.js new file mode 100644 index 00000000..013cbb4a --- /dev/null +++ b/src/components/ChatLog.js @@ -0,0 +1,32 @@ +import React from 'react'; +import PropTypes from 'prop-types'; +import ChatEntry from './ChatEntry'; + +// const messageIndex = 0; + +const ChatLog = (props, onLikeCallback) => { + const chatComponents = props.entries.map((chat, index) => { + return ( + + ); + }); + return ( +
+
{chatComponents}
+
+ ); +}; + +ChatLog.propTypes = { + entries: PropTypes.arrayOf(PropTypes.object), +}; + +export default ChatLog; diff --git a/src/components/TotalHearts.js b/src/components/TotalHearts.js new file mode 100644 index 00000000..3133be4a --- /dev/null +++ b/src/components/TotalHearts.js @@ -0,0 +1,22 @@ +import React from 'react'; +import PropTypes from 'prop-types'; + +const TotalHearts = (props) => { + const hearts = (numberOfLikes) => { + // let heartString = ''; + // for (let i = 0; i < numberOfLikes; i++) { + // heartString = `${heartString}❤️`; + // } + // console.log(numberOfLikes, heartString); + // return `${numberOfLikes} ❤️ s ${heartString}`; + return `${numberOfLikes} ❤️s`; + }; + + return
{hearts(props.totalLikes)}
; +}; + +TotalHearts.propTypes = { + totalLikes: PropTypes.number.isRequired, +}; + +export default TotalHearts;