Mithril as main method

This commit is contained in:
2017-10-25 12:08:41 +07:00
parent 6040809710
commit d1d5ee1b0c
157 changed files with 19593 additions and 716 deletions

View File

@@ -0,0 +1,74 @@
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

View File

@@ -0,0 +1,20 @@
let m = require('mithril')
let _ = require('lodash')
const landingPost = function(root) {
m.request({
method: 'GET',
url: '/api/pengumuman/?status=1',
background: false
})
.then(function(res) {
let images = require('../components/images')
if (!_.isEqual(res['data'], images.list)) {
images.list = res['data']
m.mount(root, null)
m.mount(root, images)
}
})
}
module.exports = landingPost

View File

@@ -1,18 +1,110 @@
let m = require('mithril')
let fecha = require('fecha')
let user = require('./user')
let cookie = require('../others/cookie')
var post = {
loading: false,
list: [],
current: {},
curStatus: 3,
interval: 10000,
loadList: function() {
post.loading = true
return m.request({
method: 'GET',
url: '/api/posts/' + post.curStatus,
url: '/api/pengumuman/?status=' + post.curStatus,
withCredentials: true
})
.then(function(result) {
post.list = result.data
post.list.map(function(p) {
post.validateStatus(p.id)
})
post.loading = false
})
},
loadCurrent: function(id) {
post.loading = true
return m.request({
method: 'GET',
url: '/api/pengumuman/' + id,
withCredentials: true
})
.then(function(result) {
post.current = result.data
post.loading = false
})
},
upload: function() {
post.loading = true
return m.request({
method: 'POST',
url: '/posts/post/' + window.location.search,
data: post.current,
withCredentials: true,
headers: {client: 'api', 'x-query': window.location.search}
})
.then(function(response) {
if (response.status) {
m.route.set(response.route_to)
} else {
console.log(response);
}
post.current = {}
post.loading = false
})
},
save: function() {
post.loading = true
return m.request({
method: 'POST',
url: '/posts/put/' + window.location.search,
data: post.current,
withCredentials: true,
headers: {client: 'api'}
})
.then(function(response) {
post.current = {}
if (response.status) {
m.route.set(response.route_to)
} else {
console.log(response);
}
post.loading = false;
})
},
toggleStatus: function(id) {
post.current = _.find(post.list, function(o) { return o.id == id })
if (post.current.status == 2) {
post.current.status = 1
post.current.valid_at = fecha.format(new Date(), 'YYYY/MM/DD')
} else if (post.current.status == 1) {
post.current.status = 0
}
post.save()
// console.log(post.current);
},
validateStatus: function(id) {
post.current = _.find(post.list, function(o) { return o.id == id })
// console.log(new Date(post.current.valid_at) > new Date());
if (new Date(post.current.valid_at) > new Date()) {
status = 2
// console.log(post.current.id, 'soon');
} else if (new Date(post.current.valid_at) <= new Date()) {
if (new Date(post.current.expired_at) < new Date()) {
status = 0
// console.log(post.current.id, 'expired');
} else {
status = 1
// console.log(post.current.id, 'active');
}
}
if (post.current.status != status) {
post.current.status = status
post.save()
}
// console.log(post.current);
}
}

53
assets/js/models/user.js Normal file
View File

@@ -0,0 +1,53 @@
let m = require('mithril')
var user = {
current: {},
login: function() {
return m.request({
method: 'POST',
url: '/login',
data: user.current,
withCredentials: true,
headers: {client: 'api'}
})
.then(function(response) {
if (response.status == true) {
window.location = response.redirect_to
} else {
console.log(response.status);
}
})
},
logout: function() {
return m.request({
method: 'GET',
url: '/logout',
withCredentials: true,
headers: {client: 'api', 'x-query': window.location.search}
})
.then(function(response) {
console.log(response);
if (response.status) {
window.location = response.redirect_to
}
})
},
register: function() {
return m.request({
method: 'POST',
url: '/post',
data: user.current,
withCredentials: true,
headers: {client: 'api'}
})
.then(function(response) {
if (response.status == true) {
m.route.set(response.route_to)
} else {
console.log(response);
}
})
}
}
module.exports = user