bukopin-redemption-client-r.../src/common/stores/surf.js
Rifqy Zacky Ariadhy 1a000700e6 Initial commit
2019-01-02 18:39:53 +07:00

104 lines
2.1 KiB
JavaScript

import { observable, action, computed } from 'mobx';
export default class Surf {
@observable list = [];
@observable selected = {};
@observable istLoading = false;
@observable isSearching = false;
@observable filtered = [];
constructor(context) {
this.http = context.http;
this.context = context;
this.tagId= '4747ff80-6379-43eb-bf34-a4fbd733a593';
}
@action
getList(){
this.isLoading = true;
return this.http.get("posts/by_tag/"+this.tagId)
.then(res => {
this.list = res;
this.isLoading = false;
})
.catch(err => {
this.isLoading = false;
throw err;
})
}
@action
getDetail(id){
this.isLoading = true;
return this.http.get(`posts/${id}`)
.then(res => {
console.log(res,'ini res store')
this.selected = res;
this.isLoading = false;
})
.catch(err => {
this.isLoading = false;
throw err;
})
}
@action
post(data){
this.isLoading = true;
return this.http.post("posts", data)
.then(res => {
console.log('rest men',res);
this.isLoading = false;
this.getList();
return res;
})
.catch(err => {
console.log(err,'err men')
this.isLoading = false;
throw err;
})
}
@action
put(id,data){
this.isLoading = true;
return this.http.put(`posts/${id}`, data)
.then(res => {
this.isLoading = false;
this.getList();
return res;
})
.catch(err => {
this.isLoading = false;
throw err;
})
}
@action
delete(id){
this.isLoading = true;
return this.http.delete(`posts/${id}`)
.then(res => {
this.isLoading = false;
this.getList();
return res;
})
.catch(err => {
this.isLoading = false;
throw err;
})
}
filterItems = (query) => {
return this.list.filter((el) =>
el.title.toLowerCase().indexOf(query.toLowerCase()) > -1
);
}
@action
search(text){
this.filtered = this.filterItems(text);
}
}