Skip to content

Commit

Permalink
Userdata passing works, simplifyed clerk call as well
Browse files Browse the repository at this point in the history
  • Loading branch information
aamirazad committed Jun 25, 2024
1 parent 0d4cbfc commit 23e02d6
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 37 deletions.
Binary file modified bun.lockb
Binary file not shown.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
"dependencies": {
"@clerk/nextjs": "^5.1.5",
"@clerk/themes": "^2.1.9",
"@clerk/types": "^4.6.1",
"@hookform/resolvers": "^3.6.0",
"@radix-ui/react-alert-dialog": "^1.0.5",
"@radix-ui/react-dropdown-menu": "^2.0.6",
Expand Down
64 changes: 27 additions & 37 deletions src/app/settings/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -30,15 +30,14 @@ import {
import { EyeIcon, EyeOffIcon } from "lucide-react";
import type { UsersTableType } from "@/server/db/schema";

function PaperlessURL({
setActiveTab,
userData,
}: {
const queryClient = new QueryClient();

interface FormProps {
setActiveTab: Dispatch<SetStateAction<number>>;
userData: UsersTableType;
}) {
const { user, isLoaded } = useUser();
const pathname = usePathname();
}

function PaperlessURL({ setActiveTab, userData }: FormProps) {
const [isAutofilled, setIsAutofilled] = useState(false);
const formSchema = z.object({
URL: z.string(),
Expand All @@ -51,17 +50,11 @@ function PaperlessURL({
},
});

if (!isLoaded) {
return <LoadingSpinner>Loading...</LoadingSpinner>;
} else if (userData.paperlessURL && !isAutofilled) {
if (userData.paperlessURL && !isAutofilled) {
form.setValue("URL", userData.paperlessURL);
setIsAutofilled(true);
}

if (!user) {
return redirect("/sign-in?redirect=" + pathname);
}

async function onSubmit(values: z.infer<typeof formSchema>) {
if (values.URL == "") {
setActiveTab((prevTab) => prevTab + 2); // Skip api key form
Expand Down Expand Up @@ -108,15 +101,7 @@ function PaperlessURL({
);
}

function PaperlessToken({
setActiveTab,
userData,
}: {
setActiveTab: Dispatch<SetStateAction<number>>;
userData: UsersTableType;
}) {
const { user, isLoaded } = useUser();
const pathname = usePathname();
function PaperlessToken({ setActiveTab, userData }: FormProps) {
const [isAutofilled, setIsAutofilled] = useState(false);
const [isHidden, setIsHidden] = useState(false);
const formSchema = z.object({
Expand All @@ -129,17 +114,11 @@ function PaperlessToken({
},
});

if (!isLoaded) {
return <LoadingSpinner>Loading...</LoadingSpinner>;
} else if (userData.paperlessToken && !isAutofilled) {
if (userData.paperlessToken && !isAutofilled) {
form.setValue("token", userData.paperlessToken);
setIsAutofilled(true);
}

if (!user) {
return redirect("/sign-in?redirect=" + pathname);
}

async function onSubmit(values: z.infer<typeof formSchema>) {
setActiveTab((prevTab) => prevTab + 1); // Increment activeTab

Expand Down Expand Up @@ -218,10 +197,11 @@ const ProgressIndicator: React.FC<ProgressIndicatorProps> = ({
);
};

const queryClient = new QueryClient();

export default function SettingsPage() {
export function Forms() {
const [activeTab, setActiveTab] = useState(0);
const { user: clerkUser, isLoaded } = useUser();
const pathname = usePathname();

const { data: userData, isLoading } = useQuery({
queryKey: ["userData"],
queryFn: async () => {
Expand All @@ -230,8 +210,10 @@ export default function SettingsPage() {
},
});

if (!userData || isLoading) {
if (!userData || isLoading || !isLoaded) {
return <LoadingSpinner>Loading...</LoadingSpinner>;
} else if (!clerkUser) {
return redirect("/sign-in?redirect=" + pathname);
}

const formElements = [
Expand All @@ -248,14 +230,22 @@ export default function SettingsPage() {
];
return (
<>
<QueryClientProvider client={queryClient}>
{formElements[activeTab]}
</QueryClientProvider>
{formElements[activeTab]}
<ProgressIndicator
activeTab={activeTab}
totalTabs={formElements.length}
setActiveTab={setActiveTab}
/>
</>
);
}

export default function SettingsPage() {
return (
<>
<QueryClientProvider client={queryClient}>
<Forms />
</QueryClientProvider>
<Toaster />
</>
);
Expand Down

0 comments on commit 23e02d6

Please sign in to comment.