1+ const promptForm = document . querySelector ( '#promptForm' ) ;
2+ const promptText = document . querySelector ( '#promptText' ) ;
3+
4+ promptForm . addEventListener ( 'submit' , async ( event ) => {
5+ event . preventDefault ( ) ;
6+ // alert('폼 제출!')
7+ const promptTextValue = promptText . value . trim ( ) ;
8+ // alert(`입력된 프롬프트: ${promptTextValue}`);
9+ if ( ! promptTextValue ) {
10+ alert ( '프롬프트를 입력해주세요.' ) ;
11+ return ;
12+ }
13+ // 페치 시 폼 버튼 disabled
14+ const submitButton = document . querySelector ( '#promptForm button' ) ;
15+ submitButton . disabled = true ;
16+ // 프롬프트 처리
17+ try {
18+ const baseUrl = 'https://annoying-sunbae.onrender.com' ; // 이걸 바꾸면 됩니다
19+ const response = await fetch ( `${ baseUrl } /api/prompt` , {
20+ method : 'POST' ,
21+ // Fetch시 직렬화하세요...
22+ body : JSON . stringify ( { text : promptTextValue } ) ,
23+ headers : {
24+ 'Content-Type' : 'application/json'
25+ }
26+ } ) ;
27+ if ( ! response . ok ) {
28+ alert ( '프롬프트 처리 중 오류가 발생했습니다.' ) ;
29+ return ;
30+ }
31+ const result = await response . json ( ) ;
32+ // alert(JSON.stringify(result));
33+ // 결과를 화면에 표시
34+ const promptDataId = document . querySelector ( '#promptDataId' ) ;
35+ const promptDataQuestion = document . querySelector ( '#promptDataQuestion' ) ;
36+ const promptDataAnswer = document . querySelector ( '#promptDataAnswer' ) ;
37+ promptDataId . textContent = result . id ;
38+ promptDataQuestion . textContent = result . question ;
39+ promptDataAnswer . textContent = result . answer ;
40+ // 복사 관련
41+ const pageUrl = 'https://qus0in.github.io/sunflower' ;
42+ const shareLink = document . querySelector ( '#shareLink' ) ;
43+ shareLink . value = `${ pageUrl } /history/?id=${ result . id } ` ;
44+ const openLink = document . querySelector ( '#openLink' ) ;
45+ openLink . href = `${ pageUrl } /history/?id=${ result . id } ` ;
46+ const promptData = document . querySelector ( '#promptData' ) ;
47+ promptData . style . display = 'block' ;
48+ } finally {
49+ // 다 끝나면
50+ submitButton . disabled = false ;
51+ }
52+ } ) ;
0 commit comments