-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmain.js
More file actions
150 lines (137 loc) · 6.05 KB
/
main.js
File metadata and controls
150 lines (137 loc) · 6.05 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
async function getRecommendations() {
const startingCity = document.getElementById('startingCity').value;
const activities = Array.from(document.querySelectorAll('input[name="activities"]:checked')).map(cb => cb.value);
const budget = document.querySelector('input[name="budget"]:checked').value;
const regions = Array.from(document.querySelectorAll('input[name="regions"]:checked')).map(cb => cb.value);
const startDate = document.getElementById('startDate').value;
const endDate = document.getElementById('endDate').value;
const prompt = `
I'm planning a vacation with the following preferences:
- Starting from: ${startingCity}
- Activities: ${activities.join(', ')}
- Budget: ${budget}
- Preferred regions: ${regions.join(', ')}
- Travel dates: ${startDate} to ${endDate}
Please recommend 5-8 major cities that would be perfect for this vacation.
For each city, provide the following information in JSON format:
{
"cities": [
{
"name": "City, Country",
"activities": ["activity1", "activity2", ...],
"budget": "luxury/moderate/budget-friendly",
"bestSeasons": ["spring", "summer", "fall", "winter"],
"description": "Brief description of the city",
"continent": "Continent name",
"country": "Country name",
"coordinates": {"lat": latitude, "lng": longitude}
}
],
"travelTimes": {
"City1, Country1": {
"City2, Country2": {"airplane": hours, "train": hours}
}
}
}
Please ensure the response is in valid JSON format and includes both the cities array and travelTimes object.
Make sure the cities are well-distributed geographically and offer the activities requested.
For each city, determine if it's luxury, moderate, or budget-friendly based on average costs.
Include approximate travel times between cities using both airplane and train (if available).
`;
try {
const response = await fetch('/api/gemini', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({
prompt: prompt,
startingCity: startingCity
}),
});
if (!response.ok) {
throw new Error(`HTTP error! status: ${response.status}`);
}
const data = await response.json();
if (data.error) {
throw new Error(data.error);
}
// Parse the response and update the UI
const recommendations = JSON.parse(data.response);
displayRecommendations(recommendations);
} catch (error) {
console.error('Error:', error);
const errorMessage = document.getElementById('errorMessage');
errorMessage.textContent = `Error: ${error.message}`;
errorMessage.style.display = 'block';
}
}
function displayRecommendations(data) {
try {
// Make sure the recommendations tab is active
showTab('recommendations');
// Get the containers
const cityCardsContainer = document.getElementById('cityCards');
const travelTimesContainer = document.getElementById('travelTimes');
if (!cityCardsContainer || !travelTimesContainer) {
throw new Error('Required containers not found');
}
// Clear existing content
cityCardsContainer.innerHTML = '';
travelTimesContainer.innerHTML = '';
// Create a card for each city
data.cities.forEach(city => {
const card = document.createElement('div');
card.className = 'col-md-4 mb-4';
card.innerHTML = `
<div class="card">
<div class="card-body">
<h5 class="card-title">${city.name}</h5>
<p class="card-text">${city.description}</p>
<p><strong>Budget:</strong> ${city.budget}</p>
<p><strong>Best Seasons:</strong> ${city.bestSeasons.join(', ')}</p>
<p><strong>Activities:</strong></p>
<ul>
${city.activities.map(activity => `<li>${activity}</li>`).join('')}
</ul>
</div>
</div>
`;
cityCardsContainer.appendChild(card);
});
// Create travel times table
travelTimesContainer.innerHTML = '<h3>Travel Times Between Cities</h3>';
const table = document.createElement('table');
table.className = 'table table-striped';
table.innerHTML = `
<thead>
<tr>
<th>From</th>
<th>To</th>
<th>Airplane</th>
<th>Train</th>
</tr>
</thead>
<tbody>
${Object.entries(data.travelTimes).map(([fromCity, destinations]) =>
Object.entries(destinations).map(([toCity, times]) => `
<tr>
<td>${fromCity}</td>
<td>${toCity}</td>
<td>${times.airplane ? times.airplane + ' hours' : 'N/A'}</td>
<td>${times.train ? times.train + ' hours' : 'N/A'}</td>
</tr>
`).join('')
).join('')}
</tbody>
`;
travelTimesContainer.appendChild(table);
} catch (error) {
console.error('Error displaying recommendations:', error);
const errorMessage = document.getElementById('errorMessage');
if (errorMessage) {
errorMessage.textContent = `Error displaying recommendations: ${error.message}`;
errorMessage.style.display = 'block';
}
}
}