From 39b96d9e72f26e7c1d0e9b5568f97a99f4819e2b Mon Sep 17 00:00:00 2001 From: GG_Cugs Date: Tue, 21 Mar 2023 15:21:52 +0100 Subject: [PATCH 1/3] first changes --- code/src/App.js | 20 ++++++++++++++++++-- code/src/components/displayMessage.js | 1 + code/src/components/likeButton.js | 1 + code/src/components/postMessage.js | 1 + 4 files changed, 21 insertions(+), 2 deletions(-) create mode 100644 code/src/components/displayMessage.js create mode 100644 code/src/components/likeButton.js create mode 100644 code/src/components/postMessage.js diff --git a/code/src/App.js b/code/src/App.js index f2007d229..b70185a94 100644 --- a/code/src/App.js +++ b/code/src/App.js @@ -1,9 +1,25 @@ -import React from 'react'; +import React, { useState, useEffect } from 'react'; export const App = () => { + const [messageList, setMessageList] = useState([]) + const [loading, setLoading] = useState(false) + + const fetchMessages = () => { + setLoading(true); + fetch('https://happy-thoughts-ux7hkzgmwa-uc.a.run.app/thoughts') + .then((res) => res.json()) + .then((data) => setMessageList(data)) + .catch((error) => console.error(error)) + .finally(() => setLoading(false)) + } + + useEffect(() => { + fetchMessages(); + }, []) return (
- Find me in src/app.js!
); } + +/* npm install --save date-fns */ diff --git a/code/src/components/displayMessage.js b/code/src/components/displayMessage.js new file mode 100644 index 000000000..4eee36c68 --- /dev/null +++ b/code/src/components/displayMessage.js @@ -0,0 +1 @@ +import React from "react"; \ No newline at end of file diff --git a/code/src/components/likeButton.js b/code/src/components/likeButton.js new file mode 100644 index 000000000..4eee36c68 --- /dev/null +++ b/code/src/components/likeButton.js @@ -0,0 +1 @@ +import React from "react"; \ No newline at end of file diff --git a/code/src/components/postMessage.js b/code/src/components/postMessage.js new file mode 100644 index 000000000..4eee36c68 --- /dev/null +++ b/code/src/components/postMessage.js @@ -0,0 +1 @@ +import React from "react"; \ No newline at end of file From 6749885078bc72c9d73f1eacfd8f75a80dc4e2f5 Mon Sep 17 00:00:00 2001 From: GG_Cugs Date: Thu, 23 Mar 2023 14:29:14 +0100 Subject: [PATCH 2/3] nothing works, getting sad --- code/.eslintrc.json | 1 + code/package-lock.json | 18 +++++ code/package.json | 1 + code/src/App.js | 70 +++++++++++++++++-- code/src/components/displayMessage.js | 1 - .../displayMessage/displayMessage.js | 31 ++++++++ .../displayMessage/displaymessage.css | 3 + code/src/components/likeButton.js | 1 - code/src/components/postMessage.js | 1 - .../src/components/postMessage/postMessage.js | 17 +++++ .../components/postMessage/postmessage.css | 3 + 11 files changed, 137 insertions(+), 10 deletions(-) delete mode 100644 code/src/components/displayMessage.js create mode 100644 code/src/components/displayMessage/displayMessage.js create mode 100644 code/src/components/displayMessage/displaymessage.css delete mode 100644 code/src/components/likeButton.js delete mode 100644 code/src/components/postMessage.js create mode 100644 code/src/components/postMessage/postMessage.js create mode 100644 code/src/components/postMessage/postmessage.css diff --git a/code/.eslintrc.json b/code/.eslintrc.json index c9c0675c3..3905eed2b 100644 --- a/code/.eslintrc.json +++ b/code/.eslintrc.json @@ -26,6 +26,7 @@ "react-hooks" ], "rules": { + "linebreak-style": 0, "react/function-component-definition": [ 2, { diff --git a/code/package-lock.json b/code/package-lock.json index f7e97e1e9..fab8e56d2 100644 --- a/code/package-lock.json +++ b/code/package-lock.json @@ -9,6 +9,7 @@ "version": "1.0.0", "dependencies": { "babel-eslint": "^10.1.0", + "date-fns": "^2.29.3", "eslint": "^8.21.0", "eslint-config-airbnb": "^19.0.4", "eslint-plugin-import": "^2.26.0", @@ -6205,6 +6206,18 @@ "node": ">=10" } }, + "node_modules/date-fns": { + "version": "2.29.3", + "resolved": "https://registry.npmjs.org/date-fns/-/date-fns-2.29.3.tgz", + "integrity": "sha512-dDCnyH2WnnKusqvZZ6+jA1O51Ibt8ZMRNkDZdyAyK4YfbDwa/cEmuztzG5pk6hqlp9aSBPYcjOlktquahGwGeA==", + "engines": { + "node": ">=0.11" + }, + "funding": { + "type": "opencollective", + "url": "https://opencollective.com/date-fns" + } + }, "node_modules/debug": { "version": "4.3.4", "resolved": "https://registry.npmjs.org/debug/-/debug-4.3.4.tgz", @@ -21996,6 +22009,11 @@ "whatwg-url": "^8.0.0" } }, + "date-fns": { + "version": "2.29.3", + "resolved": "https://registry.npmjs.org/date-fns/-/date-fns-2.29.3.tgz", + "integrity": "sha512-dDCnyH2WnnKusqvZZ6+jA1O51Ibt8ZMRNkDZdyAyK4YfbDwa/cEmuztzG5pk6hqlp9aSBPYcjOlktquahGwGeA==" + }, "debug": { "version": "4.3.4", "resolved": "https://registry.npmjs.org/debug/-/debug-4.3.4.tgz", diff --git a/code/package.json b/code/package.json index 68869f589..4cbbdb2d2 100644 --- a/code/package.json +++ b/code/package.json @@ -4,6 +4,7 @@ "private": true, "dependencies": { "babel-eslint": "^10.1.0", + "date-fns": "^2.29.3", "eslint": "^8.21.0", "eslint-config-airbnb": "^19.0.4", "eslint-plugin-import": "^2.26.0", diff --git a/code/src/App.js b/code/src/App.js index b70185a94..f7db49a93 100644 --- a/code/src/App.js +++ b/code/src/App.js @@ -1,9 +1,16 @@ +/* eslint-disable no-shadow */ +/* eslint-disable no-unused-vars */ +/* eslint-disable no-underscore-dangle */ +/* eslint-disable max-len */ import React, { useState, useEffect } from 'react'; +import { DisplayMessage } from './components/displayMessage/displayMessage.js' +import { PostMessage } from './components/postMessage/postMessage.js' export const App = () => { const [messageList, setMessageList] = useState([]) const [loading, setLoading] = useState(false) - + const [newMessage, setNewMessage] = useState(''); + const fetchMessages = () => { setLoading(true); fetch('https://happy-thoughts-ux7hkzgmwa-uc.a.run.app/thoughts') @@ -16,10 +23,59 @@ export const App = () => { useEffect(() => { fetchMessages(); }, []) - return ( -
-
- ); -} -/* npm install --save date-fns */ + const handleNewMessage = (event) => { + setNewMessage(event.target.value) + } + + const onFormSubmit = (event) => { + event.preventDefault() + + // options that says how the information we send should be added into the API + const options = { + method: 'POST', + headers: { + 'Content-Type': 'application/json' + }, + body: JSON.stringify({ + message: newMessage + }) + } + // 2nd fetch request within OnFormSubmit + fetch('https://happy-thoughts-ux7hkzgmwa-uc.a.run.app/thoughts', options) + .then((result) => result.json()) + .then(() => fetchMessages()) + .finally(() => setNewMessage('')) + + // function to increase the likes + const LikeCounter = (LikeID) => { + // options-object decides how changes in the API should look + const options = { + method: 'POST', + headers: { + 'Content-Type': 'application/json' + } + } + fetch(`https://happy-thoughts-ux7hkzgmwa-uc.a.run.app/thoughts/${LikeID}/like`, options) + .then((result) => result.json()) + .then(() => fetchMessages()) + .catch((error) => console.log(error)) + .finally(() => { setLoading(false) }) + return ( +
+ +
+ +
+
+ ); + } + } +} \ No newline at end of file diff --git a/code/src/components/displayMessage.js b/code/src/components/displayMessage.js deleted file mode 100644 index 4eee36c68..000000000 --- a/code/src/components/displayMessage.js +++ /dev/null @@ -1 +0,0 @@ -import React from "react"; \ No newline at end of file diff --git a/code/src/components/displayMessage/displayMessage.js b/code/src/components/displayMessage/displayMessage.js new file mode 100644 index 000000000..ce530ee92 --- /dev/null +++ b/code/src/components/displayMessage/displayMessage.js @@ -0,0 +1,31 @@ +/* eslint-disable react/button-has-type */ +/* eslint-disable no-underscore-dangle */ +import React from 'react'; +import { formatDistance } from 'date-fns'; +import './displaymessage.css' + +export const DisplayMessage = ({ loading, messageList, LikeCounter }) => { + if (loading) { + return ( +
+

Loading in progress...

+
+ ) + } + return ( +
+ {messageList.map((list) => ( +
+

{list.message}

+
+
+ +

x {list.hearts}

+
+

{formatDistance(new Date(list.createdAt), Date.now(), { addSuffix: true })}

+
+
+ ))} +
+ ) +} \ No newline at end of file diff --git a/code/src/components/displayMessage/displaymessage.css b/code/src/components/displayMessage/displaymessage.css new file mode 100644 index 000000000..9d2b685b7 --- /dev/null +++ b/code/src/components/displayMessage/displaymessage.css @@ -0,0 +1,3 @@ +body { + width: fit-content; +} \ No newline at end of file diff --git a/code/src/components/likeButton.js b/code/src/components/likeButton.js deleted file mode 100644 index 4eee36c68..000000000 --- a/code/src/components/likeButton.js +++ /dev/null @@ -1 +0,0 @@ -import React from "react"; \ No newline at end of file diff --git a/code/src/components/postMessage.js b/code/src/components/postMessage.js deleted file mode 100644 index 4eee36c68..000000000 --- a/code/src/components/postMessage.js +++ /dev/null @@ -1 +0,0 @@ -import React from "react"; \ No newline at end of file diff --git a/code/src/components/postMessage/postMessage.js b/code/src/components/postMessage/postMessage.js new file mode 100644 index 000000000..27f60febb --- /dev/null +++ b/code/src/components/postMessage/postMessage.js @@ -0,0 +1,17 @@ +import React from 'react'; +import './postmessage.css'; + +export const PostMessage = ({ newMessage, onFormSubmit, handleNewMessage }) => { + const maxCharacterCount = 140; + + return ( +
+

What is making you happy right now?

+
+