Skip to content

Commit

Permalink
feat: skeleton loaders instead of spinner
Browse files Browse the repository at this point in the history
  • Loading branch information
aamirazad committed Jun 29, 2024
1 parent b152608 commit 7d1d1fa
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 11 deletions.
6 changes: 5 additions & 1 deletion src/app/paperless/document/[id]/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,9 @@ export default function ModalDocumentPage({
}: {
params: { id: number };
}) {
return <DocumentViewer id={params.id} />;
return (
<main className="h-full w-full">
<DocumentViewer id={params.id} />
</main>
);
}
35 changes: 25 additions & 10 deletions src/components/document-viewer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,29 @@ export default function DocumentViewer(props: { id: number }) {
const [pdfUrl, setPdfUrl] = useState<string | null>(null);
const [loading, setLoading] = useState(true);

const SkeletonLoader = () => (
<div className="flex h-4/5 justify-center w-full">
<div className="flex h-full md:w-1/2 min-w-0 justify-center">
<div className="flex h-full w-full flex-col rounded-xl bg-slate-600/50">
<div className="m-4 flex flex-grow animate-pulse flex-col justify-center gap-8 md:m-8 md:flex-row md:gap-16">
{/* PDF Skeleton */}
<div className="h-full flex-shrink flex-grow rounded-lg bg-gray-300"></div>
{/* Button Skeleton */}
<div className="flex flex-shrink-0 flex-col gap-8">
<div className="h-10 w-24 rounded-md bg-gray-300"></div>
<div className="h-10 w-24 rounded-md bg-gray-300"></div>
<div className="h-10 w-24 rounded-md bg-gray-300"></div>
<div className="h-10 w-24 rounded-md bg-gray-300"></div>
<div className="h-10 w-24 rounded-md bg-gray-300"></div>
<div className="h-10 w-24 rounded-md bg-gray-300"></div>
<div className="h-10 w-24 rounded-md bg-gray-300"></div>
</div>
</div>
</div>
</div>
</div>
);

useEffect(() => {
const fetchData = async () => {
setLoading(true);
Expand All @@ -65,23 +88,15 @@ export default function DocumentViewer(props: { id: number }) {
}, [props.id]); // Dependency array to refetch if id changes

if (loading) {
return (
<div className="flex justify-center">
<div className="mx-auto max-w-sm rounded-lg bg-black p-4 shadow-md">
<LoadingSpinner className="w-min rounded-full text-xl font-bold">
Loading...
</LoadingSpinner>
</div>
</div>
);
return <SkeletonLoader />;
}

if (!pdfUrl) {
return <h1>Failed to get document</h1>;
}

return (
<div className="flex h-full w-full min-w-0 justify-center">
<div className="flex min-w-0 justify-center">
<div className="flex h-4/5 flex-col rounded-xl bg-slate-600/50 md:w-1/2">
<div className="m-4 flex flex-grow flex-col justify-center gap-8 md:m-8 md:flex-row md:gap-16">
<div className="h-full flex-shrink flex-grow">
Expand Down

0 comments on commit 7d1d1fa

Please sign in to comment.