Hey there and welcome back to tube guruji channel today in this video we are going to build and complete SAS application we are going to build an AI kid story generator application where you can learn a lot of different technology and use of AI to build this beautiful application let's walk through the application first and let's see what we are going to build in this particular video If you see we have this beautiful Landing page and on this page is a simple and clean landing page where uh you have an option to sign in with
the help of clerk authentication now you have option to sign in with Google Facebook or you can even sign up with the email now once you sign up with the any account you will see the dashboard and the option to manage your account now let's jump to the dashboard and boom if you see you will see all the story which this user is generated not only That but we can maintain the credit uh usage so if user created one story then one credit need to be used okay and you can have you have option to
buy more credit as well but let's create a new story so just click on this create story and you have to add the subject so let's say you want to create gender story on boy with magical school okay and you can select the stories type here if you see how beautifully we design this one as soon as I hover on any of this image the Color the image color is get changed immediately cool right now select the story type here you have option to select the age group and not only that but our application is
going to generate an AI image Yes you heard right now select the type of image you want to generate let's say you want to generate a 3D cartoon image and simply click generate story here you will see this beautiful loading indicator as well and once the story is generated you will see This beautiful toast message which we added and and wow if you see this AI image is also generated not only that but we have the book cover with the title of the book okay and I'm going to tell you how to generate this AI
image okay now if you want to read a book just click on this next button and wow that's cool right it open like a book only okay not only here but if you want to play this particular um story you have option to play this particular chapter now you Can even go to the next page and you will see this beautiful animation to open uh every page cool right you can go back to the previous page or go back to the next chapter and boom perfect right now in order to access your all story you
can just go to your dashboard page and here you will see the credit got used right now over here you will see all your story which you just created now if you want to add more credits you want to buy more more credits you have Option to buy more credits you can select the plan you want which you want and you can make a payment with the help of PayPal now obviously we give all the option you have you can even pay with the debit or credit card as well now not only that but if
you want to explore more stories just go to this explore stories option and here you will find all the stories which other user generated okay and we added this load more option so it's a kind of pagination And uh infinite scoring thing that we added to our application perfect right and you can even read any book which you want see so this complete application obviously we are going to deploy it on the cloud as well so you can access it and you can share this project with your friends and family now let me talk about
the text tag which we are going to use to build this application the first and very Important as this is the nextjs project or react based project and we are going to use nextjs as our react web framework and we are going to use typescript uh to write our code uh the most important with the help of T CSS we are going to use and explore a new telin CSS based Library called Next UI and it is quite beautiful and very easy to use uh UI library for authentication I'm going to use my favorite clerk
authentication service provider and I'm going to tell You the new and updated version of cler how to integrate with any of your application for a database obviously we are going to use a drizzle orm along with the postgate SQL database so that you will learn how to U connect your application with the backend as this is a completely full stack based application now coming to the part of AI we are going to use and gini AI a gini API or a gini AI is free to use and is very uh popular and very fast so
with The help of Jin API we are going to uh integrate to build or to get the text based AI response and the one of the most important is to build or to generate the AI image and for that one we are going to use a replicate replicate is used to run any AI model and you will find and I'm going to tell you a different and bunch of AI model to generate the image not only that but one of the cheapest in the market how to generate this API within a scent okay Less than
Cent I think okay and within a $1 you can generate 300 plus images with this particular model and that one also I'm going to teach you in this particular course so guys if you did not subscribe to my Channel please do subscribe if you did not join the tube GJ Pro membership please join it so that you'll get the source code for free of for all of my projects yes you are right just it's under $9 just join it or you can join a yearly or the lifetime Membership is just $125 okay so guys before
doing any further delay let's begin to develop this AI kid story generator application let me talk about the billb doai where you can create and winning content with the help of AI you can even generate the all the social media post like you can generate Instagram post Twitter post you can generate the YouTube script or you can even generate the advertise for your Business Bill bus is very powerful AI based tool if you want to join the bill bus you can find the link in the description and you can get a 25% off on the
current pricing let me walk through how it works once you log in you will find this dashboard where you will find all the options where you can generate the content with the help of AI let me show you one example let's say you want to create an Instagram post so you can Select the type then you can select the product let's say you want to generate the Instagram post for the fitness here you can write the context so once you fill all the details you can simply click on generate Instagram post and within a few
seconds it will generate the complete and uh SEO base or SEO keyword base uh post for you and if you see on the right hand side you will see how it actually look on the your Instagram post here it will also Generate the AI based image for you so you don't need to bring or get any images this bill bu will take care of generating the AI image as well and he will find the content along with the hashtag for your Instagram post how cool it is right and not only that but if you come
back come back later on this billb you can find your post which you generated previously like this okay see so guys uh without doing any further delay if you want to join the bill bus The link is in the description also you will get a 25% off on this particular product so just join it in order to create a react application we are going to use a NE JS nextjs is the react framework for the web application and it provide a lot of different feature the most important is is to provide a folder based routing
when you want to develop any big application the folder based routing is quite convenient uh for your big project So in order to create this project let's uh just copy this com command npx next app at latest and paste it inside your terminal so open a folder and open a terminal at that folder and simply just paste this command and click enter now at latest will install the latest version of the nextjs application here we'll say uh yes to proceed and then you need to give the project name here we'll say AI story Builder or
here we just rename this AI Kits story Builder and click enter now it will ask you that do you want to use typescript yes or no so we are going to use typescript in this particular project so say yes then us link say no the telin CSS obviously you want a telin CSS say yes and Source directory say no for the app router obviously which is recommended so we are going to use and say yes for that one and for input all say no now if you see It installing some of the dependency like react
react Dom next and some de dependency as well and now if you see our project is created successfully and is ready to use now open a vs code and open the folder and just locate where you just created your project so in my case it is in projects nextjs and AI kid story Builder click open here you have to say yes I trust the author and on the left hand side you will see All the folders files which generated uh with that command okay let's walk through one by one the first folder and very important
is app folder now inside the app folder which we are going to write all our code okay which we are going to add new screens and all of these things inside this app folder we already have this font folder okay then we have this globals uh. CSS so whatever The Styling you want to apply throughout the Application you can Define inside This Global CSS then we have layout. TSX file now inside this layout. TSX file uh which used to render your application okay so if you see uh this is the root layout and every negs
application has a root layout uh file so over here inside this HTML body tax we are going to render the children's and this metat is useful when you want to um add the SEO for your pages and applications then we have page. TSX file which is the default Page for our application so once we run this application you will get to know and we will also edit this particular page then after that we have this node module so whatever the dependency which we install uh that packages which is inside this node module obviously we are
not going to touch to this particular folder then we have this another important file called next. config MGS file uh which help us uh to add the configuration related to the nextjs Application then we have package lock and package.json file which is the heart of our application so which contain the name of the uh application the version the script to run the application or to build the application and the dependency which is installed along with their version okay and to maintain the package version that's the reason we have this package lock. Json so once you
share this project with some other uh friends or someone right and they want to work On that one this package lock is that that time is important then we have post css. config.js file and T.C config.inc SS now if you see inside this content uh whatever the path is Define inside that only the TN CSS is applicable if you want to uh add the TN is in some specific folder and that path is not mentioned here you have to make sure that path is added inside the content okay so now let's run the application so
Simply go to the terminal and click new terminal it will open the terminal inside your vs code only and just type npm run de and click enter now it will open the port 3,000 on the URL localers 3,000 just open this URL on your browser and and obviously uh first time it will take uh time to build and if you see this is the default page uh showing on the screen now let me bring this side by side and let's edit this page so let's go to this app and inside That we have this page.
TSX file so if I want to remove all of this thing just clean this and simply add a d okay and save it as soon as you save it you will see the screen is showing empty or blank page and if I type something let's say h2 tag we say subscribe to TU guruji so guys if you did not subscribe to our Channel please do subscribe and as soon as I save it you will see the text subscrib To TU guruji is showing on the screen okay so it's also support the hot reload as soon
as you type or make some changes in your file and save it it automatically reflect on your browser VI without refreshing manually okay so that's how guys you can create the nextjs react project and run the project at very first time now it's time to add an UI library for our application and for that one we are going to use this next UI next UI Provide a lot of different components and is very easy to use also uh it provide the different type of theming so if you see we have the next UI the mod
modern elegant retro and there are lot of them and it's very easy to switch between them as well so let's go to this next u.org I will keep the link in the description and simply go to the documentation here uh select the installation section and over here there are some installation step which we need To follow in order to install this next UI into our application so first thing we already created the project right so we are not going to uh go with this automatic installation rather we are going to jump to this manual installation
and don't worry it's quite easy first thing you need to do is to install this npm package so copy this one go back to your application and inside the terminal let's add a new terminal and paste this Command so it will install this uh next UI for us after that uh this particular step is not applicable for us okay this this is only applicable if you are using this pnpm package next thing we need to set up this twin css. config.js file okay so just copy this line of code and simply go back to your
application and inside the tvin css. config.js file you need to add under this content section and then save it next next you can add this plugin and Dark mode if you want so I will I'm not going to use dark mode in this particular tutorial so just copy this plugins and under this plugin section I will just replace this with this next UI plugin now make sure to import this next UI plug-in from this next UI or/ react okay so it will get imported this one and after that you need to wrap your application inside
this next UI provider so simply create the provider uh new file Called provider so here I will just create provider. TSX and inside that let's add a default template by using RFC obviously uh this shortcut to generate the automatic template with the help of vs code extension so for that one you need to install the EXT extension called react yes 7 plus react Redux react native extension along with that also make sure to install this TN CSS intell sense okay which help us to write your code much Faster now once we have this provider make
sure to render the children so here I will add a children inside here you need to provide the type as react do react node okay and here I will just just remove and we'll use this next UI provider so copy this component and simply paste it here and inside that you can just render your children now make sure to import this next UI uh Provider from this NEX UI or/ React let's save it and now let's go to this layout. TSX file and inside here you have to make sure to wrap this children inside this
provider so let me rename this provider with a capital P and now you can import it like this okay and then save it now if I go back to our application make sure nothing get changed and right now if you see we don't have any changes on The screen yet okay now next step is to add component and you're ready to go right so let's go uh to this component section and try to install this button component okay so copy this button component and whatever the component you want to use it you have to make
sure to install this component first so I will install this button component and say Yes here so first time you are installing so it will might install some of the package which we might not have Like this if you see it updated our um version of the next UI and some other things okay it happens only first time now once the button component is installed let's go to this page. TSX and over here I'm going to use this button component from this next UI or button see and here I will say subscribe and save it
make sure it is imported now if I go back to our application and boom if you see we have this button component right how cool it is now if you want to Change the color of this button component it will just provide the proper call color and say primary and boom if you see it's added the primary color now if I go to the button here you will find a lot of different example and there are lot of things you will you can see over here obviously if you want to customize you want to add
a button like this you you can customize this button according to your requirements okay how cool it is correct now if you want to Change your primary color to some other color you can do that so simply uh if you want to learn more about the color and uh other options go to the customization here you will find the colors option and this is the way that you can install the color now we already have the next UI plugin added into our Tailwind doc config.js file simply you can copy this theme section and go back
to your application and inside this T.C config inside the Next UI paste this themes information now inside the them whatever the color you want to add you can add over here inside the color section so let's say you want to add a primary color right now inside the primary I will say default color and you can add your own hex code so in this case I will add this hex code 525 3 A3 okay and save it now if I go back to our application you will see our button color is changed to something like
dark blue color okay so That's how you can update your color combination inside your tail. config.js file obviously you can also add that color combination to the dark mode as well okay so obviously we are going to use a lot of different component from the next UI and that help us to uh build our application much faster and it looks much modern now it is very important to update our application font so you can use the your own C font for your application and for that one we are Using a Google font so you can
search the font which you like it okay so you can uh go to Google font and you can search whatever font you want to use once you decided uh let's go to this go to our application I will close all of this tab and then go to this layout. TSX now if you see over here you can even use your own custom font you just need to put your font into this public folder under the font and you can refer over here but I will I'm going to remove this All of this code and we
can directly use the custom uh Google font over here so simply in order to use it you need to import it okay from and here you need to write next next SL font SL Google and whatever the font you want you just name it for example I want to use this new new to font and then after that you need to initialize that font so maybe we can use over here we'll say constant my app font is equal to you need to initialize it get a reference and then here we'll Say subsets uh you can
say acrylic Latin let's say Latin and save it now next inside the class name I will just remove all of this code and then you can just use this my app font. class name and then save it now if I go back to our application you and refresh it you will see our font is now changed to the to the font which we selected so that's how easily you can update your application Font now let's create a landing screen for our application if you see this mockup uh we have this header section and then we
have this hero section we'll keep this as simple as it is now um both the this particular screen uh we are going to divide into two different component one is for header and one is for the hero section so obviously in order to integrate or add a uh navigation bar called header with the help of next UI okay so first thing Let's go back to our application and inside the app folder obviously our default page is this page. TSX I will remove all the content and here first thing obviously we are going to show header
and after that we are going to show the hero section now in order to create a component I'm going to create a new folder under this app folder but I will give a name with this underscore components okay as you know that next uh JS or next is a next JS is A uh contains the folder based routing right and in order to avoid the routing um you need to give the name of the folder start with the underscore otherwise it will consider as a new route okay now once you define this component let's add
a new uh component file called header do TSX and another file for hero. TSX now let's add a default template for both of this file and save both this file okay now inside the header we are going To add a navigation bar now if I go to this uh next UI and just search for the uh now bar see this one this particular component right and if you see these are some of the different different example they already given into their documentation okay and really it's very easy to use Let's uh first thing let's import
this component so I will just copy this one and inside the terminal I'll paste it here next you need to import all of these uh Components so I will copy this import and simply I will paste it inside this header okay and then save it now obviously we'll do uh or we will implement it one by one first thing we need to do is to add the now bar component like this now if you want to know how to use it and everything you can go to this usage and you can search or look for
the uh code how they implemented okay but uh in our casee we are going to implement uh line by line Once you define this now bar inside there you can add the now bar content now inside the content first thing we need to do is to add the logo of our application so over here we'll add the now bar brand and inside here we'll simply add image make sure to import this image from the next SL image the source and in order to add the logo for our application I'm going to use a logo placeholder
so just search for the logo Placeholder I will use this um untitle ui.com site in order to copy one of the logo for our application so let's search it little bit and I will use this one okay so I will copy this SVG code and inside the public folder so we don't have the public folder so let's create a new public folder this need to be a folder and inside there I am going to create a logo do SVG file and paste the Code which you copied okay now inside here we'll just access it using
logo. SVG uh let's give the alt tag and here we'll also give width of 40 height of 40 and save it now if I go back to our application let's refresh it you will see nothing on the screen because uh we did not import this header into the page. TSX file so under this header let's import this uh sorry under this page let's import this header component first and Then save it and once you save it let's go back and boom if you see we have this beautiful um logo showing on the screen right now
inside the header again you can give the name of your brand here here I will say h2 tag and just will say Kito story okay here I will add some class name so let's add a class name we'll make font bold text is of 3XL and I will make text primary okay and we'll add margin left to three let's save it and if you see The text is showing on the screen but oh so this text need to be primary not 50 and and now we are good see okay perfect also after that we need
to add the menu options okay so for that one again I'm going to add a naar Content so whatever the item you want to add you can add inside this naar content okay now here we need to iterate the list of menu so let's define the menu list first so here we say constant menu list and inside here we'll say name as a Home I'm also going to Define path for each of them okay something like this and I will just copy paste this couple of times uh next I will say create story here I
will say create story as a path then explore story here will say explore and last we'll say contact us okay now in order to display the all the list we need to iterate it right so let's simply add menu list dot map here we'll say Item comma index and the arrow function now inside here you need to add a now bar item so here we'll simply add a now bar item because inside the navigation we need to show that item right and simply We'll add a link obviously so this link tag will help uh will
very is very helpful when you want to navigate on the click of the any of the menu item and for this link you need to provide the H reference tag and for the h reference we are going to add a Path okay which path we already defined over here right after this uh we need to define the item do name of the menu and if I go back let's refresh it and boom if you see we are uh we are able to display the list of all the menu also for this content you can mark
it justify and just mark it as a center so it will be in the center of the screen so it's not yet in Center because we only have the two item but let's add on one more nowar contain and in that one We want to show the button and we'll simply say get started and for this one we can mark it justify end okay and boom now if you see this m option is in the center the button is on the right hand side and this logo is on the left hand side now for this
button I will change the color to primary and uh let's save it and boom perfect now one more important thing uh currently the na bar is uh U putting Some padding from the left and right right and we want the full padding now bar so simply for this navigation bar we are going to add the Let's uh search for Max width and here you need to add a full as a Max width and boom if you see now it's full width uh now navigation bar perfect right also uh for a background of our home screen
I'm going to change it so let's go to this page. TSX file and inside here uh for this du I will add a Class name now here I want to add uh the custom color right and in order to uh apply some custom value to your T CSS you need to Define inside the square bracket and now obviously for background color I want to provide my own HX code so here I will simply add # C A D3 FF which is the hex code which I want to use it save it and boom if you
see our background color is kind of L lighter purple color okay so that's how you can Define it uh now for the header let's go To the header and let's apply some styling uh to our navigation bar item so for this one I will add a class name here we'll make it text as a Excel text primary and then we want to add font as medium and on Hover I want to add a underline and save it now if I save it if I hover on this one you will see this underline and the color
of this menu item also get changed you can also provide some Gap So I'm not sure whether we can provide over here or not let's add a class name and put a gap of three I think it's not applicable but let's margin X to 2 I think that's good see okay so that's how our header is ready now we have to make sure this is completely responsive so if I go to this uh navigation bar and if you scroll down little bit you will see some example uh that it also handle this kind Of side
navigation if I increase the screen size you will see the menu but on the smaller screen it uh it will be come under this uh hamburger icon how cool right so let's Implement that one so under after this uh nowbar content right we are going to add a Now nowar new nowar content and let's add menu instead of that so let's add nowar uh menu okay and inside that we going to iterate this menu item so let's add menu list sorry the map here we'll Say item comma index like this okay now inside that we
add a nowar menu item okay and here let's add a link H reference will be similar like this one so item. path and inside here we'll add item. name okay now once you add that one we need to add one state which will open this menu bar item okay so simply here we say constant then we'll Say is menu open okay now this will this particular state will hold the value for us it's kind of variable for us okay you might familiar with this state so here we'll simply add you state and initially we'll make
it false okay now to this navigation bar it has a one property called on menu open change and here we simply set is menu open okay and let's save it everything and let's go back and if you see we have one error that it's saying We use a use State on server side so let's make this component on the client side so here we'll just simply add use client to this particular header and boom now in order to enable that particular hambur Burger icon inside this now bar content you need to add the nowar menu
toggle so here we say have property called now Bar menu toggle okay and inside this we are going to add a class name and it will be hidden when the screen size is Smaller and let's add the areial label so here we'll say areial label which is equal to up sorry is menu open if it's open then we'll show close menu otherwise we'll show open menu okay now all of this you can obviously get it from the uh documentation from the next UI and also we have to make sure you will hide this when the
screen size is smaller okay so here we'll add a class Name and simply we'll hide it but on smaller screen we'll show it okay and save now let's go go back if I in decrease the screen size let's make it smaller and boom if you see on the smaller screen the menu is gone and now I can able to see this option as well cool right so that's how guys you can add uh or you can make this navigation bar respons you depends on the screen side now let's implement this hero section part if you
see uh on the left Hand side we are just showing the some uh tagline for application along with some button and on the right hand side we are going to add an PNG image so um I have the image already so I'm going to copy this image and then simply go back to your uh in vs code inside the public folder we'll paste this hero.png file if you want this all the image and access that is available in the source code file so the link is in the description now let's go to this hero. TSX
file and Make sure this particular component is imported under this page.js TSX file so I will add this hero and then save it make sure on the screen you are able to see this hero uh component now inside this hero. TSX file let's add a du inside this du uh we'll add a class name and we'll make a grid uh we'll make grid column one when the screen size is smaller and on medium screen size we are going to add grid Column two because we are going to uh divide this particular screen into two part
one for this one and other for this image okay uh so let's add a two de like this now let's add an image first so here I will add image make sure you're adding from next SL image then Source tag and inside that I going to refer this hero.png here give Al tag give a width let's say 700 and some height let's give 400 and Then save it now if I go back to our application you will see this beautiful uh image right uh for this main du I'm going to add a class name we
give padding X to 10 when the screen size is medium we give padding X to 28 and on large will give padding X to 44 and let Q margin top to 10 uh now on this particular D We'll add h2 tag and inside here we'll simply going to copy this content so I want same text as it is Inside here and for this one I'm going to add a class name let's say text is of 70 pixel Here If You observe I'm giving the custom size okay so I'm giving inside the square bracket then we'll
give a text primary color and the font as a extra bolt okay and let's you padding y to 10 and let's see how it looks and boom if you see it's look pretty good right and then I'm going to add one more P tag and inside that I'm going to add some uh small description okay here we'll add a class name and simply for that one we'll add text is of 2 Excel text is of primary color and then save it boom if you see this is how it will look like uh next you can
make this text sorry the font little light okay so little yeah that's pretty good next I want to add a button under the same de so we'll add a button now to this button you can Mention the size as well okay and not only that you can also provide the color like primary and save it now you will see this button obviously we need to mention the name uh something the text for the button here we will say create story okay now for this button you can even apply some custom uh button size and all
okay I will keep it as it is for now here let's add margin top to five and save it I think that's pretty Good also uh here for now I will make the height to the full let's make it screen let's see how it looks I think that's pretty good okay and that's how our uh Landing screen is almost ready only thing that if you want to add add some custom button size you can also add that one let's say you want to add font bold text is of 2XL and I will add padding of
10 as well something like this see okay now on the click on this one we want to navigate to the new Screen okay so if I click on this create story I want to navigate to the create uh screen so for that one I'm going to add a new route under this app folder so in order to add a new route you just need to create a new folder and you can give the name of the folder so let's say create story okay and this will be our new route now inside that you need to
create a page. jsx file add a default template and here I will just rename This to create story as a new screen okay now in order to navigate to this particular screen you can simply go to this button and either you can wrap this button inside the link tag to this link tag you need to provide H reference and in this case you want to navigate to this create story which is this new route which we just created and then make sure to wrap your button inside this link tag and then save it now if
I go back and click on this create story You will see it's navigating to this create story page okay and same thing if you want to navigate uh to home screen or um dashboard when user click on get started you can even create a new route called dashboard and navigate it okay but now next thing is very important because we have to make sure when user click on this create story uh user is authenticated before uh creating a new story or showing this page that's the that's the uh next thing which we are Going to
look we are going to add authentication with the help of clerk authentication service provider to our application now it's time to create a signin page for our application uh if you see once user click on this get started or one user click on the create story and if user is not authenticated to our application we are going to navigate to this signin page where on the left hand side we are going to show some kind of Image and on the right hand side we are going uh to display a login UI where user can enter
email password or user can uh log with the help of um social authentication provider like Google Facebook and in order to add an authentication to our application we are going to use the clerk authentication in many of my applications I use a clerk and uh clerk provide a lot of different features and it also uh provide a different provider support like Google Facebook Microsoft and lot more also clerk is very easy to integrate and free to use as well uh they also provide their own UI kit but if you want to customize the signin page
according to your requirements they also have the their own clerk components where with the help of that you can create your own signin and sign up page also uh in order to add uh authentication security and all clerk provide a different configuration uh to your authentication Sign in or sign up page where you can control everything at one place so first thing let's go to this clerk.com I will put the link in the description so just click on it and uh once you sign into your application just go to the dashboard once you are jumped
to the dashboard here if you see I already have a lot of uh project created with this clerk but if you want to create a new application just go over here and click on this create application give the Application name so in this case we'll give VI AI kits story generator and you can enable the provider which you want so right now I enable email Google also you can enable Facebook if you want and simply click create application on the right hand side you will see how it actually look on the screen once you integrate
it so just click on create application and now it just simply in couple of Step you are ready to use it and integrate it inside your nextjs Application now if you see right now obviously you have to select the framework which you are using So currently we are using NE JS so first thing we need to install this uh cler hdk for NE JS so copy this command go to your project and inside the terminal just paste it here so it will install the clerk for nextjs then you need to copy this environment variable key
and you need to paste it inside your EnV dolo file so we Need to create anv dolo file and paste it here and then save it next thing we need to create this middleware dots file so let's create this middle. TS file inside your root directory and then you need to paste this line of code and save now once you add this cler from middle view right you need to add the public and private route which you want to protect from the unauthorized user so I will keep the link in the description for the documentation
of the cler so on this documentation they also mention how you can um add the cler middleware or update the cler middleware so over here you have you will see this link called cler middleware reference click on it and just scroll little bit over here you can uh add this line of code in order to add the protected route okay so just copy this one go back to your application and simply paste it here now Make sure to import this clu route matcher and whatever the route you want to protect you just need to mention
here let's say you want to protect this create story uh route right so you can mention here like this now whatever the uh if you have the sub doain or sub route right after this or nested route we can say then you can mention uh like this okay uh not slash something like this and then Simply Save it after that let me go back to this page and then you Need to wrap the cler provider sorry wrap your application into this cler provider component okay so if you see this is the next step so simply
what I will do I will just you can use any of this documentation okay this is just a shortcut kind of thing this is the complete detail documentation so I will copy this clerk provider and instead of adding into a layout I will add inside this provider like This and make sure to import this clerk provider as well and then save it and we are ready to use it now if I go back to our application Let me refresh once and click on this create story if you see it's navigate to some other domain but
it's navigating to signin page because our clerk sorry the create story route is protected see okay but if I I'm trying to access the Local Host 3000 I'm able to access because we did not protect this home screen route Right but the question is how can we change this domain because our domain is local L 3,000 and we want to use our own domain right so for that one uh you need to create your own custom signin and sign up page so just click on this one so it will navigate to this particular page where
uh you need to create your own custom signin and sign up page so it's quite easy so first thing just copy this path okay and then go back to your application now inside this app folder I'm going to create a new folder called a but I start started with the folder name with this circular bracket because I I want to keep sign in and sign up page into one folder so I will keep like this and inside here again click on this plus uh create file icon and simply paste the route which you copied so
automatically it will create the folder and then the file for you then simply copy the content and then paste it here and save it same thing you need to do For the signin page so I will copy this and then make sure you are in AU folder and simply click on plus icon add this one and copy all of this content and then simply paste it here and save it next thing if you scroll down uh you you can uh add this uh inside your public route but you don't need to because if you did
not mention this route uh inside your clerk middle middleware then by default uh it it act Like a public route only okay but you have to make sure you will you need to add this environment variables inside your env. loal file so just copy this one and inside this env. loal file just paste it here and save it next if you scroll down little bit I think we are ready so now if I go to this local 3,000 and click on this create story and boom if you see in this case you are on your
own domain and now it's going to The signin page and after redirect it will go to this create Story Once you sign in how cool right now our signin page is not look good so let's redesign this one so let me close all of this tab for now and let's go to the signin page so on this one I will just wrap this in a curly braces and inside that I will add a du now for this du I will add a class name and I will divide this screen into two parts now first obviously
on smaller Screen I will make GD column one but on medium screen we'll make grid column two okay then we'll add two D in One D we'll simply add this signin component from the clerk and for for this one we'll add a class name we'll make them Flex justify Center item to be in the center and then save it now this is how it will look like but let's add height to the screen now it is in the center of the Screen okay now on the left hand side I just want to Simply add the
I want to add a image so inside the public folder I already added this login.png file okay obviously you can get it from the uh source code so the link is in the description or you can access it from the tug.com now um over here I will add the image tag then source and simply you can refer using login.png give Al tag as a login here you can give a Width and the height and save now if I go back to our application this is how it will look like okay okay um you can even
make add a class name and just make WID to full okay and your signin page is now ready okay now if I decrease the screen size this is how it will look like so obviously sign in page at the bottom and then image at the top okay so in order to put this signin page on the top when the screen size is Smaller you can just mention here on smaller screen or just add order first on the smaller screen and when the screen size is medium or larger you can add order last okay now if
I go back to our application go to the inspect panel and when I decrease the screen size you will see now the login kit is at the top and the image at the bottom perfect okay now if I click on uh any authentication select go account and sign in then it will redirect you to this create Story okay now if I go to this local 3,000 on the uh header we did not make any changes yet but once user is login we have to make sure this button will change to the dash with the text
dashboard and here we need to show the user profile icon let's do that so first thing I will go to this header and inside this head where we have this get started button right now in order to check whether user is signning or not clerk provide their One hook called use user okay so it also provide the user information but this use with the help of this use user Hook from the clerk NE make sure it has a method sorry the uh property call is sign in now when you are using a hook make sure
uh the component is on the client side and if I go over here here simply I'm going to add is sign in if user is sign in then we are going to show text called dashboard otherwise we are going to show text called get Started and I will just remove this text and save it now if I go back and refresh it you will see the text called dashboard because we already sign in in order to show the user profile image clerk has one component called user button okay and that's all and if I go
back you will see this profile icon if I click on this one you will see this beautiful option icon if I click on manage account you will see this model where you can manage your account if you Have also have option to sign out as well okay now obviously on the click of dashboard we want to navigate to the dashboard or on the click get started we also want to navigate to the dashboard so simply I will WB this button inside the link tag it will I will provide the H reference as/ dashboard and I
will WRA this over here okay now we don't have the dashboard route yet so let's create a dashboard route so inside the app Folder create a dashboard route and inside that we'll create a page do jsx not TSX TSX file add a default template and I will just rename this to dashboard okay now if I click on this dashboard it will navigate to the dashboard page okay but if I sign out you will see the button text is changed to get started and if I click on this get started obviously I have to uh sign
in first and obviously after that it will redirect it to the dashboard page okay so that's how Guys you can add the sign sign in and sign up page obviously you can add or customize your own signin and sign up page and we we also learn how can we protect the route from the unauthorized user and we added the authentication for that one in order to access that particular route if you have any question any doubt let me know in the comment section now before moving to the next part uh if I go to this
clerk dashboard you will see that new user who Sign in with this all the information uh you can obviously see the all the user list because clerk recently updated their uh UI dashboard UI so you will see this all the users under this user section also uh CL also provide the an organization features okay right now we are not implementing in this particular uh application but if you want to learn more about the organization feature let me know in the comment section and next we have configuration where you can Enable the social Authentication right now
we are we are in development mode but at the end of the tutorial I will teach you how can we move to this production mode as well then uh you can obviously enable the multiactor authentication you can have sign up restriction if you have organization or um you want to enable the sign in or sign up to certain domains you have option to do that as well you can even have option to block list you can add a Restriction and there are lot of different other option which you can Implement so guys let me know
how you what do you think about the clerk and let me know if it's very easy to use or not in the comment section let me talk about the tube guru.com where you will find all of my exclusive courses along with the source code for each of the project not only that but you can track each course chapter by chapter here you'll get the Source code the application Depot or you can even uh watch this complete video for free on YouTube how cool it is right also you can find your own personal dashboard and you
can track your course depends on your how much progress you did it okay and in order to join this particular tube gurji Pro membership it will just add $899 per month okay and you will get an access to the all the courses and the source code for Free now we are going to build and create story page where user going to add the subject of the story then us have option to select the type of the story whether it's a bed story or it's just a story book or educational story then we are also giving
option to select the age for uh those kids uh the depends on the age group obviously we are going to create a story and the last the type of the image so this need to be an image type okay or image style whether it's a 3D or paper paper cut or watercolor and so on okay and once you click on this create story there are some of the step that we need to follow on but obviously I'm going to tell you and uh teach you how can we do that but before that let's build this
create story page first uh so let's go back to our application and obviously if I go to this local 3000 and click on this create story we are navigating to the create story page right now if you see over Here you don't see any header okay but uh we need to bring this header to throughout the application so first thing we need to do let me close all of this tab and from this page. J TSX where we have this header I will move this header from this particular page to the layout. TSX okay or
we can just move to this provider. TSX just before this children and make sure to import this header okay and save it now if I go back to the create story and boom if you see Now we have the header on this particular local 3,000 page as well as the create story page as well okay now also we need a background or similar background throughout the application right now we just applied the background to the home screen only okay so what we can do let me go to this page. TSX and I will just copy
this background color and then go to the global CSS so let me close all of this tab Let's go to the global CSS and inside this route if you see we have this background color I will just replace it with our own hex code and now if I refresh it you will see the header has um now the background which we Define and not only there but if I go to this create story now for that one as well you will see the background perfect right now next thing let's go to this create story page
me close these tabs and go to the create story page now Inside here let's add h2 tag and inside that we'll write create your story as a text for this one I will add a class name we'll make font extra Bol text is of 70 pixel and we'll make text primary and text to be in the center and save it if I go back this is how it will look like uh obviously for this particular de I'm going to add some padding uh on the medium screen I will add a padding of padding X to
20 and on last screen I will add padding X to 40 Something like this after this uh I'm going to copy this particular uh tag line and or description kind of thing and inside the P tag I will just paste it then for this P tag as well we'll add a class name we'll just say text is of 2 Exel and text is of primary color something like this okay let make this uh text to be in the center so it will be in the Center like this okay next thing uh we need to divide
This particular screen into two different section similar like this one okay so on the left hand side we have one uh subject story and other we have story type and the same way so obviously we are going to use grid so here I will add a du and here we'll add a class name grid grid column one when the screen size is extra small or smaller but on this med screen size we'll make Creed column two we'll also give Gap and we'll make sure The margin top to 14 now first thing obviously on the left
hand side we want a user or we can say story subject then we want the story type the uh age group and the last image style okay so for each of these particular section we are going to create a component okay dedicated component so under this create story we'll create a new folder and we'll call It as underscore components because we don't want this as a route and uh we'll keep all this component which is related to this create story under this component folder now inside here we'll say story subject input. TSX or let's make
this file to TSX because we are using typescript right so make sure it's a TSX only and for this story subject input I will add default template let's save all of this Information and let's import this story subject input over here okay now make sure on the screen you are able to see this story subject input okay now first let's implement this story subject input now if You observe the mockup we have this title and then we have the text area so let's Implement in same way so over here we'll add a label and we'll
say one do SUB subject of a story sub subject of the story uh obviously for this one I will add a class name we'll make font bold text is of 4 Exel and text primary let's see how it looks on the screen perfect after this uh we want to add a text area so in order to add a text area I'm going to use a next UI text area component okay So first let's import this component so let's go over here and inside here I will add this component and once the component is imported you
can uh see this uh use case how they use it okay so simply you have to you can use text area from this next org next UI org input and to this text area let's provide the placeholder let's say write the subject of the story which you want to Generate then you can even mention the size I will me add it as a large now for the input okay the inner input you can add the class name okay so they have method called class names and inside that you can add the input uh style to
this input okay let's say you can add the resizable so you can just say you can just write uh resize to Y then you can even add the minimum uh height to 230 let's say 230 Pixel then we'll make text is of 2 XL and we'll add padding of five as well and save it now if I go back to our application you will see this input text area input okay uh oh here to this text area let's add a class name as well and we'll give margin top to three and maximum width to large
and then save it now if you see we have this area now this area obviously text area you can you want expand it just by pinching over here okay and your Text area is now ready now to this decha obviously uh we are going to add unchange event okay uh which will save the user input information so let's Implement that one as well so here simply I'm going to add on change so obviously on change it will emit the event e and here I will just write user selection so in this method I'm going to
pass fill value as e do target. value and fill name as a input okay okay or Here we say story subject now this user selection obviously we need to pass back to our parent component so I will add the make sure user selection the spelling is correct and I will keep the type for any for now now inside this page uh or inside this page. TSX which is our parent component over here I will accept as user selection not user selection uh yeah I think user selection only uh that's correct yeah so user Selection and
we are going to create a method for that one so here we'll say on handle user selection now this method obviously we are going to call over here so constant on user selection it will act oops it will accept a data okay now inside the data we need to provide the type so let's define the type for the data so here we'll say export uh I think that's just a field data so here we'll say Interface field data and it has a fill name is of type string and then the fill value is of type
string okay so this field name and field value we are sending so that's the reason we added this type and obviously here we need to define the type of this particular data so this will be a field uh let me check what the name yeah field data something like this okay and right Now I will just console it like data only and save it okay now let's test this out and make sure you are able to accept it um over here obviously we use this function and if you are using function you have to make
sure that component is on the client side so I will just make this as a client side and now go to the inspect panel go to the console and here we will say anything you write you will see on the line uh 12 in page. TSX you are able To get fi value and the field name cool right so this is nothing but this particular console perfect now next we want to show the story type on that one we are going to show that image and on the top of that one we are going to
uh add the name of that particular story type okay so for that one um I'm going to create a new component under under this component folder and we can call it as a story type. TSX file add the default Template and save it now this story type we are going to add over here and save it now let's go to the story type and before implemen printing it I'm going to create the options list option list okay now inside this option list we are going to mention the story type and whatever the kind of story
type and all so here it will be the list of object I'm going to provide the label let's say story book then we are going to provide the name okay so here we'll say story book Only and then the image URL okay I think the name of the story book uh if you did not use that also fine so I will just remove this name we don't need it but main important thing the label and the image URL now image URL if you see uh inside the public folder I already added some of the image
story like this bed story educational and lot of others okay so you can just get it from there obviously all the asss file you will find inside The source code now here I will say story.png okay and now this particular uh path is accessible from the public folder because that is the image which I added inside the public folder and here we'll say is free okay and I will make it as a true now is free is helpful when you want to add uh make this option as a paid option only premium or paid user
can access that one currently I will just make sure that uh to add that field And anytime you can make Market as a uh free as a false or no okay now copy this field name couple of times and here we'll say bed story and then educational now for the bed story uh we have the image called bed story and for educational we have image called educational and save it now let's display that or iterate that option list uh over here first thing I'm going to show the Label as story type then for this label
I will use the same label like this I will just copy and paste it here I will just reame this to story type and after this label I'm going to add a du inside the DU We'll add story type not story type the option list because that's what we want to iterate right so option list do map here we'll say item comma index and the arrow Function inside this we'll going to define the DU and inside this du We'll add the image first let's add a image from the source here we'll give alt tag as
item. label and for the image source I'm going to provide item do image URL okay now let's give width and height so let's add a width um here you can say width of uh so width of 300 height of 500 We'll add a class name with object as a cover so it will not get stretch let's define the specific height and I will also make the rounded Corner as 3XL now if I go back to our application you will see this list of all the images is showing on the screen right now for this particular
uh de I'm going to add a class name and we'll make this as a grid grid column three because I want to show three images in one row and we'll add a gap of five margin top to three and boom If you see now we have this beautiful uh story type section also uh now we want to add an text on that one so above this image I'm going to add an S2 tag to this S2 tag we are going to show the label so item do label so obviously for each of these object we
have this label right that label we are going to show obviously if you see this label is coming on the top of this image and we want this label on the image okay on the top of the image So simply for this h2 tag I will make it as a absolute okay and after this we'll just say bottom to five and we'll make this text to White also make text to be in the center and make sure to provide the width as a full so it will be in the center so right now I don't
see that particular text so let's give to this particular de uh let's add a class name and just mark it as a relative okay so it will bound To this particular de only and now you will see the name of that particular uh story or label for each of these options now along with the h2 tag I will I will also make this to excel uh and I think that's pretty good see now we have story book bed story and educational okay now I I'm going to add also and some um awesome effect to this
one so simply what you can do uh to this S2 tag um you just add a how not how but Here we'll make a gray scale so once you make this gray scale you will see it will be in the kind of back uh black and white color okay but on H you can add gray scale to zero and let's make it cursor pointer as well so as soon as i h on this one right everything getting uh hovered okay let me fix this so let's remove this gray scale line because we want inside this
DP okay like this and now if I go back see as soon as I hover on any of this item only that item the Color of that item is get getting popul populated because we make it gr scale zero on the how how cool it is right perfect now once you just select any of these particular option we have to make sure that particular um card or particular option is get selected okay and we want to show it as a light the color one so simply uh over here I'm going to Define one state so
here we'll say selected option option comma set selected option Is equal to use State okay now make this component as a client side and save it now once us a click on this particular du so on the click of this du I'm going to save the name of that particular option so here we say set selected option as item Dot label okay and obviously uh we need to provide the type so I will provide the type is of string and save it okay now as soon as us just user click on this One it will
get selected but we have to make sure that it will once you select that any of the option it will get colored so over here let's wrap this inside or not this uh wrap this particular de inside the curly braces because we want to an add this tag because we want to add a dynamic styling so or conditional styling okay so here we'll say selected option is matches with the item do label if that condition is true then we'll make gray scale to Zero and uh otherwise we'll make gray scale okay means we will make
it black and white only let's save it and test this out if I select this one you will see now this story book is showing color okay if I select this educational one see okay so that's how it works also if you want to add more styling you can add a border to that one so what we can do uh let's add a padding one to this particular uh D and whenever user select something right we are going to add a border we'll make border rounded 3XL and we'll also make border is of primary color
and save it now let's test this out and if you see if I select this one you will see the Border okay so any of this you select you'll see this beautiful border which we added you can even uh H change the Border size to two Little border see perfect right so that's how guys um you can add The Styling and effect to your component now same thing we are going to implement for the age group obviously the image uh or we can say option list might be different so what we can do we can
just copy this component as it is so I will copy everything okay until this point I think yeah and then we'll create a new component and we Can call it as a age group. TSX add a default template and then simply I will p it here okay now obviously make sure to import the use State and then make sure to import this image from the next slash image now only change you need to do is to CH update this option list so here we'll say 0 to 2 es here we have the image URL called
02 es 02 es.png then uh here I will make 3 to 5 ES and for that one we have 35 years as a image and the last 5 to 8 years and for that one as well we have 58 years as a image okay and obviously that image going to render let's save it and make sure to update this label as well this is the third option and here we'll say age group and save it now if I go back to our application uh I think we need we need to add that in page. TSX
we Did not add it here make sure to add this component called age group and boom if you see we have this age group as well and the the assets or images is quite relevant to the age which we are showing if I user select 3 to 5 it will get selected 3 to 5 obviously the story type we selected as a this story and so on now next uh we want to add that image style section okay so it's little bit different but it's similar the way which We added for the story type and
age group so what I will do I will create a new component and I will rename name it as a image style do TSX add a default template and save it and now make sure to add this image tile inside your parent component called page. TSX so over here I will add image style component and then save it okay next uh let's go to this image style and inside here I will copy the same thing okay so I will just copy everything as It is from the story type to image style and let's make sure
to import this use state along with the image okay now obviously here we need to update the list of the options so here we update the label called 3D cartoon okay and the image Ur will be 3D so which is inside this public folder see something like this then we Have paper cut and here we will say paper cut as a image URL then water color here we have the image URL called watercolor and there is one more I believe yeah we'll call it as a pixel style pixel styp okay and then save it now
over here if you scroll down little bit right uh inside the image I'm going to change this height here I will add a height of 120 pixel And um I think everything will be as it is let's save it and let's test this out and if you see this is how it will look like obviously pixel style image is broken make sure we have this pixel style image is just a pixel so that's good so here only pixel see okay so any of this if you select anything right let's say 3D cartoon and that's how
your selection will look like how cool right perfect guys now as soon as You select any of this field we have to make sure because um similar like this subject of the story we are sending the data back to the parent component same thing we need to do it pass the data so let's go back and let me close all of this tab for now and here we have the story type okay okay now from the story type as soon as user select any of the story we need to send data back okay so you
have two option either you can create a method And on the click of that method you can select the options I think that will be easy to understand so here I will add a constant on user select and inside this method I will call this set selected option method like this obviously it will accept the item and obviously we need to provide the item type so the item type will be this label image and all so here we'll say con let's define the Export interface and option fill inside this we'll put label isop type string
then image URL is of type string and is free is of type bullet okay and this option type I'm going to pass over here so here we'll say what is this name option fill okay now this option field we are passing to this and along with that one make sure uh to call this on user select Over here and and we pass we are going to pass item okay obviously this item is of type option fill I think here as well we need to give option fill only that's where let's make it any for now
okay perfect now once we have this option fill over here also we are going to add user selection method okay now inside this user selection method let me bring this down like this inside here We'll pass field value okay now field value will be the item do label and then field name now field name is story type okay make sure uh you need to give the field name uh different for each of these particular component now this user selection we are going to pass back to the parent component so here is the selection is of
type any let's save it and let's go back to the page. TSX and similar way I'm going to call this story for the story type see now same thing we Need to implement for the age group and the image type so let's go to this age group okay and the whatever you implement for the story type I will just copy from here and then let's go to this age group let's add this method make sure to import this option field okay because this option field we already defined ins inside the story type and we exported
it so that we can use it and this user selection will pass over Here now obviously uh you need to call this on user select method inside this perfect okay okay uh let me copy now again this particular method to the image style as well and make sure to import option fill the user selection we need to pass back to the parent component and then we need to call this on user select on the click like this also I think my bad um inside The story type right we Define this item over here we don't
need to Define anything because this is just on click event right and on the click of that one we are passing this item as it is let's save everything and I think we are good now let's test it that uh on each of the selection we are getting the data printed on the console or not because we have this console right so let's go back and if I click on story book boom if you see we the story book and field name as A story type if I select the age group you'll see it's showing
the age group and for oh this need to be rename obviously and then paper cut for the image style make sure to update this image style so here as well we need to update this as a image style and the name as a image style so definitely I will recommend you not to copy paste okay Many times you make some changes and it's difficult to find inside the age group as well change this field name to age group and then save it okay now we have proper information and all the field name and field value
correct perfect guys so that's all for this particular section I hope you understand and that's how beautiful we implemented this create story page obviously at the bottom side we we are Going to add a new button uh in order to generate the story so let's go to this ph. TSX and after this du I'm going to add one du and inside that we'll create a button here we'll say generate story now to this generate story U to this button obviously I'm going to add some style so first thing let's make it as a primary as
a color then we'll add a class name We'll add padding 10 text is off to Excel and then save it now you'll see this button right now we want this Button on the right hand side so over here we'll add a class name we'll make it flex and justify 2 N margin y to 10 so that button will move to this right hand side like this cool right and obviously on the click of this one we are going to save this information first then we will generate the story from the AI and then we'll generate
the image as well that I'm going to tell you complete flow in the next section but before that once user Click on this G story we have to make sure that all this information will be inside one form State okay so let's go back to this page. TSX file and inside here I'm going to Define one state so here we say constant form data comma set form data is equal to use State like this now to this form data we need to define the type so here we say export interface form data type here we'll
get the story subject is of type string then Story type is of type string the image style of type string and then age group of type string okay and this particular uh type we are going to assign to this form data and then save it now whenever user uh uh select some input right we have to add that inside our form data so in order to add it we'll first let's check if form data dot story subject is there then form Data dot age group is there form data dot image style is there RR form
data dot story type I think yeah story type is there so all this information need to be there story subject age group I think yeah this need to be a double vertical bar and inside if condition we'll simply consol it with the data okay oh my bad actually uh first on the selection right we don't need this line of code on the selection we just need to Add that data into form so here we say set form data to previous is of type any let's define any and here we that spread operator then data do
fill name and then their respective value as a fi value perfect right here let's try to add the FI data as a type but it's not accepting somehow let's keep it as any for now and now if I console over Here you will see the form data obviously we are are updating and we are just immediately showing it right so you that state is not getting updated immediately but still I I want just wanted to show you that we are able to add that inside our form data okay so if I add story type or
something if I write something you will see the story subject with a field name and their respective field value okay also if I select this story book then I will select age group Image Style and everything you will see inside this form data cool right now this information obviously we will need it in order to generate the story but guys until this point uh let me know if you have any question any doubt here I will just rename this to uh use to add data to form and save it now we we need to set
up a back end for our application so that we can store all of this information let me walk to The workflow now now once user select all of these options and click on this create story we are going to save all the users selection to our database and obviously we are going to use a free service for that one we are going to use a drizzle orm with postgate SQL then once we save that data then we are going to generate the story with the help of AI and for that one we are going to
use a jini ai jinii ai is a Google uh it's produced by Google and it's Completely free to use okay and then once it done then we are going to use an open AI dial a two or three model to generate the image for our story book obviously here uh we are going to generate the story book cover image along with the chapter's image as well but this service is not free you have to pay at least minimum uh 0.018 which is equivalent to uh 1.3 rupees in India and then everything is done then obviously
um this image Information also we are going to store it in our database and then we will save it now whenever uh we generate the story with the help of AI here we have going to update the database and same thing we are going to do once we generate the image as well okay the that record we are going to update it so guys uh first thing we need to set up a back end for our application so for that one we are going to use a drizzle orm so drizzle orm is a typescript based
object Relational mapping and most important very easy to use if you are don't have um experience with the writing the SQL and all don't worry because dzel orm is the best option for it so first thing uh let's go to this or. drizzle. team and then click on documentation uh on the left hand side you will see the postr SQL because on the database side we are going to use a postgate SQL database and we are going to use a Neons postgate SQL database Because they provide a free post create SQL database okay so next
you need to run this command so copy this command go back to your application and simply paste it here so it will install the drizzle um omm for the neon database and then you need to install this drizzle kit as well so let's paste it here and let's install the D kit next thing we need to write this line of code I will just copy this uh for four lines and inside our Config folder let me go to this config folder I think it's at the over here I think we don't have the config folder
let's create a one so here we'll say config inside that we'll create a DB do um TSX file and simply paste this line of code next um if you see uh we we need this dzal database URL okay and we don't have this database URL so let's do one thing let's go back to this neon dote okay now neon dotech will Provide a free postgate SQL database simply uh if you don't have an account create a new account and I will just log in and once you log in you will find this uh dashboard uh
select the project if you don't have a project just create a new one and once you jump on this uh project dashboard here you will find an option called database here you have option to call a create a new database in many of my tutorial I use a neon P gr SQL database now here you can give the Database name let's say AI kids story Builder or generator let's say Builder okay and just simply say create as soon as you create it you will see this connection string copy this connection string go back to your
project and inside your env. loal file here we'll create a new en environment variable we'll say our database URL okay and paste this connection string now copy this environment variable and make sure to replace it Over here okay then make sure to export this database this is very important so that you can use it uh in uh throughout the application once you're done with this one you need to create the schema for your application it means obviously the schema is nothing but the uh to create the database to our postgate SQL database and in order
to uh uh apply the crude operation you can do it with the help of schema so inside this config Folder let's create a new file called schema. TSX inside this one we'll simply say export constant story data okay and you'll say is equal to PG table because we are using postgate SQL table inside here you need to provide the table name so I will give story data and then the curly braces so inside here you need to define the column uh which you want to create it so first Thing I will want to create the
ID is of type serial make sure you're inputting from this PG C uh this is nothing but the actual column name and this is the mapping for that one because with the help of this field we are going to add and do all the operation but this is the actual column name which is inside the PO SQL database here I will make it as a primary so it uh atut to increment the ID and it will be Unique then uh story subject this will be a text again make sure you're importing from the PG core
and here we say story subject and it will be a not null so make sure to add it as a not null then we'll Define the story type uh which will be a Ware and then I will give the column name as a story type and which is again not Null I will just copy paste this multiple times then we want to add the age group make sure the column name will be the age group then image style I and here we'll make image style uh right now let's let's remove this not null okay I
will keep everything as it is for now I don't want to make it complicated because we are using typescript and it might be Difficult many of the users okay to figure out figure it out the some error and what uh once we have the image style then here we'll say output output uh which will be in the form of Json so make sure you inputting from the PG core again and here we'll simply say output okay and we'll say cover image which will be a book cover image again it's a where care and I will
write as a cover image okay and then save it now this is your schema it's Ready now inside the db. TSX file make sure to import this schema as a schema from and give the path and this schema you need to add inside this drizzle uh function so here we'll say schema and then save it okay now next important thing we need to do inside uh from the drizzle orm is to add a configuration file so go to this drizzle kit okay and just scroll down little bit here you will find the configuration section now
this line of code you need to copy it And you need to add it inside this drizzle. config.txt file and simply P this line of code now here you need to define the correct schema so here we'll say config SL schema. TSX and over here you need to define the database URL so many of the time uh from the environment variable it is not accessible so simply uh copy this URL and paste it here like this okay now if you see we don't have that error and save it and The last thing we need to
do is to write a script in order to uh run or push the schema so that it will apply to the database so here we say DB push and simply we'll say drizzle kit push and another one for the studio diesel kit Studio okay and then save it and that's all now let's go to our terminal and simply run npm run DB push now DB push command obviously will execute this digel kit push which will um create our New table if it's not already there and if you see it's creating new table here um yes
I want to execute all the statements yes and boom here you will see the text called changes applied okay now let's run another command npm rum DB Studio in order to see your data now it will run this local drizzle. Studio open that in a uh webs uh in your browser and boom if you see we have this uh story data created and here you will see your uh Table name and the column which we added how cool right now let consider you want to make some changes to your schema and you want to add
one more column let's say you want to add a story ID okay obviously uh it will be a Ware and let's give the name of the uh column which is Story ID and then save it now in this case just stop your studio and again run the DB push command okay so what it will do it will alter your table and here it will say you want To alter say yes I want to execute all the statement and boom now again you can run your studio refresh this uh link and here you will see a
new field get added called Story ID and that's how you can update your column as well without affecting to your data okay so that's how guys our back end is getting set up uh if you face any issue while set uping set uping in the back end let me know in the comment section now it's time to generate the story with The help of a gini API now gini is a Google's uh AI model and it's free to use just go to this ai. google.de and you will see this official website you will also see
the example how to use it and all of this information just go to this get API key in uh AI Studio you will jump on this uh Google AI studio in that one you can generate the new API key I already have one so I will use the existing one and inside this create new prompt you can write the prompt whatever The prompt you want to um use it okay now I already have the one prompt in order to generate the uh story okay and if I will just paste it here obviously this prompt is
also available in the source code uh the link is in the description now if You observe in this particular prompt I just write okay create a kid story on the description okay uh then I give the age then the type of story and the type of image style okay and the main thing the Subject and here I also write give five chapter the detail image text prom for each of the chapter uh the image from from for the story cover book with the story name and everything I want in the Json format okay let's run
it and let's see what we are getting and boom if you see we got the story name covering the chapter and all of this information and everything is in detail right for each of this uh description we have the uh prompt as well okay now if you want To change anything in that one you can just update your uh particular prompt and then you'll get the result as expected now once you satisi satisfy with the your uh result you can just click on this get code and once you select get code make sure to select
this from the JavaScript and this is the code that you need to use it before that let's install the generative AIS SDK with this with the help of this command just copy this command go to your vs Code and add a new terminal here I will paste this one so it will install the uh generative Ai hdk and then copy all of this code go back to your application and inside here inside this config folder I will create a new file called jini AI do TSX file and just paste your all the code now obviously
we are going to modify little bit first thing we need to add the jinii API key so simply um inside env. loal I will create oops I will create a new en environment Variable J Min API key and then let me close this for now and let's go to this API key just copy this API key don't use mine and simply paste it here and then save it okay after that make sure you will replace this with the actual name of the API environment variable key like this next thing here uh the response type we
want in the Json format so here I will just update this one application SL Json okay and uh here You can update the model if you want to update it current currently this particular model is free to use um here the role and the users okay obviously you can update this uh history I kept this history because whenever uh you want to generate a new uh uh story right it will uh refer the history and depends on that one it will exactly create or generate the output for you okay so that's the reason it's very
important let's remove this function I don't want To keep this in a method so I will remove this this call as well and we don't want this particular lines so I will remove it but you have to make sure you will export this chart session so that you can use it anywhere perfect and your jmin a model is now ready to use let's go back to our create story where we have page. TSX file and over here we'll create a new method called constant generate Story and this particular method we are going to call on
the click of this generate story button so simply I will add on click method and I will call this generate story method okay now first thing we need to generate AI story then we need to save uh everything in database and then uh we will generate the image okay first the most important thing we need to do is to generate a story and Save in database okay so here I will add a try cach block first light exception e and console.log the error okay now over here we'll write constant result is equal to await chart
session now this chart session you have to import from our config jmin API do send message okay and then inside here you need to send the prompt now we don't have the prompt currently right and the prompt we need to send it in order to generate the actual uh story so here um let's copy This prompt I will just copy this all of this information obviously as I said you will get it in the source code and inside the environment. local I will paste it so anytime when you deploy your application you can modify your
prompt as well so here we'll say uh create story prompt is equal to and simply paste it here now couple of things I'm going to modify because we Want to update our um prompt depends on the user selection so the ear things I'm going to replace with the age group something like this then the story type here I will replace with this story type then the image style here I will replace with the uh image style and the subject story subject here I will replace with this story subject okay and everything I will keep as
it is perfect right let's say it let's copy this enement variable and go back to this particular page now At the top I will just get the AI create story prompt is equal to process. and just paste this environment variable key now from this prompt let's scroll down little bit and over here we'll generate final prompt is equal to and we use this create story prompt dot will replace this each of this particular name with the actual value okay for example first thing let's replace this Age group so we'll say age group we want to
replace with the form field or form data which we have do AG group see something like this right uh let me bring this down and I will copy copy paste this multiple times then we want to store change the story type with the story type then story subject with the story subject and Then uh what else remain image Style with the image style okay something like this I think we have an error I think that's enough we are getting error because we we did not mention that form data has a value every time so here
you can add the condition double quote and empty okay it means if the value is not there it will pass the empty value okay something like this and uh make sure it's a double question mark Something like this and we got the final prompt now simply I will just print this final prompt okay and just make sure that all the uh value is getting replaced correctly so let's go back to our application and inside the inspect panel inside the console let's add uh some story line okay then we'll select the story type let's say bed
story and 5 to 8 years I want a 3D cartoon okay and simply click generate story I let make sure it is connected it is connected okay let me refresh once okay now we are good now let's select write the story line bed Story 5 to 8 years and 3D cartoon and click generate story and boom if you see we have the proper prompt depends on the user selection right now this prompt we are going to use it in order to generate the story and we are going to pass this prompt to the to our
AI model so I will remove this console from here and I will Just uncommit this code which we write before so here we are using await let's make this as a sync and to this send message we are going to pass our final prompt okay once we got the result we'll just console this result in the form of result dot response. text okay now obviously if you remember um if I go to this code right and if I scroll down this is how we'll get the result from the uh jini AI response Model okay so
that's what we wrote over here and save it now just to make sure we will also maintain the loading state so here we say loading set loading is equal to use step initially I will make it as a false and and when we user click on generate story we'll set the loading as a true and once everything is done we'll set the loading as a false and over here as well we'll set the loading as a false if we Have any error now whenever the loading is true we will disable this button so just add
disable and set the loading and save it now let's test this together and most important we will see whether we are getting the response from the J AI model so let's go over here clean everything uh here we'll say elephant and ant friendship okay we want to write a story on elephant and friendship it's a Story book uh just write for the three and five years and I want 3D cartoon let's generate the story and right now if you see the button is disabled okay now we'll wait and boom if you see we got the
complete story line with the image prompt for the cover okay with the title then the chapter we have each chapter has a title the chapter text and the image prompt for each of the chapter right and obviously this image prompt we are going to use to generate the images As well but for now this is very important that we are getting the result now this result along with the user selection we need to save into our database let's do that so let's go back to our application and this information we are going to save it
in one state uh one sorry one variable if you want to but let's keep as it is for now now obviously this is saving database so what I will do obviously we Need to write over here I will create a new method called save in DB and the logic which we want to save that uh information in database we will write inside this same DB um method and just we'll call over here save in DB here we'll say output so not output but here we'll pass this result as our output okay and over here I
will accept It as a output okay is of type string for now now next next we need to save that in uh in our database so here we'll say constant result is equal to await DB now this DB reference from our config DB uh and then we want to insert the record so with the help of dzel orm this is the insert statement this is the query of which is given by the dzel orm now insert on which schema that schema name we need to provide so here we say story Data from this config schema
dot values and all the values we which you need to add here we are using aent let's make this as a a sync and let's add all the value one by one so first thing let's add the um Story ID which is very important okay so in order to generate the story ID um let's use the yuu ID for Library okay so what is it will do it will generate a global unique identifier so I will copy this npm Command and let's paste it here and in order to use it you need to import this
uu id4 and then you can just use it so here we'll say uu id4 oh not like this uh here I will create constant record ID is equal to new id4 and this record ID I will pass over here next we want to add the AG group so this age group will get it from the form data. AG group Next we want to add the image Style again form Data do image style then the story subject which is form data do story subject and uh story type obviously we want to add form data dot story
type and the most important the output right so the output as a output now this output we need to store in the form of Json so here I will just pass this in the form of Json here we say output perfect and all the fields are uh we assigned okay now Once the record is inserted we want to return as a inserted recorded ID okay or not inserted ID but here you can provide the whatever the field you want to add let's say Story ID you want to return so here you can write the story
data dot whatever the ID you want to return in this case we'll say Story ID perfect right and this is the result obviously I will wrap this in a try catch block something like This okay now once data is inserted we will just return the result now this result will come over here so here we'll say constant uh response is equal to and we'll conso the response okay perfect uh here make sure that we are making loading as a true over here we'll set the loading as a false and if we have any error we'll
set the loading as a false perfect okay now obviously this method is used to save that data into our Database uh we have errored that import uu id4 let me check the input because that input is not working and let's import it here I don't think so it's working so this error is coming because of this typescript okay so you just write TS ignore and it will ignore this I don't think so yeah at theate TS ignore something like this okay now it's ignored I think it's let's save it and Let's go back to our
application now everything is good let me bring this and let's write subject line so here we'll say uh three friends and magical line okay this is the subject line now you can select the story type then age group and the image style okay now I will say generate story now as soon as gener click on G story this button get disabled now we'll wait and if you see the story is generated perfectly fine okay and it's rning the Promise online page 52 let's see where so over here it's generating the okay so it's a generating
promise because we did not use await over here we have to use a wait okay and if I go back to our dzel studio and boom if you see the new record which uh which the story which we just created is get inserted it has a story subject type group image style and most important the output and in that output you will see it is in the form of Json how cool right So that's how guys you can uh generate the story with the help of AI and obviously um we are need to create the
image but the important thing we are able to generate the story with the help of gini AI model so if you any question until this point let me know in the comment section now the most important thing that we need to implement is to add the loader as well currently we don't have loader and we user don't know whether it's Getting generated or not right and obviously we are going to add our own custom loader and I'm going to tell you the best trick to add the your own custom loader so very first thing we
need to do uh let's go to this next UI and just search for kind of dialog or model so it has a one component called Model okay and if you scroll down and if I open the model so this is how it will look like and that's what we wanted so first thing um we need to import this Model let's go back to your application over here I will paste it then you need to import this all the component and for the loader I'm going to create a new component here we'll say custom loader. TSX
file add a default template and at the top I will import this all the uh components okay then in order to use it right you you you can find the use case so this is a non dismissable okay that's what I wanted Actually I will use this one go to the code and first thing I will copy this line okay which is the hook from this use disclosure make sure to import this one then I will copy everything as it is from the model see okay now we don't want footer I will remove this footer
model footer and over here I will write use effect and inside this use effect I will just call on open this particular method okay and save it now in order to use This custom loader you need to import that custom loader into your uh screen where you want to use it so in this case let's use over here so I will add a custom loader save it and if you see over here in use effect as soon as uh this particular component get load we are opening this model let's say whether it's getting displayed on
the screen or not and boom if you see it's getting displayed on the screen right now obviously we don't want this all of this Content so simply I will remove this header and I will remove this all of this content and save it right now you will see this just a simple line now in order to add an icon or loader let's go to this flat icon.com which contain a unlimited icons for you and select this animated icon here I will just search for the magic icons and here you'll find a different different icon magical
icon okay I think I will use this one this is kind of cool right open this one this Will be uh the gif file click download click GI and it will install the gif file for you copy this one and go to your project inside the public just paste it and I will rename this to loader okay so loader. JF file now inside the model I will use this image source and just refer to this uh loader. gii file go all tag as as a loader here we will give width of 200 300 height of
300 I will add a class name with a width Of 200 pixel let's say and height of 200 pixel and save it now if I go back to our application you will see this loader how cool right perfect now for this body I will add a class name We'll add padding of 10 let's see how it looks that's pretty good also we'll make Flex with to be full item to be in the center and justify in the center so it will be in the middle of the screen perfect something like this and then below that
I will write a text we say please Wait story uh we story generating something like this you can add any text We'll add a uh some Style to Excel and we'll make primary color and save it something like this see please wa story is generating we say story is generating that's make sense and save it perfect right you can mark this in the center so I will just make it Text to be in the center perfect okay and now this is our loader now in order to hide and show the loader okay there are couple
of way that you can do it but simple way that in order to do that let's wrap this inside One D and here I'm going to get the property call is loading okay and is of type Boolean here let's add any I don't know why we getting error and over here we'll simply add is loading if is loading is true then Only it will show this uh loading model okay perfect now let's go to the page. TSX and over here we'll pass is loading whenever the loader is true so loader value we are just simply
passing to this is loading oh so not loader we have the state called loading okay and save it now let's test this Al together so right now if you see the load is not showing and let's write some subject and story so here we'll Say uh fairy princess and frog story okay we want this bed story uh 0 to 2 years I want watercolor let's select watercolor everything is selected perfect and simply click generate story and boom if you see it's showing this beautiful model right and boom as soon as your story is generated the
model is gone we will just make sure it is inserted into Our database say perfect right we have the new story subject with this with obviously is a different output because every time we are generating a new story and obviously everything is ready wow that's cool right so that's how guys you can generate the story we added our own custom loader and lot of things that we are going to learn now it's time to generate an AI image uh for our story and you have couple of option to generate the image one of them Is
a open AI dlay three option but which cost you around 0.04 if you are using the dilay 3 Model because which is a powerful one okay and which is quite costly and the alternative for that one you have the replicate which is quite powerful F and it just cost you 0.3 per image and in $1 you can generate 300 image which is just 25 uh PES in a in 10 Rupees okay so uh let's Implement that one so let's go to this replicate tocom and which contain a lot of Different uh AI model okay which
you can use to generate the images uh music speech and lot of other things now for our example we are going to use the this black forest Labs flux Channel model okay and this is the only line of code we can use it to generate the exact model for us okay very easy to use uh so first thing you need to uh sign in with the help of GitHub and then once you land on this particular page just go to this explore and here you will find all Of the model uh depends on your requirements
most important thing U make sure to add the payment detail first because then only you can use this model as I as I told you this is uh free to use sorry not free to use but uh it's very cheap and if you see the image model which uh which is quite uh cheap on this particular model which is 0.03 per image okay so once you add the payment details uh select this flux uh Channel Model and if you want to play around this with this uh playground section you can just add a prompt and
it will generate the image for you but in in order to integrate select this API section and you will jump on this nodejs section copy this npm install replicate into your uh terminal and then copy this line of code and we are going to use the nextjs API uh functionality so go to the app here we Are going to create a new folder called API and inside that we'll create a new file called generate not new file we'll create a new folder called generate image and inside that we'll add route. J uh TSX file okay
now over here we'll simply write export a sync function and over here this will be a post request so I will add a post and the will accept the request which is type of uh next request okay Now inside here obviously it it will return the next response so this need to be a return next response do Json and here we'll say data for example and high okay and then save this one now whenever you make a request right now it will just return this data and it will return High to that particular fi value
now in order to use the replicate just Copy the code which you pasted so I will just copy again and I will paste it here okay now most make sure you will import over here this replicate then this is the input prompt okay this is the sample prompt obviously we are going to uh pass our own prompt and then you'll get the output here okay now output from the output you can get the uh uh zero element and then here we'll say image URL here output I will make type Any and that's all okay now
obviously here you need to pass an API key okay so inside here you need to add an au and just add the API key so I will add a new API key from the replicate here we'll say just a image or we'll say replicate API underscore key is equal to and then let's go to the uh your profile and click on API token and copy your API key so I will just copy this API key and then I will paste it here okay let's Save it and let's go back to application and just access it
using process. EnV do now in the post request uh let's go over here and first thing we'll get the uh request response so we will say constant data is equal to await request do Json okay and in order to get a prompt we'll simply write prompt is equal to data okay so we'll just D structurize this data with the prompt and we'll get this prompt Information next we'll just replace this prompt with our own prompt so here we will say prompt and along with that you can pass more option okay so here you can even
say the output uh output format and you can pass whatever permit you want let's say PNG okay you can even uh add the quality the output quality as 80% and aspect ratio also you can add so here we'll say aspect Underscore ratio and which is one by one okay how you want that that's how you can add that one okay obviously everything you will find uh it on their documentation so if I go to the documentation click on learn more you will find all of field which you can pass okay and then once you add
make sure that model name you will add the correct model name which you want to use and the input which we Already added and we obviously consoling it and once we got the data we'll return it to our um front end side okay let's save this one and let's test this out now inside your page. TSX once you generate the story then uh just before the save in deit datab database we are going to generate the image uh response is equal to a wait we'll use AOS so make sure to import the exos first which
is the HTTP client and once it installed you can use This xos to make an post request so this post request will be on slash API slash and the generate image so we'll say generate image okay now to this apia we need to pass an uh body so obviously in the body we are going to pass prompt and in the prompt we'll just say add text with title and uh uh here we say add book cover text book cover text or we'll say just add a text with a Title and we are going going to
add a title of the story okay so if I go back to our response let's face the story title so it which is inside the story cover and then title so here we'll just simply say result or let me save this in one state sorry one constant first story data or just say story is equal to this text and simply we'll say copy the story cover story Dot this is in the form of Json so we'll make sure to pass this one and then we'll say story. story cover. title okay so this is actual the
uh title of the story plus and then we are going to add the prompt here I will also write in bold text for book cover okay and then we'll add the actual prompt so which is inside the story do Story cover and just get the field name so the Field name is image prompt like this okay and that's all I think and this is our uh post request okay let's save it and we just console this one so console.log image response do data uh and inside that I think we are returning as a image URL
okay so do image URL and save it now right now I'm not going to uh save it inside the database before that we'll just test this out Whether we are getting the image response or not uh let me just get the data first and then save it and let's test this out so now let's taste this out go to the console and I will add some text here we'll say um okay now let's select the story type you can select the age group and style of the image and simply click generate story and the story
is generated now we'll wait for the image and boom if you see we CAU the response from our API and Which will returning the image URL now let's open this image URL in a new tab and this is how our uh book cover story or cover uh will look like okay with a name on it and beautiful uh image on that one once the image is generated through the AI we have to store this image at our uh server or Cloud because uh replicate will only store image for hour and after that they will just
remove from their server okay and we have to make sure that image is getting Stored on our server so for that one we are going to use a Firebase storage let's walk through how we're going to store this uh store this particular image first whatever the URL which we are getting uh for that particular image we are going to convert this into a base 64 okay then this base 64 image we are going to store it through this Firebase hdk to the Firebase storage and then once you store it obviously we will get the URL
of the stored image from the Firebase and that URL we are going to store it into our database sounds good right let's me uh open this Firebase okay and uh the reason that we are using Firebase because the storage uh is free so that's the reason we are using this Firebase and it's very easy to and convenient so first thing let's go to the console and if you don't have an account create a new account so I will just open the account here you have option to create a New project I already have a lot
of project created but you can create a new one and select the web here you need to give the project name so I will give AI story generator I will say kids story then register app and then it will add your application to the Firebase console Next Step you need to install this Firebase hdk so copy this npm command go to your project I close this tab for now and I will paste it Here and then next you need to add this config file uh with all these Firebase configuration so inside this config folder I
will create a new file called Firebase config do TSX file and simply paste all of this thing then you need to just make sure to remove this API key from here okay and put it inside your env. loal file here I will say Firebase API key is equal to and paste this key okay obviously here need to add nextore public and Firebase API key go over here and just make sure to add this Firebase API key over here inside this Firebase config and then remove this analytics part here you need to add export constant storage
and you have to add this or initialize this storage over here okay inside this get storage you need to pass this app reference and then you have to make sure to import the Storage from Firebase SL storage okay perfect now your storage initialized and once you export it it means you can use it any here now let's save it and in order to uh upload this images to the Firebase storage we are going to use it through this API so let's create a new API under this API folder and we are going to call it
as a uh save image inside the save image let's create a new file called router. TSX and then here simply we are going to add a Function export a sync function this is a post request so post here the parameter will be the request and is type next request okay next let's get the data so whatever the URL which we are going to pass we'll get it here so inside the data we'll say await request. Json and constant URL is equal to data so we dest structurize the URL from this data data okay next as
I told you that we need to Convert this into a base 64 image so here we'll say constant base 64 image is equal to now obviously the base 64 image once you convert uh you need to add the base 64 uh tag so you you have to write like this data image is of type PNG colon base 64 comma okay then you write we have to write a wait and then simply we need to convert over here okay now in order to convert this uh image to Base 64 we'll write one method so let's create
a new folder called services and inside that we will write that method okay or you can just simply create over here so here we'll say export constant con convert image is equal to a Sync here will be the image URL of type string oops and the arrow function now let's add a TR catch block and inside the tri block we'll say Constant response is equal to a wait and here we'll make a call exos call with a gate operation and inside that oops here we have to make sure we'll import the exos doget and then
we'll simply pass the image URL here we will also pass the response type and which will be the array buffer okay and then once you get this here we'll say constant base 64 image is equal to buffer o Sorry buffer from and what are the respon will get so that is in the form of response. data. two string and here we will say base 64 cool right perfect and then we'll simply return the base 64 image otherwise we'll just console over here error converting base 64 image okay something like this now this convert image we
are going to call over here so here we'll Say convert image and simply pass the URL okay so this URL we are going to pass it in order to convert it cool correct now next uh if you want to know how I uh how we are going to use it right you can just search for the Firebase uh document for storage okay and it will redirect you to the Firebase documentation here you will learn how to uh store the data upload the files and everything okay and this is the best documentation I find on the
internet and It's very easy to use but let's write it one by one now once you're done with this one right let me bring this down here next thing you need to define the image reference okay so is equal to we'll write reference from the Firebase fire storage then storage and here we need to write a file name okay okay right now we don't have file name so let's create one so simply we constant we'll write file name is equal to and date do now so I'm I'm Using the timestamp okay so that we'll get
the unique file name every time and I will attach the extension as a PNG okay now another important thing if I go to this Firebase storage and search for the storage option now uh if you using this first time you have have to make sure the go to the rule section and make sure to add this future date so you can access it okay without any permissions next thing you need to create a new folder and here We'll say AI story for example okay now I added this new folder called AI story and if you
want to store all your images inside this AI story folder you have to provide the ref file name in that way so here I will add AI story SL Dash and then that date uh the file name okay and this is our file name and this file name we pass to this storage uh so that we'll get the reference I'll make sure to import this storage from our Firebase config okay Now we have the image reference now with the help of this image reference we will upload our file as we have the base 64 image
we need to upload this file using this upload string okay now here we'll write image reference and converted image oh sorry we have the base 64 image and uh this particular line of code you will find over here okay see uh not here this one uploaded from string okay so if you have Bas 64 this Is the line of code used to upload your file now once you have the base 64 image uh pass that b 64 image to this upload string and here also you need to pass a data URL dot then and here
we'll write snapshot like this okay and inside that we'll simply addite console.log file upload it okay so this is nothing but the this Line of code which we use it perfect right here uh even though you pass base 64 I think that also work okay now once you pass this one right next thing you can get the download URL this is nothing but the URL uh which from which you can access your image so here we'll say await to get the download URL you need to write get download URL and you need to pass the
image reference which you uh just mention above right and here I will write console.log Download URL I will just add that download URL and make sure to import this get download URL as well from the Firebase storage and once it's everything is done you need to just return the next response from the next server do Json inside here I will add image URL which is uh gener uh added to the Firebase and here we'll add the download URL as a response perfect right so this is the complete code in order to store our um The
AI generated image to our Firebase let's save it and let's test this out so now let's go to the create story page. TSX file and here we have this image URL now what I will do I will just add constant image response or image result I will say in this case is equal to await exos do Post request and here we'll add this API slash uh save image okay this is our Call and inside that you need to pass the URL the URL is nothing but the this image URL which is generated from the AI
so here we'll say AI image URL which is quite meaningful and then here I will write console.log AI image URL okay now this AI image URL right now we are just consoling that we need to console the data and then save it this need to be like this image result. data okay and Save it now let's go back to our application and go back over here and click on create story now over here I will just add the Harry Potter's kid story format so I will select the story type as a book 5 to 8
years you can select the image Tyle let's select this pixel style and simply click generate story before that I will just open the inspect panel inside the console we'll see whether we are getting the image or not okay the Image URL of the uploaded image in the Firebase storage so we'll wait and our image is getting generated and boom if you see we got the image URL okay I will copy this image URL obviously and I will paste it inside the browser um just I will make sure this code is not there and enter it
and boom if you see we got the image uh which is stored in the Firebase and if I go to this Firebase storage and if I refresh this one you will see that Image is get stored perfect right now this image we are going to or this image URL actually we are going to store in our database so uh once we have this downloaded URL right and if you see the format of the result and inside that we have the image URL uh let's go to the page. TSX and that's where we have this image
result right and from that one we'll get the image URL so here we'll Say constant Firebase image URL is equal to and this image URL perfect and I will just uncommit this code now and inside this one we are going to pass Firebase store image URL okay now once we pass this one inside our save in DB make sure to add this uh or pass this image URL accept that as a string format and then pass it to this cover image image URL and save it okay let's test it all this together So now I
will select the subject story bed Story 3 to 5 years old and I will select the paper cut click or let's say watercolor let's see how it looks and simply click generate story now as soon as it generate it's supposed to redirect it to the uh new page I hope uh the logic that logic we added but will wait and boom if you see we redirect it to the uh view story page with an ID and I added this uh toast message that I'm going to tell you in the next chapter How to do that
and now if I go to our database Let me refresh this database and over here we'll find the Firebase image URL and I can open that one boom as we select the water color is not giving giving a best quality but it's quite good right and this is our result perfect so guys that's how uh you can uh generate the AI image then convert it into B 64 so that you can store it in our own server okay that's cool so uh guys if you have any question any doubt Let me know in the comment
section now once we added all the information like uh images story output and all of these things the most important part we need to add is the who added this story the username email and the um user image if you want okay and right now inside our database we don't have that column so let's fix that one so first thing let's go to this schema okay and inside the schema We'll add couple of more columns so first thing is User email who added this particular story right so this will be a whereare here I will
add um user email okay then comma over here then we want to add username Ware user name and the last is user image again this is a Ware here we'll say user image okay so this is very important and save it now Make sure to stop your studio and then we'll run DB push command so it will update your uh table so here we will say execute all the statements the changes are applied now you can run the studio again and if I refresh the screen you will see the three new column get added now
inside your uh create story where we are adding or inserting that uh this story information and all here you'll add user email and now in order to get the user email You have the clerk uh sign authenticated service right so let me okay yeah let me get that one so here we'll say constant user is equal to use user now this use user hook you have to import from the clerk nextjs make sure to import from there okay and if I scroll down now here we'll simply add user. primary email address. email address then here
we'll add user image as a user do primary not this is just image do image URL and then we'll add username as user Do full name and that's all and save it now whenever you insert a new story it will also insert the user information who added this story because this is very helpful when you want to show this uh all the stories belongs to that particular user on the dashboard now let's quickly implement the error hand link now whenever we CAU an error uh while passing the Json request or some other error we have
want to show the toast message that okay Let's try again we have some error right and for that one we are going to implement a toaster the next UI currently not providing the toaster component but in future they are planning but uh for that one currently we are going to use a react toasttify Library okay very easy and simple to use and very modern so first thing we'll install this uh react toasttify library then if you scroll down uh this is the simple use case that how you can Use toaster first thing you need to
add this toaster container now first thing uh we need to import this one okay so first let's import this toaster container so let's go to this provider. TSX file and just below this children We'll add this toaster container make sure to import this toaster container and then make sure to import this CSS as well this is very important okay and save it now in order to display it you will call this Notify which is a kind of toast message and let go over here create story I will just add this notify and uh whatever the
message will pass we'll pass this message to this method okay obviously it is of type string and then make sure to import this toast as well okay now whenever you want to call you can just call this notify and it will show this message um I think that's all so let's try this out so let's let's do one thing whenever we Select something right we'll notify it and we'll say uh Choice selected okay some kind of message just want to show whether the toast message is displaying or not so if I click on this one
see the choice selected right cool right if I select this one you'll get the second option that's how the to message will look like now obviously you can uh explore this one if I go to this documentation and what are type of uh to message you want To show you can show uh the toast message okay so if you want to show the toast message with the background color you can add that one as well now in order to show the error message right you and say notify error and here you need to add toast
do error and then you can show this message okay so whenever uh we got error inside the catch we can show server Error try again okay perfect and uh I will remove this notify because we don't want this anymore and then uh after success we can just show not story generated okay and save it perfect now let's test this out either we'll get error or we'll get that success message here I will write uh some subject here we say princess and frog bed story uh you can select the age group and the style of the
image and simply click generate Story now once it generated right uh successfully then you'll get the success to message otherwise you'll get the error tost message so we'll wait and if you see we got the success message saying story generated and make sure inside your database that record new record get added with an image see perfect right so that's how guys um uh you can add the uh error handling functionality let me talk about the billb Doai where you can create and winning content with the help of AI you can even generate the all the
social media post like you can generate Instagram post Twitter post you can generate the YouTube script or you can even generate the advertise for your business billb is very powerful AI based tool if you want to join the B bus you can find the link in the description and you can get a 25% off on the current pricing let me walk through how it works Once you log in you will find this dashboard where you will find all the options where you can generate the content with the help of AI let me show you one
example let's say you want to create an Instagram post so you can select the type then you can select the product let's say you want to generate the Instagram post for the fitness here you can write the context so once you fill all the details you can simply click on generate Instagram post and Within a few seconds it will generate the complete and uh SEO base or SEO keyword base uh post for you and if you see on the right hand side you will see how it actually look on the your Instagram post here it
will also generate the AI based image for you so you don't need to bring or get any images this bill buz will take care of generating the AI image as well and he will find the content along with the hashtag for your Instagram post how cool It is right and not only that but if you come back come back later on this billb you can find your post which you generated previously like this okay see so guys uh without doing any further delay if you want to join the bill bus the link is in the
description also you will get a 25% off on this particular product so just join it now on The View story page we are going to display the story title uh with a big uh font and then we are going to add the flip book Kind of feature okay where user click on that and automatically it open the page similar like a blue book okay and to implement ment this particular functionality or the UI we are going to use this react page flip library and if you see this is how it will look like how cool
right so similar way we are going to implement in our application so first thing we need to install this dependency copy this npm command go to your project I will close all of this Tab and inside the terminal just paste it here so it will install this page flip next thing uh let's go to this view story inside that we have page. jsx file and most important thing we need to get the parameter value the ID of the story so in order to get it you need to write params like this and obviously we need
to provide the type of the parameter let's define any for now outside of this one okay and Then inside the use effect let's check what we are getting so here we'll write inside the use effect so the empty value and whenever this component get load this use effect will get execute I will just console it using params do ID okay let's save it and just verify that we are getting this particular ID as we are using use effect we have to make sure this component is on on the client side now go to the inspect
panel and and Go to the console and if you see we got this ID which is same what we pass it perfect that's what we want right next thing with the with the help of this ID we need to fetch the record or story which is added to our database okay now one more important thing we need to do over here is to add the two new column okay cut here okay let's go back and here we'll write constant Get story and whatever story we pass so here will constant result equal to await DB do
select do from and inside that we need to pass a story data as our schema name do where and inside the where condition we'll pass equal to so story data dot Story ID which matches with the parameter do ID okay and only that data we are fing here we make this a Sync perfect and I will just conso this using the result of first element and now this get story we are going to call inside the use effect let's save it and let's test this out so if I go back to our application and refresh
this screen once and boom if you see we got the result cool right now this result we are going to store in in one state so here we'll say constant Story comma set story is equal to Y St and simply I will set the story using result of zero okay now obviously you need to mention this is of type any for now or you can Define the type okay so that you need to add all of these fields but right now I will keep as it is okay and save it now next thing we need
to display uh the name of the title for this parent du I will add a class name we add padding of 10 padding X to 20 20 when the screen size Is medium and for large screen We'll add padding X to 40 then inside the h2 tag we'll simply add story dot output dot story let's see what is the name of the story how it structurize so inside the output we have the story cover and inside that we have the title of the story okay so similar way you need to Define over here do title
of the story perfect and save it let's see whether we get the Title and boom if you see we got the title now for this one let's add a class last name font bold text uh let make it for Xcel we'll make text to be in the center I will add a padding to 10 because I want to give background to a primary and text to White and save it now if I go back this is how beautiful it will look like perfect right now next thing we need to do is to add this uh
page flip component Right and if you scroll down this is the way that you can add it so simply copy this all of this line of code and then simply paste over here okay so make sure to import this HTML flip book so if you import it it's supposed to import automatically let's import manually if it's not importing perfect and now we have this flip book okay so here we just write at the rate TS ignore okay typescript ignore and so it will Ignore this and save it right now if I go back to our
application you will see nothing on the screen say but you will see this page effect obviously we need to add some styling to that one but over here first thing I will remove everything okay and then we need to add the cover uh book cover image first so let's create a component I will say underscore components over here and inside that here we'll say book cover page. TSX add A default template and here I will say image URL only is of type string so obviously you need to Define define the type uh differently let's add
any for now and uh over here I will add this book cover page now to this book cover page we need to pass the cover image okay so if I go over here and this is our cover image that we need to pass it so simply here we'll add image URL is equal to cover Story do ccore image let me check oh so it's just like cover image something like this okay and in inside the book cover image we have the D here simply add a image tag then Source tag and here I will add
alt tag as a cover inside the image source tag We'll add the image URL and we'll pass the width as 500 height of 500 okay which will match with this one I will make this 500 for now and height with of 500 let's save it and Let's test this out so right now uh we have error that it's saying the Firebase Google API this particular domain is not configured under this next config so let's go to that inside this N.C config MGS file here I will add the images under that you have to add the
domain which you want to Whit list so in this case we want to Whit list this Firebase storage Google API because this is the domain for our Image and just restart your application and refresh the screen and boom if you see now uh it show showing the image of the our of our book perfect right now next let me go back and to this HTML book cover here we can add some property right you can say show cover as a true okay and uh let's add a class name I will add a margin top to
10 and save it okay make sure perfect and that's our book cover right now it's only one page okay but if you add multiple Pages then It will work beautifully if I try to paste this multiple times see okay and you will see the result so let me refresh this once before that make sure to wrap this inside the D okay because um for each of the page we have to make sure that each of the content is wrapped inside the de so I just copy pasted and if you see if I click on this
one boom see how cool right so that's how it will look like it will automatically open a page so something like this right right Now we just copy paste the same image same component okay but just I I I wanted to show how actually it look like okay next thing um I will add a new component in the component folder we say story pages. TSX file add a default template and then save it now over here um this particular Library cannot load the dynamic pages okay you have to create the pages before so here I
will add dot dot dot array okay and then I will use okay let me first wrap it Inside the curly braces and then we'll add this array okay now this array length will be the uh length of our story dot let me see the go to the inspect panel console okay so inside the object we have this output and then chapters okay so here we say outputs dot chapters okay dot l so that much Pages we want to create Okay so when we done with this one here we'll map it and inside that we'll add
a dip perfect now inside the DU let's add h2 tag and here we'll simply say page okay so I think we incorrectly map this one so inside that we'll say item comma index and the arrow function and then we'll add a d okay now simply I will add the h2 tag and I will just print the index okay and then save it let's test this out how it looks like so okay we Have one error that node off to be removed if not child node let's see what's the issue so here let's add key as
a index and save it and now we don't have error but if you see now we have page numbers right 1 0 2 three and four five perfect that's look weird now I understand but let's do one thing so here we'll add a class name We'll add a background to be white padding to20 and we'll also add a border and inside that we we are going to render the Components okay called story Pages now inside the story Pages we are going to pass each of the story chapter uh title and the description right so here
we'll say story chapter is equal to and simply we'll say story. output dot chapters and then we are going to pass the chapter depends on the index size something like this okay and this story chapter let's go over here and we pass to this story chapter Here you can Define the uh type as well for now I will mark it as a any okay because it will take some time to Define each of the type and now we have the story chapter okay perfect now let's go to the story pages and inside this one first
thing inside the do we'll add a S2 tag here we'll say Story chapter dot let see the chapter title so from the output we'll get the chapter title okay and P tag for the Chapter uh actual content right so here is story chap story chapter do chapter text now to this S2 tag We'll add a class name we'll say text is of 2 Exel font bold text primary okay and for this P tag we'll add a class name um we'll add a text is of excel we'll add a padding of 10 margin top to three
and then we'll add a rounded corner to LG we add background slate to uh 100 I think that's pretty good and then save it perfect let's see How it looks on the screen and boom if you see this is how content will look like cool right see now this is book if I open this one this is a hard cover so that's how it look like but now on the pages we have each of the page and if you see this animation how beautiful it look like right now even though you can just uh uh
open this page through the mouse or you can just click like this see and then the book end now obviously we need to add the last page of our uh Book okay so inside the component We'll add uh last page. TSX file add a default template and inside the page. TSX after this one we'll add that inside the DU here we'll say last page okay now inside this last page first thing we'll add a class name and we'll add a background to be primary and we'll add a padding to 10 here we'll add h2 tag
we'll say n of Story but this we'll say in we'll add a class name text to be in the center text to excel text white okay now here I will add a du and button share the story okay those Share story share button here will add a class name Flex item to be in the center justify in the center and save it and let's see how it looks like so we have again new error that's pretty weird Let me refresh this Screen okay so after refresh it working fine I just refresh it and this is
how our page will look like let's try to update this one so over here and add a okay not inside the last page let's add height full and let's see how it looks I think that's pretty good perfect and Let me refresh it again so there is some issue uh with the last page which we are adding okay but as soon as I got some solution I will fix this issue don't worry okay but right Now if you see we have the proper solution and proper uh book to read it cool right so that's how
guys you can add uh this react page flip library and give a beautiful effect to your story now we are going to add an audio for each of the chapter and in order to dis uh add an audio uh which speak act the chapter's content okay so here uh to this line I'm going to add an button which will uh start speaking whatever the chapters contain is okay and and for That one obviously uh we need to add a button and on click functionality and whenever we click it right if you see it's automatically flip
the page so first thing we need to stop the flipping page functionality so if I go to this page. uh TSX p uh TS file inside here if I go to the HTML flip book I will just add um use mouse event is equal to false okay which will avoid or which will on the click of that right you can I think Let me refresh this once and now I'm not able to flip it even though if I click on this one okay perfect next thing we need to add two buttons which will act like
a next and previous page so in order to add it uh let's go to this bottom section here I will add a du inside D let's add a button okay and this button let's say next for now and here I will add a class name we'll add a absolute position and we'll make right to zero and then save It now if I refresh this screen you will see this button okay so we have another error this is quite weird okay let me go back and make sure to add this button outside of this flip book
like this okay now if you see this button is coming to the next but we want near to this particular page so what you can do you can just wrap this de inside the relative okay and save it and let me refresh this once it's still on the right Side I think that's not working out let's do one thing let's me wrap this I table book in one de like this and inside that we'll add that one and for this du let's add a class name and let's try to apply it as a relative and
let's see if I refresh it if you see the button is come just below this uh page but now we mark this as absolute so let's bring in the middle so what we can Do uh we can add the top to 250 pixel because uh the our height is 500 pixel and in the middle it's nothing but a 250 and if you see now it's coming in the middle we want this button on the right hand side so simply uh I will add minus and here we'll add let's say 10 okay so it will be
like this and obviously we want to change this button to uh some icon okay and for that one we are going to use react icons uh if you did not install make sure to install This react icon it's contain a lot of different icons okay just make sure to install it so that we can use that one and just search for the but uh button called arrow and we want this kind of Arrow so I will copy this import statement first in order to use this icon and paste it here and next you need to
add this component okay so obviously if you remove this button that also fine and then we add added this button if I go Back to our application now you will see this button it's quite small but let's add a class name We'll add a text Excel and let's see how it looks now let's make it 4 XEL yeah that's pretty good right and even though here if I save five that will be good perfect okay you can change this color as well and uh if you want to increase the size let's say 60 pixel you
can add like this as well and I will make this as a text primary See Perfect Right make it 40 only perfect and now uh we'll make it cursor pointer so when I H on this one it will look like you are pring some button okay now same thing you you need to add it but for the previous one so I will add this button obviously uh this right Circle I will add left and make sure to import this as well make sure that Circle that particular arrow is there I hopefully it's there but that's
fine and make sure to change this left To right or right to left see perfect right now we want to show this previous or this left side previous button only when uh user is uh flipping the book first US flip the first page okay now first thing uh on the click of this on the click let's add on click method and inside this we'll just add the uh flip functionality so for that one let's define one reference so here we'll say constant ref uh say book reference is equal to use reference oops My bad reference
like this okay and then this book reference you need to attach over here so here we will say reference as a book reference so that you can handle it on the click of that one so here we'll say book reference dot current and it has a method called page flip okay and on this click of next we want to go to the next page so here we'll have a method called flip next and that's all and save it now let's test This out if I click on this one boom if you see page is flipping
right um also make make sure uh not to okay let me remove this Flex because I added for testing purpose and now I think that's good perfect Let me refresh the screen now we have the book and if I click next it's showing boom see perfect right now next add previous one so similar way we can do that so I will just copy this line and for this du as Well I will add an onclick method and I will add this one but here instead of next here we'll say previous okay now if I refresh
this screen if I click next and if I click previous see perfect and now we want to show this previous button only when user flip the page so here uh let's define a count or here we say constant we'll say count comma set count which Will hold how many pages you flip it okay so initially the count value is zero and whenever we click next button right so here we'll add a count set count as count + 1 and for the previous button We'll add count minus one okay and here we can write a condition
if count is not equal to zero then show this uh don't show this button okay it means uh this is the arrow Okay over here perfect so uh whenever count is zero then don't show the previous button and now if I go back to our application and if you see the previous button is not showing anymore but if I click next then previous button is showing right but if I click previous the button is gone same thing we need to do for the next page whenever the there is a last page we just make sure
that uh it will not show that button okay so over here we can write a Condition that if count is not equal to and then we need to get the length of the chapters it's not equal to length of the chapter so something like this dot length minus 1 and and operator and just make sure to wrap this one I will just format this one okay and save it now if I go back if I click next next next and next and boom see perfect right so that's how guys it will work so uh once
we done with this one the most important Thing we want to implement is the play button over here okay let's add that one so if I go to this story Pages where we have this title here I'm going to add a pan tag and inside that I'm going to add a play button icon so let's search over here let's say play and let's use this one so I will copy this import statement first and then we'll copy this component and we'll paste it here okay Now here make sure to mark it as a flex and
justify between so it will be on the right hand side and if I go back let's refresh this screen because whenever you make a change right this HTML flip page is not update in the real time now if I click next you will see this button cool right now to this button if I want to increase the size you can increase this size as well and I will make this cursor pointer and let's add on click event to this one here We'll say play speech I don't know what we'll call but let's say Place speech
and we'll add constant play speech like this now to this play speech uh I'm going to pass the chapter text I know that we already have but we'll pass this one and here we'll say text which you want user or uh we want to make sure to speak it and like this and save it okay now we are using Inu uh browser functionality to uh speak so here we'll say constant VI or not wi but obviously if you want to choose specific wi you can get the wi but here we simply say constant synth is
equal to window do speech synthesis okay and then here we say constant uh text to speech is equal to new speech synthesis utterance this one and then pass the text which you want to make sure to talk and the last is you have to call syn dosp and then you need to pass Text to speech uh constant over here and that's all and then save it now let's take this out I will refresh the screen once and go back to the next page and click this one once upon a time there was a nam Max
who to play Super or it will start talking okay now you have option to update this voice as well because this wice is different for a different browser I'm on Mac OS is quite different but on the Windows is quite good okay so for now I will keep as it Is now let's implement the dashboard so once you click on the dashboard uh button which we have over here on the header and then on that one we are going to show this header called my story and we are going to show the number of credit
left uh to generate the story right each credit belong to one story that which user can create and then we are going to display the list of the storage which user created okay so it's quite easy and quite simple UI that we Are going to make it uh add it and obviously for that one we are going to use the next UI uh card component but before that uh let's add this dashboard header uh with showing this my story and the credit how many credits left okay so let's go back to our application and inside
here we have this dashboard page okay and if I go back to our application and click on this dashboard page it will navigate to the dashboard okay next here uh we'll add a new folder called Underscore components and all the components which is related to the dashboard we keep in this folder so here we'll say dashboard uh header. TSX add the default template and then make sure to import that over here so here we'll say dashboard header now for this main du I will add a padding of 10 when the my um screen is medium
we add padding X to 20 and on last screen We'll add padding X to 40 okay now inside the dashboard Header for now I will just simply add S2 tag and we say my stories and along with that one I'm going to add a span tag and here I'm going to show the image and all okay or there are another way we can do it uh simply We'll add a du tag and inside D I will add a image tag the source and inside this I have coin.png file okay so I already added inside my
public folder where the coin will look like This obviously you can get this all the assets from the source code as well uh the link is in the description and then we'll add a width of 50 height of 50 and over here I will just simply write in Span tag maybe let's write uh three credit left okay and for this h2 tag We'll add a class name font bold text is of 3 Exel and save it now for this du We'll add a class name We'll add a padding of seven then we'll add a background
to Primary and for this one let's make text white over here and save it and this is how it will look like now everything bring in one line and let's put this credit information on the right hand side so simp simply I will make this flex and we'll make justify between and item to be in the center something like this okay now let's bring this in one line these two coins and the text so here we'll add a class name make make it flex and uh we'll give gap of three for This particular span I
will add a class name text is of 2 XEL and save it let's make item to be in the center something like this see perfect right now below that we want to show the all the uh story list which is generated by this current user also for this background uh for this page. TSX I will add a uh minimum height to the screen okay so it will be full screen Perfect next uh we'll create a new component inside the component folder and we'll call it as a uh user story list. TSX file add a default
template and save it now this particular component we need to add over here so we'll say story sorry user story list and save perfect now before uh showing this component we need to get the user story so here we will say get user Story okay and here we say constant result is equal to await DB instance. select obviously we are using result query from and here uh we need to pass the uh the schema which is story data here we will add a we clots equal to story data so say story data dot the user
email and we need to get the user login user email so here we simply say constant user is equal to use User and obviously here I will make use client so this need to be actually use user from the clerk okay then make this as a sync and over here we'll simply pass the user email perfect so users. primary email address. email address from that you can get the user email make sure to import this you equal to sign uh we have one error so this is user email and this is the email address this
look good to me so typescript we considering okay Might equal to sorry this uh primary email address might be a null so here I will add option if it's uh there it's not there then it will show empty and save it now for now I will just console the result and inside the use effect will call this method so whenever this component get load this use effect will get call and then the sorry the user effect will get executed and we going to call this get user story now we have to make sure whenever the
user information Is available then only call this method let's save it and let's check check it out so inside the inspect panel if I go to the console and boom if you see we got the three stories perfect right so this is what we wanted now we need to save in one state before that I will Define a type so here will say type story item type is equal to and obviously we need to provide the type For each of them okay so we'll just copy here so it will be easy for me to write
it I think oh that's very big mess but here let's say ID is of type number then story type is of type string I will pause the video and I will write all of this type so I Define this all the type over here and now simply I'm going to uh Define a state constant store story list comma set story list is equal to use State and this use state is of type Story item type something like this okay oh this need to be here perfect let's save it and obviously this is the list so
I will put the story item type of list now uh over here we'll set the story list as a result and here for now I'll put the type any okay and save it now over here we are going to iterate this list of story so here we'll add a story list Dot And we'll add and and operator and story list so if story list is there then only show and map here we'll put item comma index index is your type number and item is of type story item type perfect then the arrow function and inside
that we'll display the D now instead of displaying a writing the code for the uh card here I'm going to create a new component and we can Call it as a story item card. TSX add a default template and simply use that over here so story item card like this now to the story item card we are going to pass the each of the story as a item and inside the story item card we are going to acccept here as a story okay now for that one we need to provide the type okay so we
cannot provide the type as a story item card so Simply what we need to do let's copy this code all of all of the code and I will simply paste it here but in this case I will wrap inside the curly bra say will say story perfect right and now you can mention story item type perfect next uh once we get this story information here I uh as I told you in the beginning that we are going to use this next UI card component so if I go a little bit down here you will see
this component called card uh obviously let's Install this component first and then if you scroll down little bit uh you need to import this statement as well and then you can uh use any card which you like okay now if you scroll down you will see a lot of example but I will use this particular example so go to this code and simply search for the second last card component so I will use copy this One and simply let's bring this down and let's paste inside this story item card like this okay I think we
by Mally imported inside the wrong component I will copy it from here and paste it here and make sure to import this image component as well then remove this wrapper and make sure to import this button as well perfect okay now instead of this image I'm going to show our Story cover image so here we say story. cover image save it and if I go back to our application let's say whether we are getting or not so right now we got error that image with this source is missing this width property so let's give the
width property for the image so here we say width is of 500 and height is of 500 and now we'll wait and boom if you see we got the card but obviously it's not looking good let's update the layout Little bit so if I go to this user story list where we are mapping the story right for this de We'll add a class name here we'll say grid grid column one when the screen size is smaller on medium screen size we'll say grid column two and on large we'll say grid column three and for extra
large we'll say grid column four now this particular colum depends on the grid size is very helpful when you are developing the responsive uh web page now let's give a Gap of 10 and now if I save it this is how it will look like cool right uh let's give some margin top to 10 okay to the usual story and if I go to this story item card over here I don't want this header so I will remove this header and then this is how it will look like Perfect all right now most important thing
we need to update this particular uh text okay and let's update it also we need to order this by the latest one as a first uh story and then The old one will be the last kind of thing okay but let's update the name first so over here I will add the story dot output and inside that if you see our response so inside that we have Story cover and inside that we have the title so here we say story cover. title okay and if I save it you will see that it's showing the name
of the story now obviously I'm going going to remove this P tag and over here I'm going to update The font so here we simply say text is of to Xcel and save it perfect right I think let make it Excel only and let's try to make it white and let's see how it looks I think black is much better perfect and now if you see we have this beautiful uh name of the story also uh we'll change this button to read now and uh save it let's see how it looks perfect now you have
two option either You can on H you can add an effect so in order to add a effect to this card inside the class name you can just add hover and you can make it scale to 105 and for smooth animation you can make it transition all so whenever I hover on this one you will see this beautiful effect also make this as a cursor pointer so whenever I hover on this one you will see the pointer get changed see right perfect and that's how our uh uh Dashboard story list card is ready now on
the click of this one obviously you want to navigate it uh to the view story option so you need to add an onclick event over here or simply you can wrap this du inside the link tag to this link tag you have to provide the H reference and then you need to add the path so in this case is view story slash and then Story ID so story. Story ID something like this okay and obviously here also I'll put it in the link I will wrap It in the link and boom now if I want
to read this story just click on it and boom now if I click over here everything is working like a charm right perfect if I go back again I will jump to the dashboard now one more and last important thing over here in the dashboard right whenever the data is getting load inside the user story list because we are fetching from the server right you can show some kind of loading effect so simply I will Define one state Called constant loading comma set loading is equal to use State either you can use the skeleton effect
or you can use our existing loader right so it's up to you how what you want to use we already have the loader so we can use that loader so simply first thing you need to do is to implement the loader over here so we'll add a custom loader and obviously this custom loader we are going to show when the loading is true and once it's not We'll show it as a false okay but for the testing purpose I'm making this loading as a true just want to check whether we are able to see the
loader or not okay okay right now I don't think so we are able to see this loader let's see the issue so by mystically we we we need to pass the let's go to this custom loader first so this is the custom loader and we need to pass is loading as option and here we'll pass the loading Let's see now whether we are getting a you see we are able to see it perfect now obviously I will just make this as a false and then whenever we are fetching the result we'll set the loading as
a true and once is finished we'll set the loading as a false and save it and if I refresh it you will see it loading is showing and immediately we are getting the result because we don't have that much result showing on the screen now in order to uh Show or order it by the ID here inside the query we'll just add order by then we'll say which order like say let's say descending and then you have to add on which field so Story data. ID because ID is incremental right and then save it now
obviously make sure to import this uh descending from the dzal orm save it and let's go back and boom if you see now this is showing as a first element perfect so that's how guys you can show The dashboard next thing and very important we going to learn how can we implement this credit functionality so that user can use it uh or generate the story depends on the credit if the credit is over obviously user need to buy more credits now we are going to implement this credit uh functionality where um depends on the user
generation of the story that credit going to use it if user have zero credit then user cannot Create any new story so let me walk through how it works first thing whenever user log in we are going to check if user is already exist into our database if yes then we are going to update the context the context is very helpful uh so that we can share uh the user detail information throughout the application you don't need to pass component by component and that's the reason we are going to use context but if user is
new And uh he's not uh um that it is not in our database so in that case we need to add that user to our database and then we are going to update the context okay so it's very straightforward functionality so first thing we need to create a new table called users so let's go to this schema so inside this config folder we are going to the schema and here we are going to add a new schema so here we say export constant users is equal To PG table and the name of the table is
users and inside that uh we'll add us ID which is the primary key then username so this is a Ware as username as a column then user email again this is a Ware as user email um you can add the user image if you want let's keep that one as well so that is also whereare is a just a URL and most important the credit okay so credit is of type integer make sure to import it uh from this PG core and give the column name as a credit and we are going to add default
value to three okay so user can create a three story by default but perfect and save it now let's stop the studio so inside the terminal I will stop the studio and here simply We Run The npm Run DB push command so it will update our database so here you will say I want to execute all the statement and then now you can simply run the studio and we'll refresh the studio and we'll see whether the new table is get added or not so if you see inside the users we have the new tables with
the all the column which we added into our SK schema perfect now let me close all of this tab and let's go to this provider now inside the provider we Define this clerk Provider right I will move this clerk Provider from uh provider component to the layout component okay so let's go to this layout and just wrap this provider over here okay make sure to UT it and obviously nothing will get change into your application next thing we'll Define one state called user detail comma set user detail is equal to use State okay so this
is our uh state Where we are going to save our users information now here I'm going to call new method called save new user if not exist now inside this method first we'll check if user is already exist or not okay and then if not will'll add new user to database okay perfect so these two uh function the uh DB call we need to add it so first thing we'll check if user is already exist or not so here we'll say user is equal to Await DB do select fromom and here we need to provide
the user schema here we add we Clause equal to and inside that we need to check if users do email or user email is matches with the current user email so obviously we need to add the user use user Hook from the clerk and here we'll simply check that so here we'll say User so use user hook we added from the clerk that's good I will remove this one and here we will say user dot I don't know why it's not getting the suggestion so let's write manually so here we'll say user dot primary email
address do email address so basically we need to I don't know why it's not okay so so basically uh we need to change this name user response something like this and Then we'll get the users just add a question mark here like this and we'll add a condition if email address is not there we'll just add the empty value and here I will make this as a sync perfect right and then we will get the response so here we say console.log user the response okay now if the response is there we'll simply set the user
detail as a or let's let's not uh do it over here okay here now we'll add uh if call so if User response oops user response of zero if it's not there means if you user is not exist then we'll write a new method so this is the comment for that one so here we'll say constant result is equal to await db. insert obviously you need to provide the schema name which is user do values and inside that we need to provide all column name so first is user email that We can get it from
the email address then user image as user do image URL and then user name as user dot full name okay so this three value we added obviously we are adding the credit by default which is three so we don't need to mention it again and then once it successfully inserted we are retaining some value so first is we'll say user image as a user Dot user email I will just copy paste this couple of times and this will be username as usern name then user image as user image and the last we want also want
createit so that also we are going to get it from the users do credit okay and save it now once we got the we get this result we'll simply set it to the uh uh to the state called set user Detail because that's what we Define at the top right here uh for now I will make the type any and that's all perfect right and if user is already exist so obviously it will go to the else call and inside the else we'll simply set the set user detail as you user response of zero perfect
right so that's how we are going to Define it now this particular method called we need to do it inside the user effect so inside the user effect uh whenever the users is Available we are going to make this call okay obviously if user is not loging then obviously this will not get executed only if the user is loging to our application now let's save it and uh once we we have this user detail we obviously need to share this across the application but before that let's uh console this whether we are getting the data
inserted into our database or not okay and here I will say new User and here we'll say existing user okay and save it now let's go back to our application and inside the inspect panel go to the console and refresh the screen so we got error inside the provider obviously inside the provider we need to mark it as a client and let's refresh the screen and boom if you see now it's saying the new user okay because obviously uh first time we are in Setting that's why it's showing new user and this value is getting
return it means uh the data is inserted successfully if I refresh this screen for the user if you see we have all the details right now if I refresh this screen again you will see now it it might saying the user uh existing user see because that user is already there right and obviously it will not insert the new record again to our database perfect it means it's working Now in order to share it across the application we need to use the context so context is very easy to use uh let first thing uh let's
go to this app folder and I'm going to create a new folder called undor context and inside that we'll create user detail context. TSX now inside here we simply say export constant user detail context you can give the same name as as a file name okay is equal to create context and make Sure to input it from the react like this and here uh you can Define type any and pass as a null for now and save it now inside the provider you need to wrap your uh complete component inside this context so that you
can share this context user detail value throughout the application so simply here I will say user detail context do provider and and then you need to pass the value which is the user detail comma set user detail close this Tag and make sure to wrap it something like this and save it okay now obviously whenever you load the page or whatever right the user it have some value and that value you are passing to this context provider and wherever you want to use you can just use it let's go to the dashboard and inside the
dashboard uh header we want to use it right I will make this component first as a client side then here in order to use the contact First need to wrap it in A curly braces here you need to give user detail comma set user detail is equal to use context and inside that you need to provide the context name so in this case is user detail context right and close the tag perfect now you have the user detail in information if you want to print you can just print it out but here if you want
to replace with the user detail sorry user detail Dot credit that's all and save it Now if I go back to our application and refresh the screen you will see it's showing the three credit now if I change this credit to let's say five and save one change and now if I refresh it you will see the f as a credit perfect right so that's how uh you need to um maintain the users information along with their credit now next obviously whenever user create the new story right uh on the click of gener story and
when the story successfully Generated we have to make sure that we will uh do the current credit minus one okay because we want to uh use one credit from here so in this case if user have five credit and user generated new story then credit it will be four okay so that logic we need to write now now below to this button I'm going to write one simple message so let me close all of this tab and let's go to this create story components where we have okay inside the page. TSX we have this button
And just below this button I'm going to write in a span tag we'll say uh one credit use will use okay and save it and let's say whether where it's showing that so it's showing right now in one line but let make it Flex colum and and here we'll say item to end okay so it will be at the end see something like this Perfect and uh once user click generate story we need to make sure we'll deduct the that particular credit right so uh we already have lot of logic inside this page. uh TSX
file so first thing we need to get the context over here so we'll say constant user detail comma set user detail is equal to use context and then we'll use the user detail context now um once this uh image is generated we are inserting the record I believe yeah and then we are going to Make a call so here we'll say constant or we'll create new method here we create a new method actually so so below this one we'll say constant update user credits is equal to a sync and the arrow function like this and
here we say constant result is equal to await DB dot update and we need to update the user schema here we'll say Set and inside that we need to update the credit right so now credit will be the user details credit minus one okay and we have to make sure this will be a number I will just wrap it like this and OB we need to add a we class where equal to user. primary email address do email address will match with the users dot user email okay here I will just put it on the
right hand side so It's it easy to read otherwise empty perfect and this particular method now we are going to to call inside this save in DB or we can call it just after the storage we are notifying it right so over here also we can call this method so update user credit here we'll say await and that's all and simply uh we can also write dot returning and here we'll just return the ID of updated story so we'll say id Users. id okay perfect and let's save it and let's test this out so let
me go back to our application now here just write uh call with magic book as a subject story you can select the any story type the age group and the image style now right now if you see we have the five credit okay and now as soon as I generate the story and once the story generated successfully we you will see the story will uh the story count will update Right now we have one error let's let's refresh it once and let's try it again so girl with magical book okay and now we'll select the
story type and G story it's still working and now if you see the story is generated and boom we have this beautiful cover with the book name where we have all of this information now if I go to this credit section and refresh it you will see now we have four credits perfect now Let's go to the dashboard and that there you will see you left with the four credits perfect right now we have to make sure if user don't have credit user cannot create the uh new story so let's update that so if I
go back to our application and over here we'll check before uh generating The Story So inside the gener story component right here we'll first check if user detail Dot Credit is less than equal to zero so it's need to be a credit then we'll simply use the tost message to notify and here we'll say notify dot I uh let's say with we have notify error so we use notify error and here we'll say you don't have enough credit okay and that's all and save it okay now and make sure You will return nothing okay otherwise
it will go to the next page but if you check over here then obviously uh it will show the error and save it now let's test this scenario so first thing I will go to the studio and I will make this creit to zero okay let's save change and obviously if I go to the dashboard you will see now you have zero cre if I go to the uh let's go to the create story here We'll say magic land and boy uh select the story type age group image style and just simply click generate story
okay and as soon as I click you got this to message saying you don't have enough credits right so that's how guys uh you can uh restrict user to create a new story if user don't have the credits now we are going to implement the functionality where user can buy a more credit right now if you see I have Only zero credit left and if you want to buy uh we are going to give a button to buy more credit and then we are going to display a new screen where user have option to select
how many credit he want so once the option is selected then we are going to integrate the pay Paypal payment Gateway it's very easy to integrate and anyone can use this PayPal account also they have the sandbox developer account so that you can use it for free or testing purpose as well now Once you integrate we are going to com uh implement the complete workflow for the payment and once the payment is successful then then we automatically going to add the credit uh to users account so first thing uh on our dashboard so over here
we are going to add a button uh to buy a more credits so let's go back to our application and first thing inside the dashboard uh where we have the dashboard header over here we are going to add the New button so here we we add a button and we'll say buy more credits okay and save it now let's see how it looks I will refresh the screen and this is how it will look like okay I think that's pretty good uh for this one let's add a class name and not class name but we'll
add a color and let's add a color as a secondary and we'll see how it looks so this is how it look like uh a secondary color or if you want you can also change the color over here okay so If you want a blue color light blue let's see how it looks I think this is much better okay perfect now once you the click on this one we want to navigate to the new screen so let's Implement a new route for that one so inside the app folder we are going to add a new
route called buy credits and click enter inside that we'll add page. TSX file add a default templates and we'll rename this by Credits and save it now inside the dashboard I will wrap this button inside the link tag so that we can add H reference to buy credits let and make sure to wrap your button inside this link tag okay so like this and save it now if I go back and click on this buy more credits if you see it's navigating to this by creates uh new page now first thing I will add a
class name here I will add height sorry minimum height to Screen and now we want to give some options as per this mockup right on the left hand side we are going to add some options and on the right hand side we are going to integrate the palal button to make a payment so for that one I'm going to add an options here uh we'll add ID as a one then price as let's say 1 199 and credits which we are giving as a 10 credits okay so user can create a 10 story uh same
Thing I will just copy it couple of more times so one is two three and let's add one more so for this is fourth here I will make a$ 299 and user can buy 30 credits uh here 75 credits for 4 5.99 and for $9.99 you can give 150 credits perfect right so all the options are ready now simple we need to just display that option before that let's add h2 tag give a heading here we say add more credits for S2 I will add a Class name let's add text is of 3 Exel and
font bold okay uh for this du let's add text to be in the center so all the content will be in the center and most important I forgot to add padding padding X to 20 and padding on medium screen padding X to 20 and on large screen padding X to 40 and save it okay if I go back to our application this is how it will look like um let make it for XL and here I will make text primary okay so it will be con Consistent throughout the application now next we'll add a de
and uh we need to divide the screen into two section okay so we'll add a class name here we'll make grid on the smaller screen we'll make one column but on medium or larger screen we'll make a true column now on the First Column we are going to show an option and on second column we are going to add the PayPal button so inside the first option let's add an uh let's iterate this options do Map here we'll say option comma index is of type number and option is of type I don't know whether we
do we need to provide the type for it or not but let's keep it for now as it is and then just provide the arrow function like this I think we don't need to provide because we directly use this option list right next um let's add a de inside this D we'll add an H2 tag and we'll simply write get here we Say option do credits credits is equal to item op option do credit story okay as many credits which is equivalent to same story okay so that's the reason I wrote like this if I
save it you will see on the screen how it looks here okay obviously we are going to add some style to that one but before that let's add one more text and we want to display the Price so dollar and here we'll say item do price uh is option. price okay and then we'll add a class name for this one we'll make font bold text is of to Excel and save it now for this due let add a class name here uh we'll add a padding of six margin Y 2 3 then we'll add a
border and uh we'll make background as a primary text to be in the center and we'll make rounded as a large and save it and this is how Beautifully it will look like obviously uh we'll make text to White so text white perfect okay and that's how our card is now ready perfect now obviously we want to add a selection option as well uh over here I will just add margin top to 10 some some gap between this perfect now on the select of this one we want to add an selection option so let me
collapse this one and here we'll add a state we will say selected Option comma set selected option is equal to use State okay now whenever you just select something right we are going to add it so here I will add a cursor pointer and on H I will just change or add some kind of Animation so we'll scale it to 105 and we'll make smooth transition and let me show how how it looks okay so we use use state so I will make this component on the client side so here we'll add use client and
save It now if i h on this one see how beautifully it looks correct perfect um or maybe we can add a border let's say how it looks on Border um here we need to give a margin to one and let's add a border let's look how it look so B is getting added but it's not that much visible so on how let's add a padding to one that not good okay let me keep like this for now Okay so here we'll scale to 105 and we'll keep like this only and that's fine perfect okay
next as soon as user select any of this uh uh option right we need to make sure to add a effect but here first thing we'll add onclick method and we'll set the selected option as and complete option okay so or we can say option. ID let's add option do ID okay obviously here uh we need to add an state so here we'll say Number perfect okay now we have the option. ID let's save it and as soon as I select it is getting saved now once it saved we need to add some kind of
uh selection effect okay this particular one is is selected or not okay so simply uh let's wrap this class name let me bring this down and wrap it inside the curly braces this tag so that we can add and uh conditional styling so over here inside this tag only we will write condition if selected option is matches With the it option do ID okay if it's matches then we can write a condition let's add a background as a black color and save it and let's see how it looks so if I select this one see
perfect right so that's how it will look like perfect now once I selected it means this particular um option is selected and we are saving that option ID now it's time to uh uh add a payment option so in order to add a Paypal payment option we are going to Use this Library called PayPal react PayPal JS I will keep the link in the description it's very easy to use first you need to install this um Library just copy this npm command go to your terminal and just paste it here okay next step if you
scroll down little bit I'm I'm going to tell you the simplest way and easiest way to do it so over here you need to wrap your application inside this PayPal script provider okay so copy this command copy this line of Code only and then go to the providers. TSX file okay which is inside your root directory inside here maybe under this provider you can just wrap it like this and then make sure to import this PayPal script provider so I will just copy this command and paste it here obviously we are not using PayPal button
on this particular provider so I will remove that input and then we need to provide the client ID now the question is where To get this client ID now in order to get this client ID just go to this developer . paypal.com okay this is the developer account where you can uh play with it now if you don't have an account create a new account it's completely free and then go to the dashboard so I will just go to this dashboard section yeah go to the dashboard developer dashboard and if you want to start this
in a live mode you need to upload your business document or some freelancing Document so it will uh enable the live mode for you so currently I'm on the sandbox mode now inside here click on this API credential and if you don't have API credential make sure to add a new uh or create a new app from here I already have default app I will copy this client ID make sure to copy the client ID only then go to your env. loal file inside here we'll create nextore Public PayPal client ID is equal to and
paste this client ID and save it now copy this environment key go to your provider. txx file and instead of this or on this place to the client ID let's add process. EnV and the the client ID environment key okay so right now we got error that string and undefined so here we can put option if this client ID is not there put it empty and obviously PayPal will not work but we are passing this ID so it will never fail now let's Save it and now go back to your page. TSX file now inside
here uh to this second do okay now we want to add the button so go to this document and if you see this button we need to add it copy this button and simply paste it here make sure to import this import statement and then save it now if I go back to our application you will see this button is showing on the screen cool right now you can even change this uh layout currently showing horizontal But if you want to make it vertical you can just make it vertical and just refresh the screen and
now you'll see uh these three options now for this particular grid uh you can just give gap of 10 item to be in the center justify Center so it will be in the center like this and once you select the any option then we want to make sure that PayPal button is get enabled so to this PayPal button I think we have an option called disable and it Is disabled if the selected option is not there okay now if I refresh this screen and now right now if you see the option is disabled until unless
you are not making selection once you make the selection it is get enabled immediately now the question is how to pass the amount which uh user is selected that amount need to be passed to this PayPal so for that one um we have some method over here so first thing we have to create an order So uh PayPal button has one method called create order inside that we'll say dataa action and then inside that we have to return the statement we'll say actions do order do create so it will create an order for us with
an uh amount which we want so here we'll say purchase unit and inside that we have to pass amount now amount value we need to add It here so in this case the amount value we need to get it from the options of selected option okay so whether it's a zero element or one okay something like this um over here I will just add minus one because uh if the option is one right we have to make sure user will select this first item and second and so on and just add minus one and here
we need to add the price something like this make sure to wrap it as a Number and Dot to fix to two okay here we got if you got an error just add this TS ignore and uh after this we need to add the currency code so which is the USD okay now this TS signore I think we need to add just before or maybe just before this one and now it's saying the TS sorry selected option possibly and undefined right so here you can add in zero for example okay and obviously you need to
write a Condition then for the disable so we have this button disable if selected option is not there or or you can add s option is not equal to zero okay and that will work perfect now once your order is created then it successfully pass this particular amount okay and that's all Next Thing Once the payment is successful you can show an on approval um it it will give on approve call back and on that one you can just Say console. log success okay now obviously on the success we need to make sure we we
will update the users credit into our database okay we'll also ignore this one perfect and let's save it now let's go back to our application and select the credits I will just refresh this ones and you can select the any credit which you want so once we have the error because button is not showing let's see why so I just refresh it again and now It's working now select the option so right now I select the option but still uh disabled so let's see why okay so is equal to equal to zero okay the condition
was incorrect now if I select anything now it's working now click on any of the option if you want to make a payment with the PayPal you can select this one or you want to make a payment with the debit or credit card you can select that option as well so right now it's flicking uh it's not Showing the option so let's see why right now in the terminal it's saying cannot read a property of undefined okay so basically whatever the option the price we are sending it's not getting correctly maybe what we can do
we can as soon as the option is selected we can uh save the actual price in different state so here I will Define the use effect and inside this use effect whenever the selected option change and here we'll add a condition if Selected option is not equal to zero then you can write a condition then simply uh constant price is equal to here we'll say uh options of selected options dot price okay and I will just console this price first to check whether we are getting the exact value or not so if I select this
299 see we are getting price as a$ 599 perfect so let's save this in one state so here we'll say constant selected price comma set selected price Is equal to use State and over here we'll simply set the selected price as a price okay so it is of type number and save it now this selected price we will pass over here so selected price so it's showing possibly undefined value because it might be undefined let's put a zero for now and save it if I go back select the amount now if you see we got
this one now oh again we have An error it's saying check out order with 404 error un possibility entity some cannot be zero or negative okay let's see the value is showing as a zero that's interesting so the question is problem is that how the default selected price is showing as a zero and that's the reason we got this error so in PayPal you cannot dynamically update this price so either way or one of the way that you can do it that you can add a condition If if selected price is greater than zero then
only show this PayPal button okay something like this and and save it now if I go back refresh this screen and if I select this price see now the paper button is showing right now on the click of this one obviously you can uh pay with the credit card or debit card if I click on this pay with the debit or credit card you will see this price and then you can add your account information cool right so here you once You add account information and click on pay now then this on um approve method
will get call so let's Implement that particular functionality so first thing let me close this tab and over here we'll say constant on payment success here we'll do constant await sorry constant uh result is equal to await DB dot insert because sorry update we want To update the record and here here we need to pass the user schema do set and inside here we'll just update the credits which is equal to the opt whatever the option user selected so here we say options of selected options dot credits okay that's all actually uh we want to
add within existing credit okay user have three credit and user buy 10 extra credit so it need to be a 10 + three so for that One we need to get a user detail context information use context and here we'll say user detail context okay and now here we'll just add user detail dot credits okay so it will just add that one and let make this as a sync over here and obviously we need to add a we condition so where you can just add equal to user detail dot sorry user detail let's first add
the Users dot user email matches with the user user detail dot user email okay perfect and if you want to return something you can return okay but I will keep this as it is and here I will just if you want to add the notify or want to add some popup you can and once it's successful we'll just redirect it to the new screen in order to Red it you can add constant router is is equal to use Router from the next SL navigation and simply here we say router do replace and then we'll navigate
to the dashboard perfect right now this on payment success we going to add it on the click of approve so here on approve we'll call this method perfect right and save it also for toast message I'm going to copy existing notify option and we'll paste it here make sure to import this toast message And save it and whenever the payment is successful we can show the toast message if result then you can show notify uh credit is added okay and when user cancel the payment right here we have on cancel method here we'll simply add
notify error payment cancel and then save it okay now if you want to teste let's teste this so if you Click on this one PayPal or credit card here you have option to add it okay but let's close this one and let's make a payment with the PayPal and if I cancel this one let's cancel this I don't know why it's not showing maybe we need to refresh this one and then it will show so once you select this one then you got this option perfect before testing it uh I will also make sure as
soon as credit is added we need to update our contact so we have This set user detail contact make sure it's set user detail okay and then inside here we'll add a previous as of type any and inside this one we'll add the arrow function like this so initially I will add all the value from the previous and we'll just update the credit okay and the value of the credit will be the same that we are going to update to our database something like this okay then it will update the set user detail uh contact
as well and then We are going to navigate it so let's navigate over here and result if it's false will notify with an error we say server error okay and then save it perfect now let's test this out also uh you have to make sure you are passing this correct value to this PayPal button and one more important thing I think I forgot here in the selected option right we'll select the minus one because it is starting from the first element okay so that is also important let's save it and Let's test this out now
if I select this uh 299 option select the PayPal and then select this pay with a debit or credit card and then if you see we have this 299 option now I will let me close this one and here in the card testing this is the test card okay where you can test uh with a testing card so I will copy this test card and let me go back over here so this is the PayPal I will just make Sure it's open on the top of this one up my bad this is literally difficult now
we are good I will paste this card number the expiration date I will put something like this and some CVB here let's add the detail make sure to add a phone number okay and then let's add a city the zip code and all of this information and simply click pay now here uh the Address was mismatch so they corrected our address and then let's make it and make a successful payment and boom if you see our credit is changed to 675 now cool right and now obviously if I go to this create story I can
generate the more story because I have the credits now right and in the dashboard you will see that all the credit information obviously if you want to buy more you can just buy it again by selecting these options perfect right so that's how guys You can add the payment integration and make this complete application into a SAS application let me talk about the tub guru.com where you will find all of my exclusive courses along with the source code for each of the project not only that but you can track each course chapter by chapter here
you'll get the source code the application Depot or you can even uh watch this complete video for free on YouTube how cool it is right Also you can find your own personal dashboard and you can track your course depends on your how much progress you did it okay and in order to join this particular tube guruji Pro membership it will just at $899 per month okay and and you will get an access to the all the courses and the source code for free now we are going to build an explore page where user can search
uh all the stories created by other users as well okay so uh this is Very simple so we are going to use existing component for that one and uh we are showing first 8 to 10 stories and on the click of load Moree we are going to show more stories okay so it's kind of infinite load uh on the click upload more they can uh go ahead and add more and more okay so first thing let's go back to our application and inside here we need to add a new um route for the Explorer stories
so let's me go back to our application inside the app uh folder We are going to create a new folder called explore and inside that we'll create a page. TSX file add a default template and we'll just rename this to explore explore more we can select that and save it now just for the sake uh let's go to the components header and inside here um we have this Manu list make sure we have here explore I think that's good perfect now for this class We'll add a class name height minimum height to screen and save
it now if I go Back to our application and click on this explore story it will go to this explore page perfect then I will give some height and width so here we'll add a padding of 10 then on medium screen size we'll add padding X to 20 and on large we add padding X to 40 and then save it over here we'll add h2 tag and here we'll say Explore More stories We'll add a class name we'll make font bold text is of for Excel and text is of primary then uh we'll also Make
sure the text to be in the center and then save it now now it's going above this one okay yeah now it's good because I scroll it that's why perfect and now we have this text as well next uh we want to write a method called constant get all storage and over here we need to write an offset value okay so obviously we'll pass this offset value which is in the form of number so I'm going to tell you why we are using this offset okay but Before that let's fetch the value so here we'll
say await db. select from and over here we'll just simply add um story data schema Dot and inside there obviously we want to F all the stories right so simply here we'll say order byy first and we want in descending order with an ID okay so this need to be a story data. ID here I will make this as a sync after this we need need to pass how many value we want to fetch okay so Right now we'll just fetch eight value okay means eight records and here we want to pass the offset offset
means where the row will start and how many record want to skip it right so initially uh the offset value will be the zero but with this value we are going to pass to this G all stories now inside the use effect so if I pass the use effect over here and obviously uh get all I'm going to call and to this offset I will pass Initially as a zero okay and next time every time we are going to increment it okay so let's save it and I'm going to maintain constant offset comma set offset
is equal to use State and initially it will be the zero only okay and save it next uh let's save and let's see whether we are getting data obviously this is a uh client side page so I will make this as a client and save It now if I go back and inspect panel go to the console and here oh so I don't think so we got it let's refresh it again and this is the different data actually let me go over here oh we forgot to console actually and we are trying to see why
we are not getting the result so make sure to console this one insert result and me refresh it again so right now if you see we got the first eight records cool right perfect now once you done with this one We need to save this in one state so here we say constant story list comma set story list is equal to use State okay initially it's empty and save now simply I'm going to set the story list as a result now obviously um from the user story list right we already have this story item type
so simply I'm going to pass this story item type over here and even though if you did not pass that's also fine and then save it so Maybe it's a list so that need to be list here I will make a type any for now and save now over here we just need to iterate this list so story list sorry uh yeah story list. map here will say item comma index and the arrow function over here I will just wrap this inside the D okay now here we'll just simply use the story item card component
and inside the story item card component we need to pass a story as an item so inside the story item data we already have Sorry we have the story item card which we are fetching okay let's save it and let's see whether we are going to display it or not so it's refreshing and boom if you see we got the story list but obviously it's not in the correct format so for this de let add a class name make it grid grid column one on the smaller screen on medium screen we'll make grid column two
and large screen we make grid column three and on extra large we'll Make grid column 4 okay then also we'll give gap of 10 and save it and boom if you see this is how it beautifully looks right now next let me give some margin top so here we'll say margin top to 10 so we have some space and now we got the information okay so right now it's only f8 records now if you want to fet more right here we are going to add a button so after this du We'll add a Button called
load more and over here to this button we'll add a class name and we'll not class name but we'll add a color as a primary button make sure it is working so if you if you see this is load more is showing over here obviously we want it is in the center of the screen so simply I'll put it in the DU We'll add a class name and text to be in the center let's see whether is working or not with this I hopefully yeah and also we'll give margin top to 10 perfect And save
it okay now once you click on the load more we have to make sure that we'll add the or we will F the new record but we need to update this story list with the existing records right so first uh let's add on click method to this onclick method we say uh what is the method name get all stories okay and in this one we have to add an opset value okay so let's add offset here offset Plus 8 okay now Obviously offset is zero but we need to update the offset every time so here
we say set offset with the offset value every time okay and save it now obviously if I click roll load more right it's updating with the existing one and it's remove the uh old stories okay so we need to fix that issue set the story list as a previous story list then we'll add spread operator and then we also want to add the all the result one by one to this story list so that's The reason I added the sprad operator save it and let's go back Let me refresh the screen and we'll see whether
we got the result and boom if you see we got the result if I click load more it will get uh the next uh lot of this uh uh what do you say the story list okay right now obviously we don't have anything that's why it's not getting but now we got all the story list and obviously once you open any story list it will navigate to this particular page Where you can start reading the story so that's how guys uh this particular story list functionality uh will work obviously uh you can add load more
or you can even uh load all the uh story at once so if you have any question any doubt let me know in the comment section now it's time to deploy our application to the cloud so so that your friends and family can share this or you can start your own small size application you don't need to deploy any Back end because our um dzel studio is already there and whatever the API we created it is on already on the nextjs uh server side now in order to deploy our application we are going to use
a woral and is taking care of everything but before that we are going to push our application to the GitHub and then we'll connect GitHub to the worel so whenever you deploy any code changes to your application to the GitHub it automatically deploy the latest changes Uh to the worel okay so let me walk through how to do it first thing let's open the terminal I will clear this one and just click on git in it okay so it will initialize the git into your application then just go to this github.com and just create a
new account if you don't have and press new here you have to create a new repo so I will give the the name AI kids story Builder and you can add a description you can make this repo public or private And simply create repo now once the repo is created you just need to copy this remote origin and just paste it here now this is you need to do only first time and simply here you need to add all the files you make the changes then you have to write commit message okay so here we'll
say initial commit you can give any message and then you need to push these changes now if you are pushing this first time you need to push With this particular command otherwise you can just write get push okay now we push this changes to this main branch and it's pushing all the changes now we have a lot of images in our public folder so that's the reason it's taking some time to push these changes but next time it it will be very quick now is push all the changes if I refresh this GitHub repo and
boom if you see just now the changes are push okay and you have all the project code is inside This a uh repository except this env. loal file and this known module because env. loal file is contain a very crucial and secret data right and that need to be upload directly on the server so now once it is pushed go to the worel and if you don't have an account on wor.com let's create an account most important thing is free to use so you can deploy as many as website you want or application you want
okay now once you create account and go to the dashboard This is how it will look if you see I deployed a lot of different application now click on add new and click project inside here you will see the pro my current project which is just deployed 53 seconds ago now obviously if you need to connect the git Depo with your GitHub account if you are using gitlab or any other make sure to add that as well and simply click import once you import this one you will see the project name then you can Obviously
update this project name if you want uh obviously it will detect the which framework you are using obviously we are using nextg so it will detect you don't need to change anywhere but you need to add this environment variables okay so whatever the variables we have inside this EnV just copy all of them and simply paste it inside this box so automatically detect the the key and the respective value see perfect right and then simply click deploy once you click Uh deploy button it will take couple of minute to deploy your application and if you
see the deployment is started currently it building some log then it will install all the dependency and then it will start actual deployment so I will pause the video and we'll wait and boom if you see our appli appication is now deployed and here you will see the preview how it actually look like and once you click it it will open that particular URL okay and if I Go to the dashboard obviously here you will see all the details inside the setting section you have option to add your own custom domain but if you want
to change this one let's change this because they added some custom uh domains so maybe this domain is not available here you can just update something like let's say AI kid story generator and save it I think this is available see and now if I open this one you will see your application is running On that particular domain and everything will work like a charm see all authentication is there if I go to the Explorer storage you will see all the stories into your application perfect right so that's how guys you can deploy your application
Universal for free