let m = require('mithril') let _ = require('lodash') var user = { current: {}, loading: false, loadCurrent: function(id) { user.loading = true return m.request({ method: 'GET', url: '/api/users/' + id, withCredentials: true }) .then(function(result) { if (result.status == true) { user.current = result.data user.current.new_password = user.current.password } else { window.message = result.message } user.loading = false }) }, 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 } window.message = response.message }) }, logout: function() { return m.request({ method: 'GET', url: '/logout', withCredentials: true, headers: {client: 'api', 'x-query': window.location.search} }) .then(function(response) { if (response.status) { window.location = response.redirect_to } window.message = response.message }) }, register: function() { return m.request({ method: 'POST', url: '/post/' + window.location.search, data: user.current, withCredentials: true, headers: {client: 'api', 'x-query': window.location.search} }) .then(function(response) { if (response.status == true) { m.route.set(response.route_to) } window.message = response.message }) }, editProfile: function() { if (!_.isNil(user.current.old_password) && !_.isNil(user.current.re_new_password)) { if (_.isEqual(user.current.re_new_password, user.current.new_password)) { _.unset(user.current, 're_new_password') } else { window.message = 'Password baru tidak sama' m.redraw() return false } } else { _.unset(user.current, 'new_password') } return m.request({ method: 'POST', url: '/edit-profile/' + window.location.search, data: user.current, withCredentials: true, headers: {client: 'api', 'x-query': window.location.search} }) .then(function(response) { if (response.status == true) { user.logout() } window.message = response.message }) } } module.exports = user