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.

203 lines
6.9 KiB

3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
  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.state.formValidation.setConstraits({
  98. 'email': {
  99. 'length': {
  100. 'maximum': 255
  101. },
  102. 'email': true,
  103. 'presence': true
  104. }
  105. })
  106. this.update()
  107. }
  108. })
  109. // create form validation
  110. this.state.formValidation = new FormValidator('#app-users-form', {
  111. 'email': {
  112. 'length': {
  113. 'maximum': 255
  114. },
  115. 'email': true,
  116. 'presence': true
  117. },
  118. 'password': {
  119. 'length': {
  120. 'maximum': 64
  121. },
  122. 'presence': true
  123. }
  124. }, (event, data) => {
  125. this.handleSubmit(event, data)
  126. })
  127. },
  128. /**
  129. * close current form
  130. *
  131. *
  132. * @param {object} event
  133. *
  134. */
  135. handleClose(event)
  136. {
  137. event.preventDefault()
  138. this.$('.sidebar').classList.remove('sidebar--open')
  139. this.reset()
  140. },
  141. /**
  142. *
  143. *
  144. */
  145. handleSubmit(event, data) {
  146. let method = 'post'
  147. let url = '/api/users'
  148. // user is set and has id, send as put with id
  149. if (this.state.user && this.state.user._id) {
  150. method = 'put'
  151. url += '/' + this.state.user._id
  152. }
  153. axios({
  154. method: method,
  155. url: url,
  156. data: data
  157. }).then((response) => {
  158. this.state.user = response.data.data
  159. // check if submit has close-attribute
  160. if (event.submitter.attributes.close) {
  161. this.$('#sidebar-user-form-close').click()
  162. }
  163. this.update()
  164. })
  165. },
  166. /**
  167. *
  168. *
  169. */
  170. reset() {
  171. this.state.user = { }
  172. this.update()
  173. }
  174. }
  175. </script>
  176. </app-users-form>