export const rootUrl = 'http://smktesting.herokuapp.com/';
const apiUrl = `${rootUrl}api/`;

const headers = new Headers();
headers.append('Content-Type', 'application/json');

class Api {
  static getProducts() {
    const option = {
      method: 'GET',
    };

    return fetch(`${apiUrl}products/`, option)
      .then(response => response.json());
  }

  static getProductsReviews(id) {
    const option = {
      method: 'GET',
    };

    return fetch(`${apiUrl}reviews/${id}`, option)
      .then(response => response.json());
  }

  static signIn(username, password) {
    const body = JSON.stringify({
      username,
      password
    });

    const option = {
      method: 'POST',
      headers,
      body
    };

    return fetch(`${apiUrl}login/`, option)
      .then(response => response.json());
  }

  static 
(username, password) {
    const body = JSON.stringify({
      username,
      password
    });

    const option = {
      method: 'POST',
      headers,
      body
    };

    return fetch(`${apiUrl}register/`, option)
      .then(response => response.json());
  }

  static signOut() {
    localStorage.clear();
    return true;
  }

  static setDataToLocalStorage(token, username) {
    localStorage.setItem('token', token);
    localStorage.setItem('username', username);
  }

  static loggedIn() {
    return !!localStorage.token;
  }

  static getUsername() {
    return localStorage.getItem('username');
  }
}

export default Api;

signUp