diff --git a/src/Config.js b/src/Config.js new file mode 100644 index 0000000..cd74e7c --- /dev/null +++ b/src/Config.js @@ -0,0 +1,4 @@ +export const baseUrl = process.env.REACT_APP_API_BASE_URL; +export const apiWeb = process.env.REACT_APP_API_CONTACT; +export const apiEmail = process.env.REACT_APP_EMAIL_KEY; + diff --git a/src/api/posts.js b/src/api/posts.js index d5dc2a9..d6be352 100644 --- a/src/api/posts.js +++ b/src/api/posts.js @@ -1,11 +1,11 @@ import axios from "axios"; -import Cookies from "js-cookie"; +import Cookies from "js-cookie"; +import { baseUrl } from "../Config"; -const PostUrl = process.env.REACT_APP_POST_URL; -const UpdatePost = process.env.REACT_APP_UPDATE_POST; -const DeletePost = process.env.REACT_APP_POST_DELETE; -const PostId = process.env.REACT_APP_POST_ID; +export const getTokenFromCookie = () => { + return Cookies.get("token") || ""; +}; export const getTokenFromCookie = () => { @@ -14,7 +14,7 @@ export const getTokenFromCookie = () => { export const getPostsRequest = async () => - await axios.get(PostUrl, {}); + await axios.get(`${baseUrl}/posts`, {}); export const createPostRequest = async (post) => { const form = new FormData(); @@ -23,36 +23,40 @@ export const createPostRequest = async (post) => { form.append(key, post[key]); } - return await axios.post(PostUrl, form, { + return await axios.post(`${baseUrl}/posts`, form, { headers: { "Content-Type": "multipart/form-data", - Authorization: `Bearer ${getTokenFromCookie()}`, + Authorization: `Bearer ${getTokenFromCookie()}`, + }, }); }; export const deletePostRequest = async (id) => - await axios.delete(DeletePost + id, { + + await axios.delete(`${baseUrl}/posts/${id}`, { headers: { - Authorization: `Bearer ${getTokenFromCookie()}`, + Authorization: `Bearer ${getTokenFromCookie()}`, + }, }); export const getPostRequest = async (id) => - await axios.get(PostId + id); - - export const updatePostRequest = async (id, formData) => { - try { - const response = await axios.put(`${UpdatePost}/${id}`, formData, { - headers: { - Authorization: `Bearer ${getTokenFromCookie()}`, - 'Content-Type': 'multipart/form-data', - }, - }); - - return response.data; - } catch (error) { - - throw error; - } - }; \ No newline at end of file + + await axios.get(`${baseUrl}/posts/` + id); + +export const updatePostRequest = async (id, formData) => { + try { + const response = await axios.put(`${baseUrl}/posts/${id}`, formData, { + headers: { + Authorization: `Bearer ${getTokenFromCookie()}`, + "Content-Type": "multipart/form-data", + }, + }); + + return response.data; + } catch (error) { + throw error; + } +}; + diff --git a/src/components/Comments.jsx b/src/components/Comments.jsx index 476b272..c1d1b54 100644 --- a/src/components/Comments.jsx +++ b/src/components/Comments.jsx @@ -4,18 +4,26 @@ import axios from "axios"; import toast from "react-hot-toast"; import Cookies from "js-cookie"; +import { FontAwesomeIcon } from "@fortawesome/react-fontawesome"; +import { + faTrash, + faPenToSquare, + faXmark, + faCheck, +} from "@fortawesome/free-solid-svg-icons"; +import { baseUrl } from "../Config"; + export const Comment = ({ comment, onDelete, onEdit }) => { const [isEditing, setIsEditing] = useState(false); const [editedText, setEditedText] = useState(comment.text); const handleDeleteComment = async () => { try { - await axios.delete( - `http://localhost:4000/api/posts/${comment._id}/comments/`, - { - withCredentials: true, - } - ); + + await axios.delete(`${baseUrl}/posts/${comment._id}/comments/`, { + withCredentials: true, + }); + onDelete(comment._id); @@ -28,7 +36,9 @@ export const Comment = ({ comment, onDelete, onEdit }) => { const handleEditComment = async () => { try { const response = await axios.put( - `http://localhost:4000/api/posts/${comment._id}/comments/`, + + `${baseUrl}/posts/${comment._id}/comments/`, + { text: editedText }, { withCredentials: true, @@ -47,7 +57,9 @@ export const Comment = ({ comment, onDelete, onEdit }) => { }; const canEditAndDelete = - comment?.commentator?.trim() === Cookies.get("username")?.trim() + + comment?.commentator?.trim() === Cookies.get("username")?.trim(); + return (
@@ -64,34 +76,42 @@ export const Comment = ({ comment, onDelete, onEdit }) => { <> ) : ( <> )} @@ -104,7 +124,13 @@ export const Comment = ({ comment, onDelete, onEdit }) => {