84 lines
2.4 KiB
JavaScript
84 lines
2.4 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) {
|
|
category.current = {}
|
|
if (result.status == true) {
|
|
m.route.set('/kategori')
|
|
} else if (result.status == 401) {
|
|
user.logout();
|
|
}
|
|
window.message = result.message
|
|
})
|
|
},
|
|
entry: function() {
|
|
return m.request({
|
|
method: 'POST',
|
|
url: '/api/kategori',
|
|
data: category.current,
|
|
withCredentials: true,
|
|
headers: {'x-query': window.location.search}
|
|
})
|
|
.then(function(result) {
|
|
if (result.status == true) {
|
|
category.loadList()
|
|
} else if (result.status == 401) {
|
|
user.logout()
|
|
}
|
|
window.message = result.message
|
|
})
|
|
},
|
|
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()
|
|
} else {
|
|
category.current = {}
|
|
m.redraw()
|
|
window.message = 'Kategori memiliki pengumuman aktif'
|
|
}
|
|
}
|
|
}
|
|
|
|
module.exports = category
|