You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

185 lines
6.3 KiB

  1. <app-users-form>
  2. <div class="sidebar">
  3. <div class="sidebar__inner">
  4. <div class="bar">
  5. <div class="bar__main">
  6. <span if={ !state.user._id }>
  7. Create User
  8. </span>
  9. <span if={ state.user._id }>
  10. Update User
  11. </span>
  12. </div>
  13. <div class="bar__end">
  14. <button id="sidebar-user-form-close" class="button button--transparent" type="button" onclick={ (event) => { handleClose(event) } }>
  15. <svg class="icon fill-text-contrast" aria-hidden="true">
  16. <use xlink:href="/symbol-defs.svg#icon-close"></use>
  17. </svg>
  18. </button>
  19. </div>
  20. </div>
  21. <div class="sidebar__body">
  22. <form id="app-users-form" novalidate>
  23. <div class="field-group">
  24. <label class="field-label">
  25. E-Mail
  26. <input name="email" type="text" value={ state.user.email } class="field-text" />
  27. </label>
  28. <field-error name="email"></field-error>
  29. </div>
  30. <div class="field-group">
  31. <label class="field-label">
  32. Display Name
  33. <input name="display_name" type="text" value={ state.user.display_name } class="field-text" />
  34. </label>
  35. <field-error name="display_name"></field-error>
  36. </div>
  37. <div class="field-group">
  38. <label class="field-label">
  39. Password
  40. <input name="password" type="password" class="field-text" />
  41. </label>
  42. <field-error name="password"></field-error>
  43. </div>
  44. <div class="field-group">
  45. <label class="field-label">
  46. <input name="roles[]" type="checkbox" class="field-choice" value="admin" />
  47. <svg class="icon field-choice__unchecked" aria-hidden="true">
  48. <use xlink:href="/symbol-defs.svg#icon-checkbox"></use>
  49. </svg>
  50. <svg class="icon field-choice__checked" aria-hidden="true">
  51. <use xlink:href="/symbol-defs.svg#icon-checkbox-checked"></use>
  52. </svg>
  53. Admin
  54. </label>
  55. <field-error name="roles"></field-error>
  56. </div>
  57. </form>
  58. </div>
  59. <div class="sidebar__footer">
  60. <button class="button m-bottom-0" type="submit" form="app-users-form">
  61. Save
  62. <svg class="icon fill-success p-left-3" aria-hidden="true">
  63. <use xlink:href="/symbol-defs.svg#icon-check"></use>
  64. </svg>
  65. </button>
  66. <button class="button m-bottom-0" type="submit" form="app-users-form" close>
  67. Save and Close
  68. <svg class="icon fill-success p-left-3" aria-hidden="true">
  69. <use xlink:href="/symbol-defs.svg#icon-arrow-right"></use>
  70. </svg>
  71. </button>
  72. </div>
  73. </div>
  74. </div>
  75. <script>
  76. import axios from 'axios'
  77. import * as riot from 'riot'
  78. import FormValidator from './../../FormValidator'
  79. import FieldError from './../field-error.riot'
  80. riot.register('field-error', FieldError)
  81. riot.mount('field-error')
  82. export default {
  83. state: {
  84. user: {
  85. }
  86. },
  87. /**
  88. *
  89. *
  90. */
  91. onMounted(props, state) {
  92. this.root.addEventListener('app-users-form-open', (event) => {
  93. this.$('.sidebar').classList.add('sidebar--open')
  94. // check for data, and if user is send add to state
  95. if (event.detail.data) {
  96. this.state.user = event.detail.data
  97. this.update()
  98. }
  99. })
  100. // create form validation
  101. const formValidation = new FormValidator('#app-users-form', {
  102. 'email': {
  103. 'length': {
  104. 'maximum': 255
  105. },
  106. 'email': true,
  107. 'presence': true
  108. },
  109. 'password': {
  110. 'length': {
  111. 'maximum': 64
  112. },
  113. 'presence': true
  114. }
  115. }, (event, data) => {
  116. this.handleSubmit(event, data)
  117. })
  118. },
  119. /**
  120. * close current form
  121. *
  122. *
  123. * @param {object} event
  124. *
  125. */
  126. handleClose(event)
  127. {
  128. event.preventDefault()
  129. this.$('.sidebar').classList.remove('sidebar--open')
  130. this.reset()
  131. },
  132. /**
  133. *
  134. *
  135. */
  136. handleSubmit(event, data) {
  137. let method = 'post'
  138. if (this.state.user && this.state.user._id) {
  139. method = 'put'
  140. }
  141. axios({
  142. method: method,
  143. url: '/api/users',
  144. data: data
  145. }).then((response) => {
  146. this.state.user = response.data.data
  147. this.$('#sidebar-user-form-close').click()
  148. this.update()
  149. })
  150. },
  151. /**
  152. *
  153. *
  154. */
  155. reset() {
  156. this.state.user = { }
  157. this.update()
  158. }
  159. }
  160. </script>
  161. </app-users-form>