@@ -65,22 +21,37 @@ const BlogItem = ({ blog, userId, token, setIsChanged }) => {
-
- {visibility === "public" && (
-
+
+
{
+ sendDeleteRequest(
+ "Are you sure you want to delete this post?",
+ `blogs/${_id}`,
+ "Blog post deleted successfully",
+ "Failed to delete blog post",
+ setIsChanged
+ );
+ }}
+ btnText="Delete"
+ />
+ {myBlog && visibility === "public" && (
+ {
+ sendPatchRequest(
+ `blogs/${_id}`,
+ {
+ visibility: "private",
+ },
+ "Are you sure you want to make this post private no one else you will be able to see the post?",
+ " post visibility converted to private successfully",
+ "Failed to Make blog post private",
+ setIsChanged
+ );
+ }}
+ btnText="Make It Private"
+ />
)}
)}
diff --git a/src/components/common/sendRequests.js b/src/components/common/sendRequests.js
new file mode 100644
index 0000000..6baaba2
--- /dev/null
+++ b/src/components/common/sendRequests.js
@@ -0,0 +1,78 @@
+import { store } from "../../store/store";
+import { toast } from "react-toastify";
+import { customFetch } from "../../utils/customFetch";
+
+export const sendPostRequest = async (
+ endPoint,
+ data,
+ successMessage,
+ failedMessage,
+ setIsChanged,
+ actions
+) => {
+ try {
+ await customFetch.post(endPoint, data, {
+ headers: {
+ Authorization: `Bearer ${store.getState().userReducers.token}`,
+ },
+ });
+
+ setIsChanged(true);
+ actions.resetForm();
+ toast.success(successMessage);
+ } catch (error) {
+ console.log(error);
+ toast.error(failedMessage);
+ }
+};
+
+export const sendPatchRequest = async (
+ endPoint,
+ data,
+ confirmText,
+ successMessage,
+ failedMessage,
+ setIsChanged
+) => {
+ const isConfirmed = window.confirm(confirmText);
+ if (!isConfirmed) return;
+
+ try {
+ await customFetch.patch(endPoint, data, {
+ headers: {
+ Authorization: `Bearer ${store.getState().userReducers.token}`,
+ },
+ });
+
+ setIsChanged(true);
+ toast.success(successMessage);
+ } catch (error) {
+ console.log(error.message);
+ toast.error(failedMessage);
+ }
+};
+
+export const sendDeleteRequest = async (
+ confirmText,
+ endPoint,
+ successMessage,
+ failedMessage,
+ setIsChanged
+) => {
+ const isConfirmed = window.confirm(confirmText);
+ if (!isConfirmed) return;
+
+ try {
+ await customFetch.delete(endPoint, {
+ headers: {
+ Authorization: `Bearer ${store.getState().userReducers.token}`,
+ },
+ });
+
+ setIsChanged(true);
+ toast.success(successMessage);
+ } catch (error) {
+ console.log(error.message);
+ toast.error(failedMessage);
+ }
+};
diff --git a/src/components/userProfile/blogs/ActionButton.jsx b/src/components/userProfile/blogs/ActionButton.jsx
new file mode 100644
index 0000000..bd2939e
--- /dev/null
+++ b/src/components/userProfile/blogs/ActionButton.jsx
@@ -0,0 +1,18 @@
+const ActionButton = ({ btnType, onClick, btnText }) => {
+ const colorClass =
+ btnType === "danger"
+ ? "bg-red-500 hover:bg-red-800"
+ : btnType === "mainBlue"
+ ? "bg-blue-500 hover:bg-blue-800"
+ : "";
+ return (
+
+ );
+};
+
+export default ActionButton;
diff --git a/src/components/userProfile/blogs/BlogCart.jsx b/src/components/userProfile/blogs/BlogCart.jsx
index c1a8dad..c0cc879 100644
--- a/src/components/userProfile/blogs/BlogCart.jsx
+++ b/src/components/userProfile/blogs/BlogCart.jsx
@@ -1,84 +1,11 @@
-import { customFetch } from "../../../utils/customFetch";
-import { toast } from "react-toastify";
-import { useSelector } from "react-redux";
-
+import ActionBUtton from "./ActionButton";
+import { sendDeleteRequest, sendPatchRequest } from "../../common/sendRequests";
const BlogCart = ({ blog, setIsChanged }) => {
- const { token } = useSelector((state) => state.userReducers);
const { _id, title, content, category, createdAt, published, visibility } =
blog;
const formattedDate = new Date(createdAt).toDateString();
const isPublished = published ? "published" : "Not published";
- const handelDeleteBlog = async () => {
- const isConfirm = window.confirm(
- "Are you sure you want to delete this post?"
- );
- if (!isConfirm) return;
-
- try {
- const response = await customFetch.delete(`blogs/${_id}`, {
- headers: {
- Authorization: `Bearer ${token}`,
- },
- });
- if (response.status === 204) {
- toast.success("Blog deleted successfully");
- setIsChanged(true);
- }
- } catch (error) {
- toast.error(error.response.data.message);
- }
- };
- const handelMakeItPublic = async () => {
- try {
- const isConfirmed = window.confirm(
- "Are you sure you want to make this post private no one else you will be able to see the post?"
- );
- if (!isConfirmed) return;
- await customFetch.patch(
- `blogs/${_id}`,
- {
- visibility: "public",
- },
- {
- headers: {
- Authorization: `Bearer ${token}`,
- },
- }
- );
-
- setIsChanged(true);
- toast.success(" post visibility converted to private successfully");
- } catch (error) {
- toast.error("Failed to delete blog post");
- }
- };
-
- const handelMakeItPrivate = async () => {
- try {
- const isConfirmed = window.confirm(
- "Are you sure you want to make this post private no one else you will be able to see the post?"
- );
- if (!isConfirmed) return;
- await customFetch.patch(
- `blogs/${_id}`,
- {
- visibility: "private",
- },
- {
- headers: {
- Authorization: `Bearer ${token}`,
- },
- }
- );
-
- setIsChanged(true);
- toast.success(" post visibility converted to private successfully");
- } catch (error) {
- toast.error("Failed to delete blog post");
- }
- };
-
return (
@@ -116,27 +43,54 @@ const BlogCart = ({ blog, setIsChanged }) => {
{content}
-
+
{
+ sendDeleteRequest(
+ "Are you sure you want to delete this post?",
+ `blogs/${_id}`,
+ "Blog deleted successfully",
+ "Failed to delete blog post",
+ setIsChanged
+ );
+ }}
+ btnText="Delete Blog"
+ />
{visibility === "private" && (
-
+ {
+ sendPatchRequest(
+ `blogs/${_id}`,
+ {
+ visibility: "public",
+ },
+ "Are you sure you want to make this post private no one else you will be able to see the post?",
+ " post visibility converted to Public successfully",
+ "Failed to make blog post public",
+ setIsChanged
+ );
+ }}
+ btnText="Make It Public"
+ />
)}
{visibility === "public" && (
-
+ {
+ sendPatchRequest(
+ `blogs/${_id}`,
+ {
+ visibility: "private",
+ },
+ "Are you sure you want to make this post private no one else you will be able to see the post?",
+ " post visibility converted to private successfully",
+ "Failed to Make blog post private",
+ setIsChanged
+ );
+ }}
+ btnText="Make It Private"
+ />
)}
diff --git a/src/components/userProfile/financialAids/FinancialAidCard.jsx b/src/components/userProfile/financialAids/FinancialAidCard.jsx
index cc74a1e..82de3bc 100644
--- a/src/components/userProfile/financialAids/FinancialAidCard.jsx
+++ b/src/components/userProfile/financialAids/FinancialAidCard.jsx
@@ -1,34 +1,10 @@
-import { customFetch } from "../../../utils/customFetch";
-import { toast } from "react-toastify";
-import { useSelector } from "react-redux";
import { Link } from "react-router-dom";
-
+import { sendDeleteRequest } from "../../common/sendRequests";
const FinancialAidCard = ({ request, setIsChanged }) => {
- const { token } = useSelector((state) => state.userReducers);
const { _id, user, course, createdAt, updatedAt } = request;
const { photo, title, category, duration, price, description } = course;
const formatCreatedDate = new Date(createdAt).toDateString();
- const formatUpdatedDate = new Date(updatedAt).toDateString();
-
- const handelCancelRequest = async () => {
- try {
- const isConfirmed = window.confirm(
- "Are you sure you want to cancel the request?"
- );
- if (!isConfirmed) return;
- await customFetch.delete(`financialAidRequests/${_id}`, {
- headers: {
- Authorization: `Bearer ${token}`,
- },
- });
- setIsChanged(true);
- toast.success("Course financial aid request canceled successfully");
- } catch (error) {
- console.log(error);
- toast.error("Failed to cancel financial aid request ");
- }
- };
return (
@@ -65,7 +41,15 @@ const FinancialAidCard = ({ request, setIsChanged }) => {