Skip to content

Commit a2ad6d3

Browse files
authored
πŸ‘Œ IMPROVE: Added suggestion feature (#412)
πŸ‘Œ IMPROVE: Added suggestion feature
2 parents 1bf0fc6 + c8a1991 commit a2ad6d3

File tree

5 files changed

+102
-4
lines changed

5 files changed

+102
-4
lines changed
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
11
OPENAI_API_KEY=
2+
RAPIDAPI_KEY=

β€Žwallpaper-generator/README.mdβ€Ž

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
### ⚑️ Features
1010

1111
- Generator wallpapers using AI
12+
- Suggest wallpapers
1213
- Mobile friendly
1314

1415
## πŸ“– Guide
-567 KB
Loading
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
// Next.js API route support: https://nextjs.org/docs/api-routes/introduction
2+
import type { NextApiRequest, NextApiResponse } from "next";
3+
import axios from "axios";
4+
5+
export default async function handler(
6+
req: NextApiRequest,
7+
res: NextApiResponse
8+
) {
9+
if (req.method === "POST") {
10+
const options = {
11+
method: "POST",
12+
url: "https://thefluentme.p.rapidapi.com/generate-post",
13+
headers: {
14+
"content-type": "application/json",
15+
"X-RapidAPI-Key": process.env.RAPIDAPI_KEY,
16+
"X-RapidAPI-Host": "thefluentme.p.rapidapi.com",
17+
},
18+
data: '{"post_title":"Suggest a list with 30 items that contains any word that describes a wallpaper","ai_model":"advanced_01","post_min_length":"199","post_max_length":"500"}',
19+
};
20+
21+
try {
22+
const response = await axios.request(options);
23+
24+
const suggestions: string[] = [];
25+
26+
response.data.ai_post.split(" ").map((word: string) => {
27+
if (!word.match(/[\d.]+/)) {
28+
suggestions.push(word);
29+
}
30+
});
31+
32+
res.status(200).json( suggestions);
33+
} catch (err) {
34+
res.status(500).json({ error: err });
35+
console.log(err);
36+
}
37+
}
38+
}

β€Žwallpaper-generator/pages/index.tsxβ€Ž

Lines changed: 62 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,16 @@ import Head from "next/head";
44
import Image from "next/image";
55
import 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}
@@ -95,11 +136,28 @@ const Home: NextPage = () => {
95136
<a href="https://rapidapi.com/?utm_source=github.com/RapidAPI&utm_medium=DevRel&utm_campaign=DevRel" className="underline">
96137
Rapid
97138
</a>{" "}
98-
DevRel Team β€’ Powered by <a href="https://openai.com/" className="underline">OpenAI</a> and <a href="https://rapidapi.com/hub?utm_source=RapidAPI.com/examples&utm_medium=DevRel&utm_campaign=DevRel">Rapid</a>
139+
DevRel Team β€’ Powered by <a href="https://openai.com/" className="underline">OpenAI</a> and <a href="https://rapidapi.com/hub?utm_source=RapidAPI.com/examples&utm_medium=DevRel&utm_campaign=DevRel" className="underline">Rapid</a>
99140
</p>
100141
</div>
101142
</div>
102143
);
103144
};
104145

105146
export 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

Comments
Β (0)