Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // const Body = require('./components/body')
- const R = require('ramda')
- const h = require('snabbdom/h')
- const F = require('../vendor/fractalEngine.min')
- const login = 'http://localhost:1337/auth/login'
- module.exports = F.defineModule({
- init: ()=>({
- log: false,
- token: false,
- email: '',
- password: '',
- user: {},
- message: false,
- }),
- inputs:{
- addEmail:(ctx,Action,email)=>ctx.action$(Action.AddEmail(email)),
- addPassword:(ctx,Action,pass)=>ctx.action$(Action.AddPassword(pass)),
- login:(ctx,Action,m)=>ctx.action$(Action.Login()),
- tokenFetched:(ctx,Action,res)=>ctx.action$(Action.TokenFetched(res)),
- },
- outputs:{},
- Action:{
- AddEmail: [String],
- AddPassword: [String],
- Login: [],
- TokenFetched: [Object],
- },
- update:{
- AddEmail: (email,m)=>R.evolve({email:R.always(email)},m),
- AddPassword: (pass,m)=>R.evolve({password:R.always(pass)},m),
- Login: (m)=>R.evolve({log:R.T},m),
- TokenFetched: (res,m)=>{
- if(res.authenticated==true && res.token){
- return R.evolve({token:R.always(res.token),log:R.F,user:R.always(res.user),message:R.F},m)
- }else{
- return R.evolve({log:R.F,message:R.always(res.err.error)},m)
- }
- },
- },
- interfaces:{
- view:(ctx,i,m)=>{
- document.querySelector('body').className = 'hold-transition login-page'
- return h('div.login-box',[
- h('div.login-logo',[
- h('b','Prestá'),
- 'MOS',
- ]),
- h('div.login-box-body',[
- h('p.login-box-msg',(m.message!=false)? m.message : 'Inicia sesion'),
- h('form',{on:{submit: preventDefault}},[
- h('div.form-group.has-feedback',[
- h('input.form-control',{on:{change:(e)=>i.addEmail(e.target.value)},attrs:{type:'string',name:'email',placeholder:'Email',value:m.email}}),
- h('span.glyphicon.glyphicon-envelope.form-control-feedback')
- ]),
- h('div.form-group has-feedback',[
- h('input.form-control',{on:{change:(e)=>i.addPassword(e.target.value)},attrs:{type:'password',name:'password',placeholder:'Password',value:m.password}}),
- h('span.glyphicon.glyphicon-lock.form-control-feedback')
- ]),
- h('div.row',[
- h('div.col-xs-4',[
- h('button.btn.btn-primary.btn-block.btn-flat',{on:{click:i.login}},'Ingresar'),
- ]),
- ]),
- ])
- ])
- ])
- },
- fetch:(ctx,i,m)=>{
- return {
- data:{
- url:login,
- options:{
- method:'POST',
- body: JSON.stringify({
- email: m.email,
- password: m.password,
- })
- },
- active: m.log,
- response: res=>res.json(),
- success: i.tokenFetched,
- denied: (i)=>console.log(i,'denied'),
- error: (i)=>console.log(i,'error'),
- netError: (i)=>console.log(i,'netError'),
- }
- }
- },
- },
- })
- const preventDefault = R.invoker(0, 'preventDefault')
Add Comment
Please, Sign In to add comment