Untitled

mail@pastecode.io avatar
unknown
plain_text
a year ago
1.6 kB
3
Indexable
Never
/* eslint-disable react-func/max-lines-per-function */
export function mealsOrDrinks() {
  let END_POINT = '';
  const END_POINT_MEALS = 'https://www.themealdb.com/api/json/v1/1/filter.php?';
  const END_POINT_DRINKS = 'https://www.thecocktaildb.com/api/json/v1/1/filter.php?';

  switch (window.location.pathname) {
    case '/meals':
      END_POINT = END_POINT_MEALS;
      break;
    case '/drinks':
      END_POINT = END_POINT_DRINKS;
      break;
    default:
      return false;
  }

  async function searchIngredient(ingrediente: string) {
    const response = await fetch(`${END_POINT}i=${ingrediente}`);
    const data = await response.json();
    return data.meals;
  }

  async function searchFirstLetter(primeiraletra: string) {
    const response = await fetch(`${END_POINT}f=${primeiraletra}`);
    const data = await response.json();
    return data.meals;
  }

  async function searchName(nome: string) {
    const response = await fetch(`${END_POINT}s=${nome}`);
    const data = await response.json();
    return data.meals;
  }

  async function searchDrinkIngredient(ingrediente: string) {
    const response = await fetch(`${END_POINT}i=${ingrediente}`);
    const data = await response.json();
    return data.cocktails;
  }

  async function searchDrinkFirstLetter(primeiraletra: string) {
    const response = await fetch(`${END_POINT}f=${primeiraletra}`);
    const data = await response.json();
    return data.cocktails;
  }

  async function searchDrinkName(nome: string) {
    const response = await fetch(`${END_POINT_DRINKS}s=${nome}`);
    const data = await response.json();
    return data.cocktails;
  }
}