@@ -4,11 +4,16 @@ import Head from "next/head";
44import Image from "next/image" ;
55import axios from "axios" ;
66
7- const Home : NextPage = ( ) => {
7+ type IProps = {
8+ data : string [ ] ;
9+ }
10+
11+ const Home : NextPage < IProps > = ( { data } ) => {
812 const [ description , setDescription ] = useState < string > (
9- "Milky way galaxy"
13+ data [ 0 ]
1014 ) ;
1115 const buttonRef = useRef < HTMLButtonElement > ( null ) ;
16+ const suggestButtonRef = useRef < HTMLButtonElement > ( null ) ;
1217 const [ wallpaper , setWallpaper ] = useState < string | null > ( null ) ;
1318
1419 /**
@@ -18,6 +23,7 @@ const Home: NextPage = () => {
1823 */
1924 const generateWallpaper = async ( ) => {
2025 buttonRef . current ! . disabled = true ;
26+ setWallpaper ( null ) ;
2127 try {
2228 buttonRef . current ! . innerText = "Generating wallpaper..." ;
2329 const response = await axios . post ( "/api/generate" , {
@@ -33,6 +39,34 @@ const Home: NextPage = () => {
3339 }
3440 } ;
3541
42+ const suggestWallpaper = async ( ) => {
43+ buttonRef . current ! . disabled = true ;
44+ suggestButtonRef . current ! . disabled = true ;
45+ setWallpaper ( null ) ;
46+
47+ // Get random word from data
48+ const index = Math . floor ( Math . random ( ) * data . length ) ;
49+ const word = data [ index ] ;
50+ data . slice ( index , 1 ) ;
51+
52+ setDescription ( word ) ;
53+
54+ try {
55+ suggestButtonRef . current ! . innerText = "Suggesting wallpaper..." ;
56+ const response = await axios . post ( "/api/generate" , {
57+ description : word ,
58+ } ) ;
59+ setWallpaper ( response . data . data [ 0 ] . url ) ;
60+ } catch ( err ) {
61+ console . log ( err ) ;
62+ setWallpaper ( null ) ;
63+ } finally {
64+ suggestButtonRef . current ! . innerText = "Suggest wallpaper" ;
65+ buttonRef . current ! . disabled = false ;
66+ suggestButtonRef . current ! . disabled = false ;
67+ }
68+ }
69+
3670 return (
3771 < div className = "flex min-h-screen flex-col items-center justify-center py-2" >
3872 < Head >
@@ -64,7 +98,14 @@ const Home: NextPage = () => {
6498 onChange = { ( e ) => setDescription ( e . target . value ) }
6599 />
66100 </ div >
67- < div className = "mt-6 flex justify-center w-full" >
101+ < div className = "mt-6 flex space-x-4 justify-center w-full" >
102+ < button
103+ className = "w-full py-2 md:text-sm bg-[#081477] rounded-md text-white"
104+ onClick = { suggestWallpaper }
105+ ref = { suggestButtonRef }
106+ >
107+ Suggest wallpaper
108+ </ button >
68109 < button
69110 className = "w-full py-2 md:text-sm bg-[#081477] rounded-md text-white"
70111 onClick = { generateWallpaper }
@@ -103,3 +144,20 @@ const Home: NextPage = () => {
103144} ;
104145
105146export default Home ;
147+
148+ export async function getServerSideProps ( ) {
149+ const res = await axios . post ( "http://localhost:3000/api/list" ) ;
150+ const { data } = res ;
151+
152+ if ( ! data ) {
153+ return {
154+ notFound : true ,
155+ } ;
156+ }
157+
158+ return {
159+ props : {
160+ data,
161+ } ,
162+ } ;
163+ }
0 commit comments