diff --git a/package-lock.json b/package-lock.json index cefaccd..c3c5b8e 100644 --- a/package-lock.json +++ b/package-lock.json @@ -16,6 +16,7 @@ "firestore": "^1.1.6", "react": "^18.2.0", "react-dom": "^18.2.0", + "react-firebase-hooks": "^5.1.1", "react-router-dom": "^6.22.3", "react-scripts": "5.0.1", "web-vitals": "^2.1.4" @@ -15800,6 +15801,15 @@ "resolved": "https://registry.npmjs.org/react-error-overlay/-/react-error-overlay-6.0.11.tgz", "integrity": "sha512-/6UZ2qgEyH2aqzYZgQPxEnz33NJ2gNsnHA2o5+o4wW9bLM/JYQitNP9xPhsXwC08hMMovfGe/8retsdDsczPRg==" }, + "node_modules/react-firebase-hooks": { + "version": "5.1.1", + "resolved": "https://registry.npmjs.org/react-firebase-hooks/-/react-firebase-hooks-5.1.1.tgz", + "integrity": "sha512-y2UpWs82xs+39q5Rc/wq316ca52QsC0n8m801V+yM4IC4hbfOL4yQPVSh7w+ydstdvjN9F+lvs1WrO2VYxpmdA==", + "peerDependencies": { + "firebase": ">= 9.0.0", + "react": ">= 16.8.0" + } + }, "node_modules/react-is": { "version": "17.0.2", "resolved": "https://registry.npmjs.org/react-is/-/react-is-17.0.2.tgz", diff --git a/package.json b/package.json index e309a20..962a519 100644 --- a/package.json +++ b/package.json @@ -11,6 +11,7 @@ "firestore": "^1.1.6", "react": "^18.2.0", "react-dom": "^18.2.0", + "react-firebase-hooks": "^5.1.1", "react-router-dom": "^6.22.3", "react-scripts": "5.0.1", "web-vitals": "^2.1.4" diff --git a/src/App.js b/src/App.js index 5135dab..4d843cd 100644 --- a/src/App.js +++ b/src/App.js @@ -7,8 +7,10 @@ import Login from 'components/Login'; import Signup from 'components/Signup'; import Profile from 'components/Profile'; import HomePage from 'components/HomePage'; -import Header from 'components/Header'; +import Header from 'components/Header/Header'; import UpdateProfile from 'components/UpdateProfile/UpdateProfile'; +import Write from 'components/Write/Write'; +import EditArticle from 'components/EditArticle/EditArticle'; function App() { return ( @@ -21,6 +23,8 @@ function App() { } /> } /> } /> + } /> + } /> diff --git a/src/components/EditArticle/EditArticle.js b/src/components/EditArticle/EditArticle.js new file mode 100644 index 0000000..2d83235 --- /dev/null +++ b/src/components/EditArticle/EditArticle.js @@ -0,0 +1,70 @@ +// src/components/EditArticle.js +import React, { useState, useEffect } from 'react'; +import { useParams, useNavigate } from 'react-router-dom'; +import { db } from 'firebase-config'; +import { doc, getDoc, updateDoc, serverTimestamp } from 'firebase/firestore'; + +function EditArticle() { + const [title, setTitle] = useState(''); + const [body, setBody] = useState(''); + const { articleId } = useParams(); // Capture the articleId from the URL + const navigate = useNavigate(); + + useEffect(() => { + const fetchArticle = async () => { + const articleRef = doc(db, 'articles', articleId); + const docSnap = await getDoc(articleRef); + if (docSnap.exists()) { + setTitle(docSnap.data().title); + setBody(docSnap.data().body); + } else { + console.log("No such document!"); + navigate('/profile'); // Redirect if the article doesn't exist + } + }; + fetchArticle(); + }, [articleId, navigate]); + + const handleSubmit = async (e) => { + e.preventDefault(); + const articleRef = doc(db, 'articles', articleId); + await updateDoc(articleRef, { + title, + body, + updatedAt: serverTimestamp(), + }); + navigate('/profile'); // Navigate to the profile page after successful update + }; + + return ( +
+

Edit Your Article

+
+
+ + setTitle(e.target.value)} + required + /> +
+
+ +