lepisi-pengumuman/assets/js/models/category.js

80 lines
2.2 KiB
JavaScript

let m = require('mithril')
let _ = require('lodash')
let post = require('./post')
let user = require('./user')
var category = {
loading: false,
list: [],
current: {},
loadList: function() {
category.loading = true
category.current = {}
return m.request({
method: 'GET',
url: '/api/kategori',
withCredentials: true
})
.then(function(results) {
category.list = results.data
category.loading = false
})
},
loadCurrent: function(id) {
category.loading = true
return m.request({
method: 'GET',
url: '/api/kategori/' + id,
withCredentials: true
})
.then(function(result) {
category.current = result.data
category.loading = false
})
},
save: function() {
return m.request({
method: 'PUT',
url: '/api/kategori',
data: category.current,
withCredentials: true,
headers: {'x-query': window.location.search}
})
.then(function(result) {
console.log(result)
category.current = {}
if (result.status == true) {
m.route.set('/kategori')
} else if (result.status == 401) {
user.logout();
}
})
},
entry: function() {
return m.request({
method: 'POST',
url: '/api/kategori',
data: category.current,
withCredentials: true,
headers: {'x-query': window.location.search}
})
.then(function(result) {
console.log(result);
if (result.status == true) {
category.loadList()
} else if (result.status == 401) {
user.logout()
}
})
},
toggleStatus: function(id){
category.current = _.find(category.list, function(o) { return o.id == id })
if (category.current.posts == 0) {
category.current.status == 1 ? category.current.status = 0 : category.current.status = 1
}
category.save()
}
}
module.exports = category