@@ -6,12 +6,58 @@ import { Button } from '@/components/ui/button';
66import { Label } from '@/components/ui/label' ;
77import { Textarea } from '@/components/ui/textarea' ;
88import { useFormStore } from '../../store/formStore' ;
9- import React from 'react' ;
9+ import React , { useEffect } from 'react' ; // Import useEffect
1010
1111export default function Page3 ( ) {
12- const { question, answer, setAnswer } = useFormStore ( ) ;
12+ const { question, answer, setAnswer, id , teamId , memberIds } = useFormStore ( ) ; // Destructure new state
1313 const router = useRouter ( ) ;
1414
15+ useEffect ( ( ) => {
16+ const assignChallenges = async ( ) => {
17+ if ( ! teamId || ! id || memberIds . length === 0 ) {
18+ console . warn ( 'Missing teamId, my_id, or memberIds for challenge assignment.' ) ;
19+ return ;
20+ }
21+
22+ const requestBody = {
23+ team_id : teamId ,
24+ my_id : id ,
25+ members_ids : memberIds ,
26+ } ;
27+
28+ console . log ( 'Assign Challenges Request Body:' , requestBody ) ;
29+
30+ try {
31+ const response = await fetch ( '/api/v1/challenge/assign-challenges' , {
32+ method : 'POST' ,
33+ headers : {
34+ 'Content-Type' : 'application/json' ,
35+ } ,
36+ body : JSON . stringify ( requestBody ) ,
37+ } ) ;
38+
39+ if ( ! response . ok ) {
40+ const errorData = await response . json ( ) ;
41+ throw new Error ( `HTTP error! status: ${ response . status } , message: ${ errorData . detail || response . statusText } ` ) ;
42+ }
43+
44+ const responseData = await response . json ( ) ;
45+ console . log ( 'Challenge assignment successful:' , responseData ) ;
46+ // Here you might want to update the question in the store based on responseData
47+ // setQuestion(responseData.challenge_question);
48+ } catch ( error : unknown ) {
49+ console . error ( 'Error assigning challenges:' , error ) ;
50+ let errorMessage = '알 수 없는 오류가 발생했습니다.' ;
51+ if ( error instanceof Error ) {
52+ errorMessage = error . message ;
53+ }
54+ alert ( `챌린지 할당에 실패했습니다: ${ errorMessage } ` ) ;
55+ }
56+ } ;
57+
58+ assignChallenges ( ) ;
59+ } , [ id , teamId , memberIds ] ) ; // Dependencies for useEffect
60+
1561 const handleSubmit = ( e : React . FormEvent ) => {
1662 e . preventDefault ( ) ;
1763 console . log ( 'Question:' , question , 'Answer:' , answer ) ;
0 commit comments