Skip to content

Commit 67410c3

Browse files
authored
Merge pull request #188 from Pseudo-Lab/feat/api-calls
feat(getcloser): api calls
2 parents b0e1f8a + 6624df7 commit 67410c3

2 files changed

Lines changed: 66 additions & 8 deletions

File tree

โ€Žgetcloser/frontend/src/app/page1/page.tsxโ€Ž

Lines changed: 27 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,37 @@ import { Label } from '@/components/ui/label';
88
import { useFormStore } from '../../store/formStore';
99
import { Rows } from 'lucide-react';
1010

11+
import { useRouter } from 'next/navigation';
12+
1113
export default function Page1() {
1214
const { email, setEmail } = useFormStore();
15+
const router = useRouter();
1316

14-
const handleSubmit = (e: React.FormEvent) => {
17+
const handleSubmit = async (e: React.FormEvent) => {
1518
e.preventDefault();
1619
console.log('Form Submitted:', { email });
17-
// Here you would typically send this data to an API
18-
alert('์ •๋ณด๊ฐ€ ์ œ์ถœ๋˜์—ˆ์Šต๋‹ˆ๋‹ค! ์ฝ˜์†”์„ ํ™•์ธํ•ด์ฃผ์„ธ์š”.');
20+
21+
try {
22+
const response = await fetch(`${process.env.APP_HOST}/api/v1/users/auth`, {
23+
method: 'POST',
24+
headers: {
25+
'Content-Type': 'application/json',
26+
},
27+
body: JSON.stringify({ email }),
28+
});
29+
30+
if (!response.ok) {
31+
throw new Error(`HTTP error! status: ${response.status}`);
32+
}
33+
34+
const result = await response.json();
35+
console.log('Auth API Response:', result);
36+
alert('์ •๋ณด๊ฐ€ ์ œ์ถœ๋˜์—ˆ์Šต๋‹ˆ๋‹ค!');
37+
router.push('/page2'); // Navigate to page2 after successful submission
38+
} catch (error) {
39+
console.error('Error submitting form:', error);
40+
alert('์ •๋ณด ์ œ์ถœ์— ์‹คํŒจํ–ˆ์Šต๋‹ˆ๋‹ค.');
41+
}
1942
};
2043

2144
return (
@@ -40,7 +63,7 @@ export default function Page1() {
4063
onChange={(e) => setEmail(e.target.value)}
4164
/>
4265
</div>
43-
<Link href="/page2"><Button type="submit" className="w-full">์ •๋ณด ์ œ์ถœ</Button></Link>
66+
<Button type="submit" className="w-full">์ •๋ณด ์ œ์ถœ</Button>
4467

4568
</form>
4669
</main>

โ€Žgetcloser/frontend/src/app/page2/page.tsxโ€Ž

Lines changed: 39 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
'use client';
22

3-
import React, { useState, useEffect } from 'react';
3+
import React, { useState, useEffect, useRef } from 'react';
44
import Link from 'next/link';
55
import { useRouter } from 'next/navigation';
66
import { Button } from '@/components/ui/button';
@@ -21,6 +21,7 @@ export default function Page2() {
2121
return initialInputs;
2222
});
2323
const [showModal, setShowModal] = useState(false);
24+
const timeoutRef = useRef<NodeJS.Timeout | null>(null); // Add timeoutRef
2425

2526
useEffect(() => {
2627
const hasSeenModal = Cookies.get('doNotShowModalPage2');
@@ -42,6 +43,34 @@ export default function Page2() {
4243
const newInputs = [...inputs];
4344
newInputs[index] = value;
4445
setInputs(newInputs);
46+
47+
// Clear any existing timeout
48+
if (timeoutRef.current) {
49+
clearTimeout(timeoutRef.current);
50+
}
51+
52+
// Set a new timeout to fetch user data after 3 seconds
53+
timeoutRef.current = setTimeout(() => {
54+
fetchUserById(index, value);
55+
}, 3000);
56+
};
57+
58+
const fetchUserById = async (index: number, id: string) => {
59+
if (!id) return; // Don't fetch if ID is empty
60+
try {
61+
const response = await fetch(`${process.env.APP_HOST}/v1/users/${id}`);
62+
if (!response.ok) {
63+
throw new Error(`HTTP error! status: ${response.status}`);
64+
}
65+
const userData = await response.json();
66+
// Assuming the API returns an object with a 'detail' field as per user's confirmation
67+
const newInputs = [...inputs];
68+
newInputs[index] = userData.detail || id; // Use fetched detail, or original ID if not found
69+
setInputs(newInputs);
70+
} catch (error) {
71+
console.error(`Error fetching user ${id}:`, error);
72+
// Optionally, display an error message to the user
73+
}
4574
};
4675

4776
const handleSolveProblem = () => {
@@ -74,11 +103,17 @@ export default function Page2() {
74103
<Label htmlFor={`team-email-${index}`}>ํŒ€์› {index + 1}</Label>
75104
{index === 0 && <Label> (๋‚˜)</Label>}
76105
<Input
77-
id={`team-email-${index}`}
78-
type="email"
79-
placeholder={`ํŒ€์› ${index + 1}์˜ ์ด๋ฉ”์ผ ์ฃผ์†Œ`}
106+
id={`team-id-${index}`}
107+
type="id"
108+
placeholder={`ํŒ€์› ${index + 1}์˜ ๋ฒˆํ˜ธ`}
80109
value={inputs[index]}
81110
onChange={(e) => handleInputChange(index, e.target.value)}
111+
onBlur={(e) => {
112+
if (timeoutRef.current) {
113+
clearTimeout(timeoutRef.current); // Clear any pending debounce
114+
}
115+
fetchUserById(index, e.target.value); // Fetch immediately on blur
116+
}}
82117
disabled={index === 0}
83118
/>
84119
</div>

0 commit comments

Comments
ย (0)