Skip to content

Commit

Permalink
feat: minor cleanups and updates to dashboard
Browse files Browse the repository at this point in the history
  • Loading branch information
thenicekat committed Apr 24, 2024
1 parent 32711e4 commit 35ded64
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 78 deletions.
55 changes: 25 additions & 30 deletions frontend/app/admin/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import { CubeIcon, FlagIcon, UserCircleIcon } from "@heroicons/react/24/solid";
import Link from "next/link";
import { HttpCodes } from "@/types/HttpCodes";
import { set } from "react-hook-form";
import { report } from "process";

axios.defaults.withCredentials = true;

Expand Down Expand Up @@ -77,22 +78,22 @@ const AdminHomepage = () => {
fetchReports();
}, []);


const closeReport = async ( reportId: string ) => {
const closeReport = async (reportId: string) => {
setMessage("Loading");
try {
const response = await axios.post(siteConfig.server_url + "/report/close",
{
reportId: reportId as string
}
);
fetchReports();
setMessage("");
} catch (err: any) {
setError(err.response.data.message || "Error closing report:" + err);
}
try {
const response = await axios.post(siteConfig.server_url + "/report/close",
{
reportId: reportId as string
}
);
fetchReports();
setMessage("");

} catch (err: any) {
setError(err.response.data.message || "Error closing report:" + err);
}
}

const banUser = async (usermail: string) => {
setMessage("Loading...")
try {
Expand All @@ -103,7 +104,7 @@ const AdminHomepage = () => {
}
fetchUsers();
setMessage("");

} catch (error: any) {
setError(error.response.data.message || "Error occured");
setMessage("")
Expand Down Expand Up @@ -196,27 +197,21 @@ const AdminHomepage = () => {
</div>
</div>
<div className="flex gap-2.5 py-2 items-center">
<span className="text-white text-xl font-semibold">{users.length}</span>
<span className="text-white text-xl font-semibold">{reports.length}</span>
</div>

<div className="flex items-center gap-6">
<div>
<div>
<span className="text-xs text-white">{users.filter(user => user.role == "user").length}</span>
</div>
<span className="text-white text-xs">Pending</span>
</div>
<div>
<div>
<span className="text-xs text-white">{users.filter(user => user.role == "admin").length}</span>
<span className="text-xs text-white">{reports.filter(report => report.status == "open").length}</span>
</div>
<span className="text-white text-xs">Completed</span>
<span className="text-white text-xs">Open</span>
</div>
<div>
<div>
<span className="text-xs text-white">{users.filter(user => user.role == "banned").length}</span>
<span className="text-xs text-white">{reports.filter(report => report.status == "closed").length}</span>
</div>
<span className="text-white text-xs">Irrelevant</span>
<span className="text-white text-xs">Closed</span>
</div>
</div>
</CardBody>
Expand Down Expand Up @@ -314,15 +309,15 @@ const AdminHomepage = () => {
<TableCell>{u.postId}</TableCell>
<TableCell>{u.reason}</TableCell>
<TableCell>
<Button onClick={() => closeReport(u.id)} isDisabled={u.status==="closed"} color = "primary">
{u.status === 'closed' ? 'Closed' : 'Open'}
</Button>
<Button onClick={() => closeReport(u.id)} isDisabled={u.status === "closed"} color="primary">
{u.status === 'closed' ? 'Closed' : 'Open'}
</Button>
</TableCell>
</TableRow>
))}
</TableBody>
</Table>
</div>
</div>

</div >
)
Expand Down
71 changes: 23 additions & 48 deletions frontend/app/report/create/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,58 +8,33 @@ import { Button } from "@nextui-org/button";
import { siteConfig } from "@/config/site";
import axios from "axios";

type FormData = {
reason: string
}

export default function CreateReport() {
const { register, formState: { errors }, control, reset } = useForm<String>()
const [postId, setPostId] = useState("");
useEffect(() => {
// Check if window is available before accessing it
if (typeof window !== 'undefined') {
const params = new URLSearchParams(window.location.search);
const postIdFound = params.get('value') as string ;
if(postIdFound) {
setPostId(postIdFound);
}
// Do something with postId
const { register, formState: { errors }, control, reset } = useForm<FormData>()
const [postId, setPostId] = useState("");

useEffect(() => {
// Check if window is available before accessing it
if (typeof window !== 'undefined') {
const params = new URLSearchParams(window.location.search);
const postIdFound = params.get('value') as string;
if (postIdFound) {
setPostId(postIdFound);
}
}
}, []);

// const onSubmit = async (reason: string) => {
// try {
// const res = await axios.post(siteConfig.server_url + "/report/create", {
// reason: reason,
// postId: postId
// }, {
// withCredentials: true,
// headers: {
// 'Content-Type': 'application/json'
// }
// });

// if (res.status == 201) {
// setError(null)
// setMessage("Report created successfully.")
// window.location.href = "/post/details/" + res.data.data.postId
// reset()
// } else if (res.status == 401) {
// window.location.href = "/"
// }
// else {
// setError(res.data.error || "There was an error creating your report.")
// }
// }
// catch (err: any) {
// setError(err.response.data.message || "There was an error creating your report.")
// }
// }

}
}, []);
const [message, setMessage] = useState<string | null>(null)
const [error, setError] = useState<string | null>(null)

const onSubmit = async (reason: string) => {
try {
const res = await axios.post(siteConfig.server_url + "/report/create", {
reason: reason as string,
postId: postId
postId: postId
}, {
withCredentials: true,
headers: {
Expand All @@ -80,7 +55,7 @@ export default function CreateReport() {
}
}
catch (err: any) {
setError( "There was an error creating your report.")
setError("There was an error creating your report.")
}
}

Expand All @@ -96,10 +71,10 @@ export default function CreateReport() {

<Form
className="flex flex-col gap-3 m-3 w-full mx-auto p-4 rounded-lg shadow-md"
onSubmit = {( {data} : any) => {
onSubmit(data.reason)
}
}
onSubmit={({ data }: any) => {
onSubmit(data.reason)
}
}
onError={() => {
setMessage(null)
setError("There was an error creating your report.")
Expand Down

0 comments on commit 35ded64

Please sign in to comment.