From 21d187cdbcc150ed444852847d1e7801c7aa2f26 Mon Sep 17 00:00:00 2001 From: Aamir Azad <82281117+aamirazad@users.noreply.github.com> Date: Tue, 20 Aug 2024 22:05:16 -0400 Subject: [PATCH] feat: simplify setings page (#126) ### TL;DR The settings page was a bit confusing having two paperless settings on different pages so I moved them to the same page. Also, changing info updates the auto fill now --- src/app/settings/page.tsx | 202 ++++++++++++++++--------------------- src/components/ui/card.tsx | 79 +++++++++++++++ 2 files changed, 165 insertions(+), 116 deletions(-) create mode 100644 src/components/ui/card.tsx diff --git a/src/app/settings/page.tsx b/src/app/settings/page.tsx index 756bf29..538ad7b 100644 --- a/src/app/settings/page.tsx +++ b/src/app/settings/page.tsx @@ -29,6 +29,14 @@ import { } from "@tanstack/react-query"; import { EyeIcon, EyeOffIcon } from "lucide-react"; import type { UsersTableType } from "@/server/db/schema"; +import { + Card, + CardContent, + CardDescription, + CardHeader, + CardTitle, +} from "@/components/ui/card"; +import BodyMessage from "@/components/body-message"; const queryClient = new QueryClient(); @@ -37,99 +45,35 @@ interface FormProps { userData: UsersTableType; } -function PaperlessURL({ setActiveTab, userData }: FormProps) { - const [isAutofilled, setIsAutofilled] = useState(false); - const formSchema = z.object({ - URL: z.string(), - }); - - const form = useForm>({ - resolver: zodResolver(formSchema), - defaultValues: { - URL: "", - }, - }); - - if (userData.paperlessURL && !isAutofilled) { - form.setValue("URL", userData.paperlessURL); - setIsAutofilled(true); - } - - async function onSubmit(values: z.infer) { - if (values.URL == "") { - setActiveTab((prevTab) => prevTab + 2); // Skip api key form - } else { - setActiveTab((prevTab) => prevTab + 1); // Increment activeTab - } - try { - await setUserProperty("paperlessURL", values.URL); - // Operation succeeded, show success toast - toast("Your paperless URL preferences was saved"); - // Optionally, move to a new tab or take another action to indicate success - } catch { - // Operation failed, show error toast - toast("Uh oh! Something went wrong.", { - description: "Your Paperless URL preferences were not saved.", - action: { - label: "Go back", - onClick: () => setActiveTab((prevTab) => prevTab - 1), // Go back to try again - }, - }); - } - } - - return ( -
- - ( - - Paperless URL - - - - Leave empty to disable - - - )} - /> - - - - ); -} - -function PaperlessToken({ setActiveTab, userData }: FormProps) { - const [isAutofilled, setIsAutofilled] = useState(false); +function PaperlessCard({ setActiveTab, userData }: FormProps) { const [isHidden, setIsHidden] = useState(false); + const formSchema = z.object({ - token: z.string(), + PaperlessURL: z.string(), + PaperlessToken: z.string(), }); + const form = useForm>({ resolver: zodResolver(formSchema), defaultValues: { - token: "", + PaperlessURL: userData.paperlessURL ?? "", + PaperlessToken: userData.paperlessToken ?? "", }, }); - if (userData.paperlessToken && !isAutofilled) { - form.setValue("token", userData.paperlessToken); - setIsAutofilled(true); - } - async function onSubmit(values: z.infer) { setActiveTab((prevTab) => prevTab + 1); // Increment activeTab - try { - await setUserProperty("paperlessToken", values.token); + await setUserProperty("paperlessURL", values.PaperlessURL); + await setUserProperty("paperlessToken", values.PaperlessToken); // Operation succeeded, show success toast - toast("Your paperless token preferences was saved"); + toast("Your paperless preferences was saved"); + void queryClient.invalidateQueries({ queryKey: ["userData"] }); + // Optionally, move to a new tab or take another action to indicate success } catch { // Operation failed, show error toast toast("Uh oh! Something went wrong.", { - description: "Your Paperless token preferences were not saved.", + description: "Your Paperless preferences were not saved.", action: { label: "Go back", onClick: () => setActiveTab((prevTab) => prevTab - 1), // Go back to try again @@ -139,35 +83,64 @@ function PaperlessToken({ setActiveTab, userData }: FormProps) { } return ( -
- - ( - - Paperless API Token - -
- - {isHidden ? ( - setIsHidden(false)} /> - ) : ( - setIsHidden(true)} /> - )} -
-
- - You can create (or re-create) an API token by opening the - "My Profile" link in the user dropdown found in the - web UI and clicking the circular arrow button. - -
- )} - /> - - - + + + Paperless settings + Paperless ngx is a pdf organizer + + +
+ + ( + + Paperless URL + + + + + + )} + /> + ( + + + Paperless API Token + +
+ + {isHidden ? ( + setIsHidden(false)} /> + ) : ( + setIsHidden(true)} /> + )} +
+
+ + You can create (or re-create) an API token by opening the + "My Profile" link in the user dropdown found in + the web UI and clicking the circular arrow button. + +
+
+ )} + /> + + + +
+
); } @@ -199,6 +172,7 @@ function WhishperURL({ setActiveTab, userData }: FormProps) { await setUserProperty("whishperURL", values.URL); // Operation succeeded, show success toast toast("Your whishper URL preferences was saved"); + void queryClient.invalidateQueries({ queryKey: ["userData"] }); // Optionally, move to a new tab or take another action to indicate success } catch { // Operation failed, show error toast @@ -263,10 +237,10 @@ const ProgressIndicator: React.FC = ({ export function Forms() { const [activeTab, setActiveTab] = useState(0); - const { user: clerkUser, isLoaded } = useUser(); + const { user: clerkUser, isLoaded: clerkUserIsLoaded } = useUser(); const pathname = usePathname(); - const { data: userData, isLoading } = useQuery({ + const { data: userData, isLoading: userDataIsLoading } = useQuery({ queryKey: ["userData"], queryFn: async () => { const data = await getUserData(); @@ -274,35 +248,31 @@ export function Forms() { }, }); - if (!userData || isLoading || !isLoaded) { + if (!userData || userDataIsLoading || !clerkUserIsLoaded) { return Loading...; } else if (!clerkUser) { return redirect("/sign-in?redirect=" + pathname); } const formElements = [ - , - , , + All done, ]; return ( <> {formElements[activeTab]} diff --git a/src/components/ui/card.tsx b/src/components/ui/card.tsx new file mode 100644 index 0000000..a22421d --- /dev/null +++ b/src/components/ui/card.tsx @@ -0,0 +1,79 @@ +import * as React from "react" + +import { cn } from "@/lib/utils" + +const Card = React.forwardRef< + HTMLDivElement, + React.HTMLAttributes +>(({ className, ...props }, ref) => ( +
+)) +Card.displayName = "Card" + +const CardHeader = React.forwardRef< + HTMLDivElement, + React.HTMLAttributes +>(({ className, ...props }, ref) => ( +
+)) +CardHeader.displayName = "CardHeader" + +const CardTitle = React.forwardRef< + HTMLParagraphElement, + React.HTMLAttributes +>(({ className, ...props }, ref) => ( +

+)) +CardTitle.displayName = "CardTitle" + +const CardDescription = React.forwardRef< + HTMLParagraphElement, + React.HTMLAttributes +>(({ className, ...props }, ref) => ( +

+)) +CardDescription.displayName = "CardDescription" + +const CardContent = React.forwardRef< + HTMLDivElement, + React.HTMLAttributes +>(({ className, ...props }, ref) => ( +

+)) +CardContent.displayName = "CardContent" + +const CardFooter = React.forwardRef< + HTMLDivElement, + React.HTMLAttributes +>(({ className, ...props }, ref) => ( +
+)) +CardFooter.displayName = "CardFooter" + +export { Card, CardHeader, CardFooter, CardTitle, CardDescription, CardContent }