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.

214 lines
7.3 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
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. <app-loading loading={ state.loading }></app-loading>
  74. </div>
  75. </div>
  76. <script>
  77. import axios from 'axios'
  78. import * as riot from 'riot'
  79. import FormValidator from './../../FormValidator'
  80. import FieldError from './../field-error.riot'
  81. import AppLoading from './../loading.riot'
  82. riot.register('field-error', FieldError)
  83. riot.mount('field-error')
  84. riot.register('app-loading', AppLoading)
  85. riot.mount('app-loading')
  86. export default {
  87. state: {
  88. user: {
  89. },
  90. loading: false
  91. },
  92. /**
  93. *
  94. *
  95. */
  96. onMounted(props, state) {
  97. this.root.addEventListener('app-users-form-open', (event) => {
  98. this.$('.sidebar').classList.add('sidebar--open')
  99. // check for data, and if user is send add to state
  100. if (event.detail.data) {
  101. this.state.user = event.detail.data
  102. this.state.formValidation.setConstraits({
  103. 'email': {
  104. 'length': {
  105. 'maximum': 255
  106. },
  107. 'email': true,
  108. 'presence': true
  109. }
  110. })
  111. this.update()
  112. }
  113. })
  114. // create form validation
  115. this.state.formValidation = new FormValidator('#app-users-form', {
  116. 'email': {
  117. 'length': {
  118. 'maximum': 255
  119. },
  120. 'email': true,
  121. 'presence': true
  122. },
  123. 'password': {
  124. 'length': {
  125. 'maximum': 64
  126. },
  127. 'presence': true
  128. }
  129. }, (event, data) => {
  130. this.handleSubmit(event, data)
  131. })
  132. },
  133. /**
  134. * close current form
  135. *
  136. *
  137. * @param {object} event
  138. *
  139. */
  140. handleClose(event)
  141. {
  142. event.preventDefault()
  143. this.$('.sidebar').classList.remove('sidebar--open')
  144. this.reset()
  145. },
  146. /**
  147. *
  148. *
  149. */
  150. handleSubmit(event, data) {
  151. let method = 'post'
  152. let url = '/api/users'
  153. // user is set and has id, send as put with id
  154. if (this.state.user && this.state.user._id) {
  155. method = 'put'
  156. url += '/' + this.state.user._id
  157. }
  158. this.state.loading = true
  159. this.update()
  160. axios({
  161. method: method,
  162. url: url,
  163. data: data
  164. }).then((response) => {
  165. this.state.user = response.data.data
  166. // check if submit has close-attribute
  167. if (event.submitter.attributes.close) {
  168. this.$('#sidebar-user-form-close').click()
  169. }
  170. this.state.loading = false
  171. this.update()
  172. })
  173. },
  174. /**
  175. *
  176. *
  177. */
  178. reset() {
  179. this.state.user = { }
  180. this.update()
  181. }
  182. }
  183. </script>
  184. </app-users-form>