From 52106e4bc91cc03448efb835039fd86b3219ff2c Mon Sep 17 00:00:00 2001 From: Aamir Azad Date: Mon, 12 Aug 2024 09:21:18 -0400 Subject: [PATCH 1/2] feat: simplify setings page --- src/app/settings/page.tsx | 197 +++++++++++++++---------------------- src/components/ui/card.tsx | 79 +++++++++++++++ 2 files changed, 161 insertions(+), 115 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..13b6eca 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, + CardFooter, + CardHeader, + CardTitle, +} from "@/components/ui/card"; const queryClient = new QueryClient(); @@ -37,99 +45,34 @@ 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"); + // 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 +82,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. + +
+
+ )} + /> + + + +
+
); } @@ -263,10 +235,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,23 +246,18 @@ export function Forms() { }, }); - if (!userData || isLoading || !isLoaded) { + if (!userData || userDataIsLoading || !clerkUserIsLoaded) { return Loading...; } else if (!clerkUser) { return redirect("/sign-in?redirect=" + pathname); } const formElements = [ - , - , +>(({ 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 } From d5cef2d0fa8c43439176cd42565f45a80cf11583 Mon Sep 17 00:00:00 2001 From: Aamir Azad Date: Tue, 20 Aug 2024 21:53:32 -0400 Subject: [PATCH 2/2] Re query audiofill on change --- src/app/settings/page.tsx | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/src/app/settings/page.tsx b/src/app/settings/page.tsx index 13b6eca..538ad7b 100644 --- a/src/app/settings/page.tsx +++ b/src/app/settings/page.tsx @@ -33,10 +33,10 @@ import { Card, CardContent, CardDescription, - CardFooter, CardHeader, CardTitle, } from "@/components/ui/card"; +import BodyMessage from "@/components/body-message"; const queryClient = new QueryClient(); @@ -68,6 +68,7 @@ function PaperlessCard({ setActiveTab, userData }: FormProps) { await setUserProperty("paperlessToken", values.PaperlessToken); // Operation succeeded, show success toast 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 @@ -171,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,13 +265,14 @@ export function Forms() { setActiveTab={setActiveTab} userData={userData} />, + All done, ]; return ( <> {formElements[activeTab]}