|
| 1 | +import { Link } from "react-router-dom"; |
| 2 | +import { getDay } from "../common/date"; |
| 3 | +import { useContext, useState } from "react"; |
| 4 | +import { UserContext } from "../App"; |
| 5 | +import axios from "axios"; |
| 6 | + |
| 7 | +const ProjectStats = ({ stats }) => { |
| 8 | + |
| 9 | + return ( |
| 10 | + <div className="flex gap-2 max-lg:mb-6 max-lg:pb-6 border-gray-100 max-lg:border-b"> |
| 11 | + { |
| 12 | + Object.keys(stats).map((key, i) => { |
| 13 | + return !key.includes("parent") ? ( |
| 14 | + <div |
| 15 | + key={i} |
| 16 | + className={"flex flex-col items-center w-full h-full justify-center p-4 px-6 " + (i !== 0 ? " border-gray-100 border-l" : "")} |
| 17 | + > |
| 18 | + <h1 className="text-xl lg:text-2xl mb-2"> |
| 19 | + {stats[key].toLocaleString()} |
| 20 | + </h1> |
| 21 | + <p className="max-lg:text-gray-500 capitalize"> |
| 22 | + {key.split("_")[1]} |
| 23 | + </p> |
| 24 | + </div> |
| 25 | + ) : "" |
| 26 | + }) |
| 27 | + } |
| 28 | + </div> |
| 29 | + ) |
| 30 | +} |
| 31 | + |
| 32 | +const deleteProject = (project, access_token, target) => { |
| 33 | + |
| 34 | + let { index, project_id, setStateFunc } = project; |
| 35 | + |
| 36 | + target.setAttribute("disabled", true); |
| 37 | + |
| 38 | + axios.post(import.meta.env.VITE_SERVER_DOMAIN + "/api/project/delete", { project_id }, { |
| 39 | + headers: { |
| 40 | + Authorization: `Bearer ${access_token}` |
| 41 | + } |
| 42 | + }) |
| 43 | + .then(({ data }) => { |
| 44 | + |
| 45 | + target.removeAttribute("disabled"); |
| 46 | + |
| 47 | + setStateFunc(preVal => { |
| 48 | + let { deletedDocCount, totalDocs, results } = preVal; |
| 49 | + |
| 50 | + results.splice(index, 1); |
| 51 | + |
| 52 | + if (!deletedDocCount) { |
| 53 | + deletedDocCount = 0; |
| 54 | + } |
| 55 | + |
| 56 | + if (!results.length && totalDocs - 1 > 0) { |
| 57 | + return null; |
| 58 | + } |
| 59 | + |
| 60 | + return { ...preVal, totalDocs: totalDocs - 1, deletedDocCount: deletedDocCount + 1 }; |
| 61 | + }) |
| 62 | + }) |
| 63 | + .catch(err => { |
| 64 | + target.removeAttribute("disabled"); |
| 65 | + console.log(err); |
| 66 | + }) |
| 67 | +} |
| 68 | + |
| 69 | +export const ManagePublishedProjectCard = ({ project }) => { |
| 70 | + |
| 71 | + let { banner, project_id, title, publishedAt, activity } = project; |
| 72 | + |
| 73 | + let { userAuth: { access_token } } = useContext(UserContext); |
| 74 | + |
| 75 | + let [showStat, setShowStat] = useState(false); |
| 76 | + |
| 77 | + return ( |
| 78 | + <> |
| 79 | + <div className="flex gap-10 border-b mb-6 max-md:px-4 border-gray-100 pb-6 items-center"> |
| 80 | + <img src={banner} alt="" className="max-md:hidden lg:hidden xl:block w-28 h-28 flex-none bg-gray-100 object-cover" /> |
| 81 | + |
| 82 | + <div className="flex flex-col justify-between py-2 w-full min-w-[300px]"> |
| 83 | + <div> |
| 84 | + <Link to={`/project/${project_id}`} className="project-title mb-4 hover:underline">{title}</Link> |
| 85 | + |
| 86 | + <p className="line-clamp-1">Published on {getDay(publishedAt)}</p> |
| 87 | + </div> |
| 88 | + |
| 89 | + <div className="flex gap-6 mt-3"> |
| 90 | + <Link to={`/editor/${project_id}`} className="pr-4 py-2 underline">Edit</Link> |
| 91 | + |
| 92 | + <button |
| 93 | + className="lg:hidden pr-4 py-2 underline" |
| 94 | + onClick={() => setShowStat(preVal => !preVal)} |
| 95 | + > |
| 96 | + Stats |
| 97 | + </button> |
| 98 | + |
| 99 | + <button |
| 100 | + className="pr-4 py-2 underline text-red-500" |
| 101 | + onClick={(e) => deleteProject(project, access_token, e.target)} |
| 102 | + > |
| 103 | + Delete |
| 104 | + </button> |
| 105 | + |
| 106 | + </div> |
| 107 | + </div> |
| 108 | + |
| 109 | + <div className="max-lg:hidden"> |
| 110 | + <ProjectStats stats={activity} /> |
| 111 | + </div> |
| 112 | + </div> |
| 113 | + |
| 114 | + { |
| 115 | + showStat ? |
| 116 | + <div className="lg:hidden"> |
| 117 | + <ProjectStats stats={activity} /> |
| 118 | + </div> |
| 119 | + : "" |
| 120 | + } |
| 121 | + </> |
| 122 | + ) |
| 123 | +} |
| 124 | + |
| 125 | +export const ManageDraftProjectPost = ({ project }) => { |
| 126 | + |
| 127 | + let { title, des, project_id, index } = project; |
| 128 | + |
| 129 | + let { userAuth: { access_token } } = useContext(UserContext); |
| 130 | + |
| 131 | + index++; |
| 132 | + |
| 133 | + return ( |
| 134 | + <div className="flex gap-5 lg:gap-10 pb-6 border-b mb-6 border-gray-100"> |
| 135 | + <h1 className="project-index text-center ph-4 md:pl-6 flex-none"> |
| 136 | + {index < 10 ? "0" + index : index} |
| 137 | + </h1> |
| 138 | + |
| 139 | + <div> |
| 140 | + <h1 className="project-title mb-3">{title}</h1> |
| 141 | + <p className="line-clamp-2 font-gelasio">{des.length ? des : "No Description"}</p> |
| 142 | + |
| 143 | + <div className="flex gap-6 mt-3"> |
| 144 | + <Link to={`/editor/${project_id}`} className="pr-4 py-2 underline">Edit</Link> |
| 145 | + |
| 146 | + <button |
| 147 | + className="pr-4 py-2 underline text-red-500" |
| 148 | + onClick={(e) => deleteProject(project, access_token, e.target)} |
| 149 | + > |
| 150 | + Delete |
| 151 | + </button> |
| 152 | + </div> |
| 153 | + </div> |
| 154 | + </div> |
| 155 | + ) |
| 156 | +} |
0 commit comments