let m = require('mithril') let _ = require('lodash') let post = require('./post') 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 }) .then(function(result) { category.current = {} if (result.status) { m.route.set('/kategori') } else { console.log(result) } }) }, entry: function() { return m.request({ method: 'POST', url: '/api/kategori', data: category.current, withCredentials: true }) .then(function(result) { if (result.status) { category.loadList() } else { console.log(result.status) } }) }, 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