import { observable, action, computed } from 'mobx';

export default class EntitiesStore {

  @observable entities = [];
  @observable isLoading = false;

  constructor(context) {
    this.http = context.http;
    this.context = context;
  }

  @action
  getAll() {
    this.isLoading = true;
    return this.http.get("entities")
      .then(res => {
        this.isLoading = false;
        this.entities = res.data;
        console.log(res,"tes");
      })
      .catch(err => {
        this.isLoading = false;
        throw err;
      })
  }

  @action
  create(data) {
    this.isLoading = true;
    return this.http.post("entities", data)
      .then(res => {
        this.isLoading = false;
        this.getAll();
        console.log(data);
      })
      .catch(err => {
        this.isLoading = false;
        this.getAll();
      });
  }

  // @action
  // post(data) {
  //   this.isLoading = true;
  //   return this.http.post('entities', data)
  //     .then(res => {
  //       this.isLoading = false;
  //       this.getAll()
  //     })
  //     .catch(err => {
  //       this.isLoading = false;
  //       throw err;
  //     })
  //   console.log(data);
  // }
}
