API Rapid

by Lucaskazama

HTML

<h1>Pesquisa de Vagas de Web Developer</h1>
  
  <button id="searchButton">Buscar Vagas</button>
  <button id="getLocationButton">Obter Localização</button>
  
  <table id="resultsTable" style="display: none;">
    <thead>
      <tr>
        <th>Empresa</th>
        <th>Localização</th>
        <th>Nome da Vaga</th>
        <th>Descrição</th>
        <th>Link</th>
      </tr>
    </thead>
    <tbody>
      <!-- Resultados serão inseridos aqui -->
    </tbody>
  </table>

CSS

table {
      width: 100%;
      border-collapse: collapse;
    }
    table, th, td {
      border: 1px solid black;
    }
    th, td {
      padding: 8px;
      text-align: left;
    }
    button {
      padding: 10px 20px;
      margin: 20px 0;
      background-color: #4CAF50;
      color: white;
      border: none;
      cursor: pointer;
    }
    button:hover {
      background-color: #45a049;
    }

JavaScript

const defaultQuery = 'Fortaleza';  // Query padrão
    let userLocation = '';  // Localização do usuário (país)

    const apiKey = '77f12372a44349299fdf0519b221f83f'; // Sua chave real da OpenCage

    // Função para obter a localização do usuário
    function getLocation() {
      if (navigator.geolocation) {
        navigator.geolocation.getCurrentPosition((position) => {
          const { latitude, longitude } = position.coords;

          console.log(`Latitude: ${latitude}, Longitude: ${longitude}`);

          // Requisição para a OpenCage API usando a chave
          fetch(`https://api.opencagedata.com/geocode/v1/json?q=${latitude}+${longitude}&key=${apiKey}`)
            .then(response => response.json())
            .then(data => {
              // Verifica se a resposta contém resultados
              if (data.results && data.results.length > 0) {
                const country = data.results[0].components.country || 'Desconhecido';
                userLocation = country; // Atualiza a variável com o país

                console.log(`País: ${country}`);
              } else {
                console.log('Não foi possível determinar a localização.');
              }
            })
            .catch(error => {
              console.error('Erro ao obter dados de geolocalização:', error);
            });
        }, (error) => {
          console.error('Erro ao obter localização:', error);
        });
      } else {
        console.log('Geolocalização não é suportada neste navegador.');
      }
    }

    // Ação para obter a localização ao clicar no botão
    document.getElementById('getLocationButton').addEventListener('click', getLocation);

    // Função assíncrona para buscar as vagas
    async function fetchJobs() {
      if (!userLocation) {
        console.log('Localização do usuário não definida. Não é possível buscar vagas.');
        return;
      }

      const query = defaultQuery;  // Pode modificar para outras consultas no futuro

      //...