Guest User

Untitled

a guest
Mar 10th, 2016
30
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.06 KB | None | 0 0
  1. // const Body = require('./components/body')
  2. const R = require('ramda')
  3. const h = require('snabbdom/h')
  4. const F = require('../vendor/fractalEngine.min')
  5. const login = 'http://localhost:1337/auth/login'
  6.  
  7.  
  8. module.exports = F.defineModule({
  9. init: ()=>({
  10. log: false,
  11. token: false,
  12. email: '',
  13. password: '',
  14. user: {},
  15. message: false,
  16. }),
  17. inputs:{
  18. addEmail:(ctx,Action,email)=>ctx.action$(Action.AddEmail(email)),
  19. addPassword:(ctx,Action,pass)=>ctx.action$(Action.AddPassword(pass)),
  20. login:(ctx,Action,m)=>ctx.action$(Action.Login()),
  21. tokenFetched:(ctx,Action,res)=>ctx.action$(Action.TokenFetched(res)),
  22. },
  23. outputs:{},
  24. Action:{
  25. AddEmail: [String],
  26. AddPassword: [String],
  27. Login: [],
  28. TokenFetched: [Object],
  29. },
  30. update:{
  31. AddEmail: (email,m)=>R.evolve({email:R.always(email)},m),
  32. AddPassword: (pass,m)=>R.evolve({password:R.always(pass)},m),
  33. Login: (m)=>R.evolve({log:R.T},m),
  34. TokenFetched: (res,m)=>{
  35. if(res.authenticated==true && res.token){
  36. return R.evolve({token:R.always(res.token),log:R.F,user:R.always(res.user),message:R.F},m)
  37. }else{
  38. return R.evolve({log:R.F,message:R.always(res.err.error)},m)
  39. }
  40. },
  41. },
  42. interfaces:{
  43. view:(ctx,i,m)=>{
  44. document.querySelector('body').className = 'hold-transition login-page'
  45. return h('div.login-box',[
  46. h('div.login-logo',[
  47. h('b','Prestá'),
  48. 'MOS',
  49. ]),
  50. h('div.login-box-body',[
  51. h('p.login-box-msg',(m.message!=false)? m.message : 'Inicia sesion'),
  52. h('form',{on:{submit: preventDefault}},[
  53. h('div.form-group.has-feedback',[
  54. h('input.form-control',{on:{change:(e)=>i.addEmail(e.target.value)},attrs:{type:'string',name:'email',placeholder:'Email',value:m.email}}),
  55. h('span.glyphicon.glyphicon-envelope.form-control-feedback')
  56. ]),
  57. h('div.form-group has-feedback',[
  58. h('input.form-control',{on:{change:(e)=>i.addPassword(e.target.value)},attrs:{type:'password',name:'password',placeholder:'Password',value:m.password}}),
  59. h('span.glyphicon.glyphicon-lock.form-control-feedback')
  60. ]),
  61. h('div.row',[
  62. h('div.col-xs-4',[
  63. h('button.btn.btn-primary.btn-block.btn-flat',{on:{click:i.login}},'Ingresar'),
  64. ]),
  65. ]),
  66. ])
  67. ])
  68. ])
  69. },
  70. fetch:(ctx,i,m)=>{
  71. return {
  72. data:{
  73. url:login,
  74. options:{
  75. method:'POST',
  76. body: JSON.stringify({
  77. email: m.email,
  78. password: m.password,
  79. })
  80. },
  81. active: m.log,
  82. response: res=>res.json(),
  83. success: i.tokenFetched,
  84. denied: (i)=>console.log(i,'denied'),
  85. error: (i)=>console.log(i,'error'),
  86. netError: (i)=>console.log(i,'netError'),
  87. }
  88. }
  89. },
  90. },
  91. })
  92.  
  93. const preventDefault = R.invoker(0, 'preventDefault')
Add Comment
Please, Sign In to add comment