Is it possible to direct users to varying links based on their individual status?

import React from "react";
import {
 Card,
 CardContent,
 CardDescription,
 CardHeader,
 CardTitle,
} from "@/components/ui/card";
import Link from "next/link";
import { cn } from "@/lib/utils";
import { FaCircleCheck } from "react-icons/fa6";
import { currentUser } from "@clerk/nextjs/server";
import ProButton from "@/components/elements/ProButton/pro-button";
type Props = {};

export const Pricing = async (props: Props) => {
 const user = await currentUser();

 return (
 <div>
   <div className="flex flex-col space-y-6 items-center md:space-y-12">
     <div className="grid grid-cols-1 md:grid-cols-2 justify-center gap-8 items-center 
       max-w-[50rem] mx-auto">
       <Card
         className={cn(
           "h-[32.5rem] md:h-[37.5rem] flex flex-col relative overflow-hidden pt-4 
        items-start space-y-0 rounded-2xl"
         )}
        >
         <CardHeader>
           <CardTitle className="text-foreground/80 mb-3 md:mb-6 capitalize text-sm 
         md:text-base">
             Starter
           </CardTitle>
           <h2 className="text-4xl md:text-5xl font-semibold pb-4">
             Free
             <span className="text-sm md:text-base lg:text-xl font-medium">
               /month
             </span>
            </h2>
           <div className="flex flex-col space-y-4">
             <CardDescription className="text-black dark:text-white text-sm md:text- 
         base"> 
               Lorem ipsum dolor sit amet consectetur, adipisicing elit.
               Doloribus, delectus excepturi! Dolorem, omnis dolor blanditiis
               velit ipsum nam dolore? Unde.
             </CardDescription>
             <Link href="">
               <ProButton />
             </Link>
            </div>
          </CardHeader>
         <CardContent>
           <ul className="flex flex-col gap-2">
             <li className="flex items-center capitalize text-sm md:text-base">
               <FaCircleCheck className="mr-4 w-4 h-4 shrink-0" />
               Lorem ipsum
             </li>
             <li className="flex items-center capitalize text-sm md:text-base">
               <FaCircleCheck className="mr-4 w-4 h-4 shrink-0" />
               Lorem ipsum
             </li>
             <li className="flex items-center capitalize text-sm md:text-base">
               <FaCircleCheck className="mr-4 w-4 h-4 shrink-0" />
               Lorem ipsum
             </li>
           </ul>
          </CardContent>
        </Card>
      </div>
    </div>
   </div>
  );
 };

//pro-button.tsx
"use client";

import { Button } from "@/components/ui/button";
import { useProModal } from "@/hooks/use-pro-modal";
import { cn } from "@/lib/utils";
import React, { useState } from "react";

type Props = {};

const ProButton = (props: Props) => {
const [recommended, setRecommended] = useState(false);
const proModal = useProModal();

return (
 <div>
   <Button
     variant={recommended ? "default": "outline"}
     className={cn(
       "w-full h-9",
       recommended === true
         ? ""
         : "border-2 border-black dark:border-white hover:border-muted 
            dark:hover:border-muted"
       )}
       onClick={proModal.onOpen}
     >
      Get started with Free
     </Button>
    </div>
  );
 };

 export default ProButton;

//pro-modal.tsx
"use client";
import {
 Dialog,
 DialogContent,
 DialogHeader,
 DialogTitle,
} from "@/components/ui/dialog";
import { useProModal } from "@/hooks/use-pro-modal";

export const ProModal = () => {
 const proModal = useProModal();
 return (
 <Dialog open={proModal.isOpen} onOpenChange={proModal.onClose}>
   <DialogContent>
     <DialogHeader className="space-y-4">
       <DialogTitle className="text-center">Upgrade to Pro+</DialogTitle>
     </DialogHeader>
   </DialogContent>
 </Dialog>
 );
};

I am currently developing a pricing page using Next.js and Clerk authentication. My goal is to redirect users when they click the "Get started with Free" button based on their authentication status - if authenticated, I want to open a pro-modal, and if not, send them to the sign-in page. However, I'm facing challenges in achieving this functionality. Additionally, maintaining async-await with client components in the same file complicates the task further. Any assistance or guidance on resolving this issue would be greatly appreciated. Thank you for your help.

Answer №1

To customize the behavior of your button based on user input, you can pass the user as props to the ProButton component. Within the onClick function of the button, you can conditionally execute different actions.

const ProButton = (props: Props) => {
 const [recommended, setRecommended] = useState(false);
 const proModal = useProModal();

 return (
   <div>
     <Button
       variant={recommended ? "default" : "outline"}
       className={cn(
         "w-full h-9",
         recommended === true
           ? ""
           : "border-2 border-black dark:border-white hover:border-muted 
              dark:hover:border-muted"
       )}
       onClick={() => {
          // Conditionally navigate or open modal depending on user presence
          !!props.user ? navigation.navigate("URI") : proModal.onOpen()
       }}
     >
      Get started with Free
     </Button>
    </div>
  );
 };

If using Next.js for navigation, make sure to include the useRouter hook from next in order to utilize the navigation functionality effectively.

