2025-01-24 17:24:45 -05:00
|
|
|
'use client';
|
|
|
|
|
|
|
|
import { useState, useEffect } from 'react';
|
|
|
|
import { Button } from '@/components/ui/button';
|
|
|
|
import { Card, CardHeader, CardTitle, CardContent } from "@/components/ui/card"
|
|
|
|
import strings from "@/strings.json"
|
|
|
|
import { PlusCircle, CircleAlert } from "lucide-react"
|
|
|
|
|
|
|
|
export default function Posts() {
|
|
|
|
const [totalPosts, setTotalPosts] = useState(0);
|
|
|
|
const [postCardError, setPostCardError] = useState(false);
|
|
|
|
const [postCardLoading, setPostCardLoading] = useState(true);
|
|
|
|
|
|
|
|
useEffect(() => {
|
2025-01-30 19:59:24 -05:00
|
|
|
console.log(strings.logsCalculatingPostCt);
|
2025-01-24 17:24:45 -05:00
|
|
|
(async () => {
|
|
|
|
try {
|
|
|
|
const username = document.cookie.split('; ').find(row => row.startsWith('username='))?.split('=')[1] || '';
|
|
|
|
const key = document.cookie.split('; ').find(row => row.startsWith('key='))?.split('=')[1] || '';
|
|
|
|
|
|
|
|
const res = await fetch(`http://localhost:3001/api/admin/posts/totalPosts`, {
|
|
|
|
method: 'POST',
|
|
|
|
headers: {
|
|
|
|
'Content-Type': 'application/json',
|
|
|
|
},
|
|
|
|
body: JSON.stringify({
|
|
|
|
username,
|
|
|
|
key
|
|
|
|
}),
|
|
|
|
cache: 'no-store',
|
|
|
|
});
|
|
|
|
|
|
|
|
if (!res.ok) {
|
2025-01-30 19:59:24 -05:00
|
|
|
alert(strings.errorsFetchTotalPostCtErr);
|
2025-01-24 17:24:45 -05:00
|
|
|
setPostCardError(true);
|
2025-01-30 19:59:24 -05:00
|
|
|
throw new Error(`${strings.errorsFetchTotalPostCtErr}: ${res.status}`);
|
2025-01-24 17:24:45 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
const data = await res.json();
|
|
|
|
if (data.success === false) {
|
|
|
|
if (data.message) {
|
|
|
|
alert(data.message);
|
|
|
|
setPostCardError(true);
|
|
|
|
setPostCardLoading(false);
|
|
|
|
throw new Error(data.message);
|
|
|
|
} else {
|
2025-01-30 19:59:24 -05:00
|
|
|
alert(strings.errorsUnknownError);
|
2025-01-24 17:24:45 -05:00
|
|
|
setPostCardError(true);
|
|
|
|
setPostCardLoading(false);
|
2025-01-30 19:59:24 -05:00
|
|
|
throw new Error(strings.errorsUnknownError);
|
2025-01-24 17:24:45 -05:00
|
|
|
}
|
|
|
|
} else if (data.count) {
|
2025-01-30 19:59:24 -05:00
|
|
|
console.log(strings.logsTotalPosts, data.count);
|
2025-01-24 17:24:45 -05:00
|
|
|
setTotalPosts(data.count);
|
|
|
|
setPostCardLoading(false);
|
|
|
|
}
|
|
|
|
} catch (error) {
|
2025-01-30 19:59:24 -05:00
|
|
|
alert(strings.errorsFetchTotalPostCtErr);
|
2025-01-24 17:24:45 -05:00
|
|
|
setPostCardError(true);
|
|
|
|
setPostCardLoading(false);
|
2025-01-30 19:59:24 -05:00
|
|
|
console.error(strings.errorsFetchTotalPostCtErrFancy, error);
|
2025-01-24 17:24:45 -05:00
|
|
|
}
|
|
|
|
})();
|
|
|
|
}, []);
|
|
|
|
|
|
|
|
return (
|
|
|
|
<div className="space-y-8">
|
2025-01-30 19:59:24 -05:00
|
|
|
<h1 className="text-4xl font-bold text-primary">{strings.postsHeader}</h1>
|
2025-01-24 17:24:45 -05:00
|
|
|
<div className="grid gap-6 sm:grid-cols-3 lg:grid-cols-4">
|
|
|
|
<Card className="flex flex-col justify-between">
|
|
|
|
<CardHeader>
|
2025-01-30 19:59:24 -05:00
|
|
|
<CardTitle>{strings.adminQuickActionsCardTitle}</CardTitle>
|
2025-01-24 17:24:45 -05:00
|
|
|
</CardHeader>
|
|
|
|
<CardContent className="grid gap-4">
|
|
|
|
<Button className="w-full justify-start" variant="outline" asChild>
|
|
|
|
<a href="/admin/post">
|
|
|
|
<PlusCircle className="mr-2 h-4 w-4" />
|
2025-01-30 19:59:24 -05:00
|
|
|
{strings.createPostButtonText}
|
2025-01-24 17:24:45 -05:00
|
|
|
</a>
|
|
|
|
</Button>
|
|
|
|
</CardContent>
|
|
|
|
</Card>
|
|
|
|
<Card className="flex flex-col justify-between">
|
|
|
|
<CardHeader>
|
|
|
|
<div className="flex justify-between items-start">
|
|
|
|
<CardTitle className="text-xl text-primary">{strings.totalPostsCardTitle}</CardTitle>
|
|
|
|
<span className="text-4xl font-bold text-primary ml-2">
|
|
|
|
{postCardLoading ? (
|
|
|
|
<div className="animate-spin rounded-full h-16 w-16 border-4 border-t-slate-800 border-white"></div>
|
|
|
|
) : postCardError ? (
|
|
|
|
<div className="flex items-center text-red-500">
|
|
|
|
<CircleAlert />
|
2025-01-30 19:59:24 -05:00
|
|
|
<p className="text-base ml-1.5">{strings.errorsSuperGeneric}</p>
|
2025-01-24 17:24:45 -05:00
|
|
|
</div>
|
|
|
|
) : (
|
|
|
|
totalPosts
|
|
|
|
)}
|
|
|
|
</span>
|
|
|
|
</div>
|
|
|
|
</CardHeader>
|
|
|
|
</Card>
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|