CodeAntidote commited on
Commit
9015f75
·
verified ·
1 Parent(s): a41d86f

searchJobs Function

Browse files
Files changed (1) hide show
  1. app.js +198 -198
app.js CHANGED
@@ -125,253 +125,253 @@ document.addEventListener("DOMContentLoaded", function () {
125
  }
126
  );
127
 
128
- async function searchJobs() {
129
- const keyword =
130
- keywordInput.value.trim();
131
 
132
- const location =
133
- locationInput.value.trim();
134
 
135
- const mode =
136
- jobSource
137
- ? jobSource.value
138
- : "auto";
139
 
140
- if (!keyword) {
141
- alert(
142
- "Enter a job title or keyword."
143
- );
144
 
145
- keywordInput.focus();
146
 
147
- return;
148
- }
 
149
 
150
- if (!location) {
151
- alert(
152
- "Enter a location, for example San Francisco, CA, Utah, United States, Berlin, Germany, or Remote."
153
- );
154
 
155
- locationInput.focus();
 
156
 
157
- return;
158
- }
159
 
160
- searchButton.disabled =
161
- true;
162
 
163
- searchButton.textContent =
164
- "Searching...";
 
165
 
166
- resultsBox.innerHTML = `
167
- <div class="empty-state">
168
 
169
- <div>
 
170
 
171
- <strong>
172
- Searching live jobs...
173
- </strong>
174
 
175
- <br><br>
 
176
 
177
- ${escapeHTML(keyword)}
178
 
179
- <br>
 
180
 
181
- ${escapeHTML(location)}
182
 
183
- </div>
184
 
185
- </div>
186
- `;
 
187
 
188
- try {
189
- const requests =
190
- [];
191
 
192
- const remoteSearch =
193
- isRemoteQuery(location) ||
194
- mode === "remote";
195
 
196
- const broadUS =
197
- isBroadUS(location) ||
198
- mode === "usa";
199
 
200
- const broadEurope =
201
- isBroadEurope(location) ||
202
- mode === "europe";
203
 
204
- requests.push(
205
- fetchMuseJobs(
206
- location,
207
- remoteSearch
208
- )
 
 
 
 
 
 
 
 
209
  );
210
 
211
- if (
212
- remoteSearch ||
213
- broadUS ||
214
- broadEurope
215
- ) {
216
- const geo =
217
- broadUS
218
- ? "usa"
219
- : broadEurope
220
- ? "europe"
221
- : "";
222
-
223
- requests.push(
224
- fetchJobicyJobs(
225
- keyword,
226
- geo
227
- )
228
- );
229
- }
230
 
231
- if (
232
- remoteSearch ||
233
- broadUS
234
- ) {
235
- requests.push(
236
- fetchRemoteOKJobs()
237
- );
238
- }
239
 
240
- const settled =
241
- await Promise.allSettled(
242
- requests
243
- );
244
 
245
- let jobs =
246
- [];
 
 
247
 
248
- for (
249
- const result
250
- of settled
251
- ) {
252
- if (
253
- result.status ===
254
- "fulfilled"
255
- ) {
256
- jobs =
257
- jobs.concat(
258
- result.value
259
- );
260
- } else {
261
- console.warn(
262
- "One job source failed:",
263
- result.reason
264
- );
265
  }
266
- }
267
 
268
- jobs =
269
- dedupeJobs(jobs)
270
- .map(
271
- function (job) {
272
- return {
273
- ...job,
274
-
275
- roleScore:
276
- calculateRoleScore(
277
- job,
278
- keyword
279
- ),
280
-
281
- locationScore:
282
- calculateLocationScore(
283
- job,
284
- location
285
- )
286
- };
287
- }
288
- )
289
- .filter(
290
- function (job) {
291
- return (
292
- job.roleScore > 0
293
- );
294
- }
295
- )
296
- .filter(
297
- function (job) {
298
- return locationMatches(
299
- job,
300
- location
301
- );
302
- }
303
- )
304
- .sort(
305
- function (a, b) {
306
- const scoreA =
307
- (
308
- a.roleScore *
309
- 4
310
- ) +
311
- a.locationScore;
312
 
313
- const scoreB =
314
- (
315
- b.roleScore *
316
- 4
317
- ) +
318
- b.locationScore;
319
-
320
- return (
321
- scoreB -
322
- scoreA
323
- );
324
- }
325
- );
326
 
327
- currentMatches =
328
- jobs.slice(
329
- 0,
330
- 40
331
- );
332
 
333
- renderJobs(
334
- keyword,
335
- location
 
 
336
  );
 
337
 
338
- } catch (error) {
339
- console.error(
340
- "JobAlign search error:",
341
- error
 
 
 
 
 
 
342
  );
 
343
 
344
- resultsBox.innerHTML = `
345
- <div
346
- class="empty-state"
347
- style="color:#ff7777;"
348
- >
349
 
350
- <div>
 
 
 
351
 
352
- <strong>
353
- Job search failed.
354
- </strong>
355
 
356
- <br><br>
 
 
357
 
358
- ${escapeHTML(
359
- error.message
360
- )}
361
 
362
- </div>
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
363
 
364
  </div>
365
- `;
366
 
367
- } finally {
368
- searchButton.disabled =
369
- false;
370
 
371
- searchButton.textContent =
372
- "Search Jobs";
373
- }
 
 
 
 
 
 
374
  }
 
375
 
