RegisterForm.vue 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. <script>
  2. import Vue from 'vue'
  3. import { mapState, mapActions } from 'vuex'
  4. import { MA } from 'vuejs/api/ma-axios'
  5. import passwordStrength from 'check-password-strength'
  6. export default {
  7. name: "RegisterForm",
  8. data: () => ({
  9. form: null,
  10. mail: null,
  11. pass1: null,
  12. pass2: null,
  13. ps: ""
  14. }),
  15. computed: {
  16. ...mapState({
  17. registerMessage: state => state.User.registerMessage,
  18. }),
  19. psswd_class: function(){
  20. return this.ps.toLowerCase()
  21. },
  22. can_register: function() {
  23. if (this.ps === "Strong") {
  24. return 'can-register'
  25. }
  26. return ''
  27. }
  28. },
  29. methods: {
  30. ...mapActions({
  31. userRegister: 'User/userRegister'
  32. }),
  33. getRegisterForm(){
  34. // Form through ajax is provided by materio_user drupal custom module
  35. // vuejs attributes a inserted by form alter in same module
  36. MA.get(`/materio_user/register_form`)
  37. .then(({data}) => {
  38. console.log("getRegisterForm data", data)
  39. this.form = Vue.compile(data.rendered)
  40. this.$options.staticRenderFns = [];
  41. this._staticTrees = [];
  42. this.form.staticRenderFns.map(fn => (this.$options.staticRenderFns.push(fn)));
  43. this.initFormBehaviours()
  44. })
  45. .catch((error) => {
  46. console.warn('Issue with getRegisterForm', error)
  47. })
  48. },
  49. initFormBehaviours(){
  50. Drupal.attachBehaviors(this.$el)
  51. this.checkSubmitEnabled()
  52. },
  53. checkSubmitEnabled () {
  54. console.log("checkSubmitEnabled", this)
  55. if (this.ps === 'Strong') {
  56. this.$refs.register.disabled = false
  57. } else {
  58. this.$refs.register.disabled = true
  59. }
  60. },
  61. register () {
  62. console.log('register', this.mail, this.pass1, this.pass2)
  63. // TODO: check for identical password
  64. // TODO: check for valide email
  65. // TODO: check for unique email
  66. if (this.pass1 === this.pass2) {
  67. this.userRegister({
  68. name: this.mail,
  69. mail: this.mail,
  70. pass: this.pass1
  71. }).then( () => {
  72. console.log('registered from register component')
  73. this.$emit('onRegistered')
  74. }
  75. ).catch((error) => {
  76. console.warn('Issue with register from registerform component', error)
  77. Promise.reject(error)
  78. })
  79. }
  80. }
  81. },
  82. beforeMount () {
  83. if (!this.form) {
  84. this.getRegisterForm()
  85. }
  86. },
  87. mounted(){
  88. // console.log('LoginBlock mounted')
  89. if (this.form) {
  90. this.initForm()
  91. }
  92. },
  93. render(h) {
  94. // console.log('LoginBlock render')
  95. if (!this.form) {
  96. // console.log('LoginBlock render NAN')
  97. return h('span', 'Loading ...')
  98. } else {
  99. // console.log('LoginBlock render template')
  100. return this.form.render.call(this)
  101. }
  102. },
  103. watch: {
  104. pass1: function(n, o){
  105. if (n) {
  106. this.ps = passwordStrength(n).value
  107. console.log('watch pass1 n', n, 'ps :', this.ps)
  108. this.checkSubmitEnabled()
  109. }
  110. }
  111. }
  112. }
  113. </script>
  114. <style lang="scss" scoped>
  115. .form-type-email,
  116. .form-type-password-confirm{
  117. // display:inline-block;
  118. max-width:30%;
  119. margin:0;
  120. }
  121. .form-type-password input{
  122. &.weak {
  123. border-width: 2px;
  124. border-color: red!important;
  125. &:focus{
  126. outline:none;
  127. }
  128. }
  129. &.medium {
  130. border-width: 2px;
  131. border-color: orange!important;
  132. &:focus{
  133. outline:none;
  134. }
  135. }
  136. &.strong {
  137. border-width: 2px;
  138. border-color: green!important;
  139. &:focus{
  140. outline:none;
  141. }
  142. }
  143. }
  144. input{
  145. box-sizing:content-box;
  146. max-width:100%;
  147. }
  148. // input#edit-submit{
  149. //
  150. // }
  151. label,
  152. div.description{
  153. display:none;
  154. }
  155. </style>