jhhackaton/frontend/script.js

97 lines
2.7 KiB
JavaScript

// 예시 텍스트 설정
function setExample(element) {
document.getElementById('searchInput').value = element.textContent;
}
// 쿼리 파싱 및 분석
async function parseQuery() {
const input = document.getElementById('searchInput').value.trim();
if (!input) {
alert('검색할 내용을 입력해주세요!');
return;
}
// UI 상태 변경
document.getElementById('loadingSection').style.display = 'block';
document.getElementById('resultSection').style.display = 'none';
document.getElementById('searchBtn').disabled = true;
try {
const response = await fetch('/api/parse', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({ text: input })
});
if (!response.ok) {
throw new Error('서버 오류가 발생했습니다.');
}
const data = await response.json();
displayResults(data);
} catch (error) {
alert('오류: ' + error.message);
console.error('Error:', error);
} finally {
document.getElementById('loadingSection').style.display = 'none';
document.getElementById('searchBtn').disabled = false;
}
}
// 결과 표시
function displayResults(data) {
const resultContent = document.getElementById('resultContent');
let html = '';
if (data.transaction_type) {
html += `<div class="result-item">
<strong>거래 유형:</strong> ${data.transaction_type}
</div>`;
}
if (data.price) {
html += `<div class="result-item">
<strong>가격:</strong> ${data.price}
</div>`;
}
if (data.location) {
html += `<div class="result-item">
<strong>위치:</strong> ${data.location}
</div>`;
}
if (data.area) {
html += `<div class="result-item">
<strong>면적:</strong> ${data.area}
</div>`;
}
if (data.rooms) {
html += `<div class="result-item">
<strong>방 개수:</strong> ${data.rooms}
</div>`;
}
if (!html) {
html = '<p>추출된 정보가 없습니다. 더 구체적으로 입력해주세요.</p>';
}
resultContent.innerHTML = html;
document.getElementById('resultSection').style.display = 'block';
}
// Enter 키로 검색
document.addEventListener('DOMContentLoaded', function() {
document.getElementById('searchInput').addEventListener('keypress', function(e) {
if (e.key === 'Enter' && !e.shiftKey) {
e.preventDefault();
parseQuery();
}
});
});