Skip to content

Commit

Permalink
feat: basic axios api request
Browse files Browse the repository at this point in the history
  • Loading branch information
isinia committed Oct 21, 2024
1 parent eda9ba5 commit ef05b6e
Show file tree
Hide file tree
Showing 4 changed files with 116 additions and 11 deletions.
8 changes: 7 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,15 @@ git clone [email protected]:stanislawowski/TrollFactory-Web.git
Install the dependencies:

```bash
pnpm install # or your package manager of choice
pnpm install
```

Set the API URL as an environment variable:
```bash
echo "NEXT_PUBLIC_TF_API_URL=http://localhost:8000" > .env.local
```


And run the development server:

```bash
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
"lint": "next lint"
},
"dependencies": {
"axios": "^1.7.7",
"next": "14.2.15",
"react": "^18",
"react-dom": "^18"
Expand Down
85 changes: 79 additions & 6 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

33 changes: 29 additions & 4 deletions src/app/page.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,27 @@
"use client";

import { useState } from 'react';
import axios from 'axios';
import Image from 'next/image';

export default function Home() {
const [person, setPerson] = useState('');
const [loading, setLoading] = useState(false);

const generatePerson = async () => {
setLoading(true);
try {
const response = await axios.get(`${process.env.NEXT_PUBLIC_TF_API_URL}/generate`);
setPerson(response.data);
console.log(person);
} catch (error) {
console.error('Error fetching data:', error);
setPerson('Error fetching data');
} finally {
setLoading(false);
}
};

return (
<div className="tf-bg-blur font-mono">
<div className="flex h-screen tf-bg-grid">
Expand Down Expand Up @@ -47,10 +68,14 @@ export default function Home() {
<input type="text" className="grow text-sm" placeholder="Last name" />
</label>
</div>
<button className="btn" id="generate-btn">
<svg xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 24" strokeWidth={1.5} stroke="currentColor" className="size-6">
<path strokeLinecap="round" strokeLinejoin="round" d="M17.25 8.25 21 12m0 0-3.75 3.75M21 12H3" />
</svg>
<button className="btn" id="generate-btn" onClick={generatePerson} disabled={loading}>
{loading ? (
<span class="loading loading-ring"></span>
) : (
<svg xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 24" strokeWidth={1.5} stroke="currentColor" className="size-6">
<path strokeLinecap="round" strokeLinejoin="round" d="M17.25 8.25 21 12m0 0-3.75 3.75M21 12H3" />
</svg>
)}
</button>
</div>
</div>
Expand Down

0 comments on commit ef05b6e

Please sign in to comment.