376
  async function fetchMuseJobs(
377
  location,
 
125
  }
126
  );
127
 
128
+ async function searchJobs() {
 
 
129
 
130
+ const keyword =
131
+ keywordInput.value.trim();
132
 
133
+ const location =
134
+ locationInput.value.trim();
 
 
135
 
 
 
 
 
136
 
137
+ if (!keyword) {
138
 
139
+ alert(
140
+ "Enter a job title or keyword."
141
+ );
142
 
143
+ keywordInput.focus();
 
 
 
144
 
145
+ return;
146
+ }
147
 
 
 
148
 
149
+ if (!location) {
 
150
 
151
+ alert(
152
+ "Enter a location such as San Jose, CA, San Francisco, CA, Utah, Texas, Germany, or Remote."
153
+ );
154
 
155
+ locationInput.focus();
 
156
 
157
+ return;
158
+ }
159
 
 
 
 
160
 
161
+ searchButton.disabled =
162
+ true;
163
 
 
164
 
165
+ searchButton.textContent =
166
+ "Searching...";
167
 
 
168
 
169
+ resultsBox.innerHTML = `
170
 
171
+ <div class="empty-state">
172
+
173
+ <div>
174
 
175
+ <strong>
176
+ Searching live jobs...
177
+ </strong>
178
 
179
+ <br><br>
 
 
180
 
181
+ ${escapeHTML(keyword)}
 
 
182
 
183
+ <br>
 
 
184
 
185
+ ${escapeHTML(location)}
186
+
187
+ </div>
188
+
189
+ </div>
190
+ `;
191
+
192
+
193
+ try {
194
+
195
+ const endpoint =
196
+ new URL(
197
+ "https://jobalign-adzuna-proxy.darbymjohn.workers.dev/search"
198
  );
199
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
200
 
201
+ endpoint.searchParams.set(
202
+ "q",
203
+ keyword
204
+ );
 
 
 
 
205
 
 
 
 
 
206
 
207
+ endpoint.searchParams.set(
208
+ "location",
209
+ location
210
+ );
211
 
212
+
213
+ const response =
214
+ await fetch(
215
+ endpoint.toString(),
216
+ {
217
+ method:
218
+ "GET",
219
+
220
+ headers: {
221
+ "Accept":
222
+ "application/json"
223
+ }
 
 
 
 
 
224
  }
225
+ );
226
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
227
 
228
+ const data =
229
+ await response.json();
 
 
 
 
 
 
 
 
 
 
 
230
 
 
 
 
 
 
231
 
232
+ if (!response.ok) {
233
+
234
+ throw new Error(
235
+ data.error ||
236
+ "Job search failed."
237
  );
238
+ }
239
 
240
+
241
+ if (
242
+ !data.ok ||
243
+ !Array.isArray(
244
+ data.jobs
245
+ )
246
+ ) {
247
+
248
+ throw new Error(
249
+ "The job service returned an invalid response."
250
  );
251
+ }
252
 
 
 
 
 
 
253
 
254
+ currentMatches =
255
+ data.jobs
256
+ .map(
257
+ function (job) {
258
 
259
+ return {
 
 
260
 
261
+ title:
262
+ job.title ||
263
+ "",
264
 
265
+ company_name:
266
+ job.company ||
267
+ "Company not listed",
268
 
269
+ location:
270
+ job.location ||
271
+ location,
272
+
273
+ description:
274
+ job.description ||
275
+ "",
276
+
277
+ url:
278
+ job.url ||
279
+ "",
280
+
281
+ remote:
282
+ normalizeSimple(
283
+ job.location ||
284
+ ""
285
+ ).includes(
286
+ "remote"
287
+ ),
288
+
289
+ tags:
290
+ [
291
+ job.category,
292
+ job.contractType,
293
+ job.contractTime
294
+ ].filter(
295
+ Boolean
296
+ ),
297
+
298
+ source:
299
+ "Adzuna",
300
+
301
+ salaryMin:
302
+ job.salaryMin,
303
+
304
+ salaryMax:
305
+ job.salaryMax,
306
+
307
+ created:
308
+ job.created
309
+ };
310
+ }
311
+ );
312
+
313
+
314
+ currentMatches =
315
+ currentMatches.filter(
316
+ function (job) {
317
+
318
+ return calculateRoleScore(
319
+ job,
320
+ keyword
321
+ ) > 0;
322
+ }
323
+ );
324
+
325
+
326
+ renderJobs(
327
+ keyword,
328
+ location
329
+ );
330
+
331
+
332
+ } catch (error) {
333
+
334
+ console.error(
335
+ "JobAlign search error:",
336
+ error
337
+ );
338
+
339
+
340
+ resultsBox.innerHTML = `
341
+
342
+ <div
343
+ class="empty-state"
344
+ style="color:#ff7777;"
345
+ >
346
+
347
+ <div>
348
+
349
+ <strong>
350
+ Job search failed.
351
+ </strong>
352
+
353
+ <br><br>
354
+
355
+ ${escapeHTML(
356
+ error.message
357
+ )}
358
 
359
  </div>
 
360
 
361
+ </div>
362
+ `;
 
363
 
364
+
365
+ } finally {
366
+
367
+ searchButton.disabled =
368
+ false;
369
+
370
+
371
+ searchButton.textContent =
372
+ "Search Jobs";
373
  }
374
+ }
375
 
376
  async function fetchMuseJobs(
377
  location,