Skip to content

Commit

Permalink
It finally works
Browse files Browse the repository at this point in the history
  • Loading branch information
aamirazad committed Jun 22, 2024
1 parent dc483d4 commit 6d447da
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 55 deletions.
16 changes: 8 additions & 8 deletions src/app/actions.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
"use server";

import { db } from "@/server/db";
import { UsersTableType, users } from "@/server/db/schema";
import type { UsersTableType } from "@/server/db/schema";
import { users } from "@/server/db/schema";
import type { PaperlessDocumentsType } from "@/types";
import { auth } from "@clerk/nextjs/server";
interface User {
Expand All @@ -18,7 +19,7 @@ export async function setUserProperty<K extends keyof User>(
const { userId } = auth();

if (!userId) {
throw new Error("Not authenticated");
return null;
}

try {
Expand All @@ -30,15 +31,15 @@ export async function setUserProperty<K extends keyof User>(
set: { [propertyName]: value },
});
} catch {
throw new Error("Database error");
return null;
}
}

export async function getUserData() {
const { userId } = auth();

if (!userId) {
throw new Error("Not authenticated");
return null;
}

const userData = await db.query.users.findFirst({
Expand All @@ -51,12 +52,11 @@ export async function getUserData() {
export async function getPaperlessDocuments(query: string) {
const { userId } = auth();

if (!userId) return Response.json({ error: "Unauthorized" }, { status: 401 });
if (!userId) return null;

const userData = await getUserData();

if (!query || query == "null" || query.length < 3 || !userData)
return Response.json({ error: "Bad Request" }, { status: 400 });
if (!query || query == "null" || query.length < 3 || !userData) return null;

const response = await fetch(
`${userData.paperlessURL}/api/search/?query=${query}`,
Expand All @@ -71,5 +71,5 @@ export async function getPaperlessDocuments(query: string) {

const data = (await response.json()) as PaperlessDocumentsType;

return Response.json({ data });
return data;
}
18 changes: 7 additions & 11 deletions src/app/paperless/page.tsx
Original file line number Diff line number Diff line change
@@ -1,11 +1,7 @@
"use client";

import { SignedIn, SignedOut } from "@clerk/nextjs";
import {
usePathname,
useRouter,
useSearchParams,
} from "next/navigation";
import { usePathname, useRouter, useSearchParams } from "next/navigation";
import { useForm } from "react-hook-form";
import { z } from "zod";
import { Button } from "@/components/ui/button";
Expand All @@ -26,7 +22,6 @@ import {
QueryClient,
} from "@tanstack/react-query";
import LoadingSpinner from "@/components/loading-spinner";
import type { PaperlessDocumentsType } from "@/types";
import { getPaperlessDocuments, getUserData } from "../actions";
import Link from "next/link";

Expand Down Expand Up @@ -97,8 +92,7 @@ function DocumentsPage() {
return Promise.resolve(null);
}
const response = await getPaperlessDocuments(query);
const data = (await response.json()) as PaperlessDocumentsType;
return data;
return response;
},
// This ensures the query does not run if there's no query string
enabled: !!query,
Expand Down Expand Up @@ -126,16 +120,18 @@ function DocumentsPage() {
</h1>
);
} else if (!PaperlessDocuments.data || PaperlessDocuments.error) {
console.log(PaperlessDocuments.data);
return (
<h1 className="text-2xl font-bold">
Connection failed! Check that the paperless url is set correctly in{" "}
<Link href="/settings">settings</Link>
Connection failed! Check that the paperless url/token is set correctly
in <Link href="/settings">settings</Link>
</h1>
);
}

const paperlessURL = userData.data?.paperlessURL;
const paperlessDocumentMap = PaperlessDocuments.data.data.documents;

const paperlessDocumentMap = PaperlessDocuments.data.documents;

return (
<div>
Expand Down
70 changes: 34 additions & 36 deletions src/types/index.ts
Original file line number Diff line number Diff line change
@@ -1,37 +1,35 @@
export type PaperlessDocumentsType = {
data: {
total: number;
documents: {
added: string;
archive_serial_number: string;
archived_file_name: string;
content: string;
correspondent: string;
created: string;
created_date: string;
custom_fields: [];
document_type: number;
id: number;
is_shared_by_requester: boolean;
modified: string;
notes: [];
original_file_name: string;
owner: number;
storage_path: number;
tags: [];
title: string;
user_can_change: boolean;
}[];
saved_views: [];
correspondents: [];
document_types: [];
storage_paths: [];
tags: [];
users: [];
groups: [];
mail_accounts: [];
mail_rules: [];
custom_fields: [];
workflows: [];
};
};
total: number;
documents: {
added: string;
archive_serial_number: string;
archived_file_name: string;
content: string;
correspondent: string;
created: string;
created_date: string;
custom_fields: [];
document_type: number;
id: number;
is_shared_by_requester: boolean;
modified: string;
notes: [];
original_file_name: string;
owner: number;
storage_path: number;
tags: [];
title: string;
user_can_change: boolean;
}[];
saved_views: [];
correspondents: [];
document_types: [];
storage_paths: [];
tags: [];
users: [];
groups: [];
mail_accounts: [];
mail_rules: [];
custom_fields: [];
workflows: [];
};

0 comments on commit 6d447da

Please sign in to comment.