Hopefully this explanation is clear and assists you in customizing your button behavior!

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Guide to utilizing JavaScript for a basic gaming experience

Looking to incorporate multiple divs that will vanish upon being clicked, while also increasing the score by 1 through javascript. Any suggestions on how to accomplish this? ...

Is it possible for me to sum up each row in the table and then deactivate it using a checkbox?

I am facing an issue with my code. The problem arises when I utilize the each() function; it iterates from the first cell to the end of the row, fetching every value along the way. What I actually want is for it to retrieve the value from each row individu ...

Removing vacant rows from a table using Jquery in asp.net mvc

In my partial view, I am using BeginCollectionItem to manage the code. <tr> @using (Html.BeginCollectionItem("QuoteLines")) { <td> @Html.HiddenFor(m => m.QuoteID) @Html.HiddenFor(m => m.QuoteLineID) </td> ...

Creating a Javascript function to turn lights off using CSS manipulation, similar to the feature found

Is there a way to use JavaScript to obscure all elements on a page except for one specific HTML element? This web application is optimized for Chrome, so CSS3 can also be utilized. ...

Implementing an API call in Vue JS on the app.vue component of a single page application

My project is experiencing delays in API requests due to a large amount of data. I have tried adding a cache, but the page still appears white upon creation. I am considering moving the API call to app.vue to speed up the request. Is there a way to do this ...

Learn how to establish a state using an array and effectively utilize the setState() method in React

For my latest project, which is API based, I am working with arrays that contain sub-arrays. How can I define a state with an array and utilize the setState() method to work with the entire array? ...

ASP.net is encountering difficulty locating a specific JavaScript function

Seeking assistance to comprehend the issue, I have devoted countless hours scouring Google for a resolution. Current tools in use: ASP.NET entity framework Within a view, the following code is utilized: <button type="button" id="btnGraf ...

The ng-click method on the checkbox input field in AngularJS is not being triggered

I'm trying to trigger a function in a toggle switch using ng-click, but the customerActiveDeactive function isn't being executed. <a title="Active/ Deactivate" > <input type="checkbox" class="js-switch" ng-init="status=True" ng-model ...

Error in Dimplejs: Line is not visible when series is empty

I have a dimplejs.org line chart set up. I am trying to colorize the Clicks data points from blue to red (blue for fewer clicks and a gradient from blue to red for more clicks). When I set the series as shown below, it works fine but the tooltip only incl ...

Unable to retrieve the field value from the Json Object

I have a JSON object that I need to parse and display in a data table, but I'm having trouble reading the contents of the object. Here is my JavaScript function: finalGrid: function(data){ console.log("Final Grid"); var strJson = JSON.strin ...

Automatically submit a PHP form if the input text box is left blank

I need a way to automatically refresh the data on my page when the textbox is empty. Manually submitting the form works perfectly, but I want it to happen automatically if the textbox is empty. The catch is that I don't want the form to submit while t ...

Developing applications using ReactJS with Typescript can sometimes lead to errors, such as the "onclick does not exist on type x

In the code snippet below, I have a method that renders a delete icon and is used in my main container. Everything functions correctly except for a small cosmetic issue related to the type any that I am struggling to identify. import React from 'reac ...

Tips for executing an asynchronous fetch prior to the first rendering

Currently, I am working with the Wordpress API using Next.js on the front end. My goal is to fetch my navigation/menu data and have it pre-rendered. However, my attempts have only resulted in an empty <nav> </nav> element being rendered when I ...

Imitating the Frameset Separator's Actions

The latest HTML5 specification has eliminated the <frameset> element. One useful feature of the <frameset> tag that is hard to replicate without it is: In a frameset, you can adjust the position of the frame divider line with the mouse. Is t ...

Create a visual representation of an image by sketching a detailed line profile using HTML5's

I am attempting to create an intensity profile for an image, using the x-axis as the line's length on the image and the y-axis as the intensity values along the length of the line. How can I achieve this on an HTML5 canvas? I have tried the code below ...

Guide to monitoring updates to a universal server-side variable in Angular 2

I am currently developing an application using Angular 2 with Electron and Node. The tests are executed on the server, and the results are stored in a global variable array named testResults. I am able to access this array in Angular by using: declare var ...

Struggling to align the push menu properly within the Bootstrap framework

I am currently utilizing Bootstrap as my UI framework and attempting to create a push menu on the left side of the page. While I have made progress towards achieving this goal, there are some bugs in the system that I am encountering. Specifically, I am ha ...

Verify the channel where the bot is currently active and dispatch a message

I've been attempting to set up my bot to send a message when it joins a guild, but for some reason, it's not functioning as expected. Here is what I have tried (along with some other variations): const { PermissionsBitField } = require('dis ...

Tips for organizing information within a table

I have been developing a tool that allows users to enter a username, and then I display the data fetched from GitHub related to that particular user. Although each cell in the table is sortable, they all seem to sort data based on the first cell. When I cl ...

Creating a custom Jquery function to generate a Div element instead of a Textbox in your web application

I need assistance with a jquery function that retrieves data from a JSON PHP MySQL setup. The retrieved results are currently displayed in textboxes. function showData(wine) { $('#Id').val(wine.id); $('#question').val(wine.question ...