-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
279 lines (230 loc) · 12.4 KB
/
script.js
File metadata and controls
279 lines (230 loc) · 12.4 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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
// Load startups from JSON
let startups = [];
async function loadStartups() {
const loadingElement = document.getElementById('loading');
const startupsGrid = document.getElementById('startupsGrid');
try {
// Show the loading state
loadingElement.classList.remove('hidden');
startupsGrid.classList.add('hidden');
// Fetch the startup data
const response = await fetch('./data/companies.json');
startups = await response.json();
filteredStartups = [...startups];
// Render the startups
renderStartups(filteredStartups);
// Populate city dropdown
populateCityDropdown();
} catch (error) {
console.error('Failed to load startups:', error);
} finally {
// Hide the loading state
loadingElement.classList.add('hidden');
startupsGrid.classList.remove('hidden');
}
}
// Populate city dropdown dynamically
function populateCityDropdown() {
try {
const cityFilter = document.getElementById('cityFilter');
const uniqueCities = [...new Set(startups.map(startup => startup.location).filter(city => city))];
uniqueCities.sort(); // Sort cities alphabetically
// Clear existing options
cityFilter.innerHTML = '<option value="all">All Cities</option>';
// Add unique cities to the dropdown
uniqueCities.forEach(city => {
const option = document.createElement('option');
option.value = city;
option.textContent = city;
cityFilter.appendChild(option);
});
} catch (error) {
console.error('Error populating city dropdown:', error);
}
}
// Initialize the application
loadStartups();
let filteredStartups = [...startups];
// DOM elements
const searchInput = document.getElementById('searchInput');
const startupsGrid = document.getElementById('startupsGrid');
const noResults = document.getElementById('noResults');
const resultsCounter = document.getElementById('resultsCounter');
const categoryFilters = document.querySelectorAll('.category-filter');
// Initialize the page
function init() {
renderStartups(filteredStartups);
setupEventListeners();
}
// Setup event listeners
function setupEventListeners() {
// Search input
searchInput.addEventListener('input', handleSearch);
searchInput.addEventListener('focus', function() {
this.classList.add('search-focus');
});
searchInput.addEventListener('blur', function() {
this.classList.remove('search-focus');
});
// Category filters
categoryFilters.forEach(filter => {
filter.addEventListener('click', (e) => {
handleCategoryFilter(e);
filterCompanies(); // Trigger combined filtering
});
});
// City filter
const cityFilterElement = document.getElementById('cityFilter');
if (cityFilterElement) {
cityFilterElement.addEventListener('change', filterCompanies);
}
// Clear filters button
const clearFiltersButton = document.getElementById('clearFilters');
if (clearFiltersButton) {
clearFiltersButton.addEventListener('click', clearFilters);
}
}
// Handle search functionality
function handleSearch() {
const searchTerm = searchInput.value.toLowerCase().trim();
if (searchTerm === '') {
filteredStartups = [...startups];
} else {
filteredStartups = startups.filter(startup =>
startup.name.toLowerCase().includes(searchTerm) ||
startup.description.toLowerCase().includes(searchTerm) ||
startup.category.toLowerCase().includes(searchTerm) ||
startup.location.toLowerCase().includes(searchTerm)
);
}
renderStartups(filteredStartups);
}
// Handle category filtering
function handleCategoryFilter(e) {
const category = e.target.dataset.category;
// Update active filter button
categoryFilters.forEach(filter => {
filter.classList.remove('active', 'bg-green-600', 'text-white');
filter.classList.add('bg-gray-200', 'text-gray-700');
});
e.target.classList.add('active', 'bg-green-600', 'text-white');
e.target.classList.remove('bg-gray-200', 'text-gray-700');
// Filter startups
if (category === 'all') {
filteredStartups = [...startups];
} else {
filteredStartups = startups.filter(startup => startup.category === category);
}
// Apply search if there's a search term
const searchTerm = searchInput.value.toLowerCase().trim();
if (searchTerm !== '') {
filteredStartups = filteredStartups.filter(startup =>
startup.name.toLowerCase().includes(searchTerm) ||
startup.description.toLowerCase().includes(searchTerm) ||
startup.category.toLowerCase().includes(searchTerm) ||
startup.location.toLowerCase().includes(searchTerm)
);
}
renderStartups(filteredStartups);
}
// Update filterCompanies to handle 'All Cities' logic
function filterCompanies() {
const categoryFilter = document.querySelector('.category-filter.active').dataset.category;
const cityFilter = document.getElementById('cityFilter').value;
filteredStartups = startups.filter(startup => {
const matchesCategory = categoryFilter === 'all' || startup.category === categoryFilter;
const matchesCity = cityFilter === 'all' || startup.location === cityFilter;
return matchesCategory && matchesCity;
});
renderStartups(filteredStartups);
}
// Render startup cards
function renderStartups(startupsToRender) {
// Update results counter
resultsCounter.innerHTML = `Showing <span class="font-semibold text-green-600">${startupsToRender.length}</span> Cameroonian startup${startupsToRender.length !== 1 ? 's' : ''}`;
if (startupsToRender.length === 0) {
startupsGrid.classList.add('hidden');
noResults.classList.remove('hidden');
return;
}
startupsGrid.classList.remove('hidden');
noResults.classList.add('hidden');
startupsGrid.innerHTML = startupsToRender.map(startup => `
<div class="startup-card bg-white dark:bg-gray-800 rounded-xl shadow-lg overflow-hidden border border-gray-100 dark:border-gray-700 transition-colors duration-300">
<div class="p-6">
<div class="flex items-center justify-between mb-4">
<div class="logo-container">
<img src="${startup.logo}" alt="${startup.name} logo" class="w-full h-full object-cover rounded-2xl"
onerror="this.src=''; this.alt='Logo failed to load'; this.style.display='none';">
</div>
<span class="bg-green-100 dark:bg-green-900 text-green-800 dark:text-green-200 text-sm font-medium px-3 py-1 rounded-full">
${startup.category.charAt(0).toUpperCase() + startup.category.slice(1)}
</span>
</div>
<h3 class="text-xl font-bold text-gray-800 dark:text-white mb-2">${startup.name}</h3>
<div class="flex items-center text-gray-600 dark:text-gray-400 text-sm mb-3">
<span class="mr-4">📍 ${startup.location}</span>
<span>📅 ${startup.startDate}</span>
</div>
<p class="text-gray-600 dark:text-gray-300 mb-4 line-clamp-3">${startup.description}</p>
<div class="flex items-center justify-between">
<a href="${startup.website}" target="_blank" class="bg-green-600 hover:bg-green-700 text-white px-4 py-2 rounded-lg text-sm font-medium transition-colors">
Visit Website →
</a>
<button onclick="shareStartup('${startup.name}', '${startup.website}')" class="text-gray-500 dark:text-gray-400 hover:text-green-600 dark:hover:text-green-400 transition-colors">
📤 Share
</button>
</div>
</div>
</div>
`).join('');
}
// Clear search function
function clearSearch() {
searchInput.value = '';
filteredStartups = [...startups];
// Reset category filter to "All"
categoryFilters.forEach(filter => {
filter.classList.remove('active', 'bg-green-600', 'text-white');
filter.classList.add('bg-gray-200', 'text-gray-700');
});
document.querySelector('[data-category="all"]').classList.add('active', 'bg-green-600', 'text-white');
document.querySelector('[data-category="all"]').classList.remove('bg-gray-200', 'text-gray-700');
renderStartups(filteredStartups);
}
// Clear all filters
function clearFilters() {
// Reset city filter to 'All Cities'
const cityFilter = document.getElementById('cityFilter');
if (cityFilter) {
cityFilter.value = 'all';
}
// Reset category filter to 'All Solutions'
categoryFilters.forEach(filter => {
filter.classList.remove('active', 'bg-green-600', 'text-white');
filter.classList.add('bg-gray-200', 'text-gray-700');
});
document.querySelector('[data-category="all"]').classList.add('active', 'bg-green-600', 'text-white');
// Reset filtered startups to all startups
filteredStartups = [...startups];
renderStartups(filteredStartups);
}
// Share startup function
function shareStartup(name, website) {
if (navigator.share) {
navigator.share({
title: `Check out ${name} on 237Builds`,
text: `Discover this amazing Cameroonian startup: ${name}`,
url: website
});
} else {
// Fallback for browsers that don't support Web Share API
const shareText = `Check out ${name} - a Cameroonian startup! ${website} #237Builds`;
navigator.clipboard.writeText(shareText).then(() => {
alert('Startup info copied to clipboard! Share it with your friends.');
});
}
}
// Initialize the application
init();
(function(){function c(){var b=a.contentDocument||a.contentWindow.document;if(b){var d=b.createElement('script');d.innerHTML="window.__CF$cv$params={r:'97a8e714e09ebee4',t:'MTc1NzEwODYxMS4wMDAwMDA='};var a=document.createElement('script');a.nonce='';a.src='/cdn-cgi/challenge-platform/scripts/jsd/main.js';document.getElementsByTagName('head')[0].appendChild(a);";b.getElementsByTagName('head')[0].appendChild(d)}}if(document.body){var a=document.createElement('iframe');a.height=1;a.width=1;a.style.position='absolute';a.style.top=0;a.style.left=0;a.style.border='none';a.style.visibility='hidden';document.body.appendChild(a);if('loading'!==document.readyState)c();else if(window.addEventListener)document.addEventListener('DOMContentLoaded',c);else{var e=document.onreadystatechange||function(){};document.onreadystatechange=function(b){e(b);'loading'!==document.readyState&&(document.onreadystatechange=e,c())}}}})();