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.

225 lines
7.8 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
  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 m-bottom-7">
  38. <label class="field-label">
  39. <input name="roles[]" type="checkbox" class="field-choice" value="admin" />
  40. <svg class="icon field-choice__unchecked" aria-hidden="true">
  41. <use xlink:href="/symbol-defs.svg#icon-checkbox"></use>
  42. </svg>
  43. <svg class="icon field-choice__checked" aria-hidden="true">
  44. <use xlink:href="/symbol-defs.svg#icon-checkbox-checked"></use>
  45. </svg>
  46. Admin
  47. </label>
  48. <field-error name="roles"></field-error>
  49. </div>
  50. <div class="field-group">
  51. <label class="field-label">
  52. Password
  53. <input name="password" type="password" class="field-text" />
  54. </label>
  55. <field-error name="password"></field-error>
  56. </div>
  57. <div class="field-group">
  58. <label class="field-label">
  59. Password confirm
  60. <input name="password_confirm" type="password" class="field-text" />
  61. </label>
  62. <field-error name="password_confirm"></field-error>
  63. </div>
  64. </form>
  65. </div>
  66. <div class="sidebar__footer">
  67. <button class="button m-bottom-0" type="submit" form="app-users-form">
  68. Save
  69. <svg class="icon fill-success p-left-3" aria-hidden="true">
  70. <use xlink:href="/symbol-defs.svg#icon-check"></use>
  71. </svg>
  72. </button>
  73. <button class="button m-bottom-0" type="submit" form="app-users-form" close>
  74. Save and Close
  75. <svg class="icon fill-success p-left-3" aria-hidden="true">
  76. <use xlink:href="/symbol-defs.svg#icon-arrow-right"></use>
  77. </svg>
  78. </button>
  79. </div>
  80. <app-loading loading={ state.loading }></app-loading>
  81. </div>
  82. </div>
  83. <script>
  84. import axios from 'axios'
  85. import * as riot from 'riot'
  86. import FormValidator from './../../FormValidator'
  87. import FieldError from './../field-error.riot'
  88. riot.register('field-error', FieldError)
  89. riot.mount('field-error')
  90. export default {
  91. state: {
  92. user: {
  93. },
  94. loading: false
  95. },
  96. /**
  97. *
  98. *
  99. */
  100. onMounted(props, state) {
  101. this.root.addEventListener('app-users-form-open', (event) => {
  102. this.$('.sidebar').classList.add('sidebar--open')
  103. // check for data, and if user is send add to state
  104. if (event.detail.data) {
  105. this.state.user = event.detail.data
  106. this.state.formValidation.setConstraits({
  107. 'email': {
  108. 'length': {
  109. 'maximum': 255
  110. },
  111. 'email': true,
  112. 'presence': true
  113. }
  114. })
  115. this.update()
  116. }
  117. })
  118. // create form validation
  119. this.state.formValidation = new FormValidator('#app-users-form', {
  120. 'email': {
  121. 'length': {
  122. 'maximum': 255
  123. },
  124. 'email': true,
  125. 'presence': true
  126. },
  127. 'password': {
  128. 'length': {
  129. 'maximum': 64
  130. },
  131. 'presence': true
  132. },
  133. 'password_confirm': {
  134. 'equality': 'password'
  135. }
  136. }, (event, data) => {
  137. this.handleSubmit(event, data)
  138. })
  139. },
  140. /**
  141. * close current form
  142. *
  143. *
  144. * @param {object} event
  145. *
  146. */
  147. handleClose(event)
  148. {
  149. event.preventDefault()
  150. this.$('.sidebar').classList.remove('sidebar--open')
  151. this.reset()
  152. },
  153. /**
  154. *
  155. *
  156. */
  157. handleSubmit(event, data)
  158. {
  159. let method = 'post'
  160. let url = '/api/users/v1'
  161. // user is set and has id, send as put with id
  162. if (this.state.user && this.state.user._id) {
  163. method = 'put'
  164. url += '/' + this.state.user._id
  165. }
  166. this.state.loading = true
  167. this.update()
  168. axios({
  169. method: method,
  170. url: url,
  171. data: data
  172. }).then((response) => {
  173. this.state.user = response.data.data
  174. // check if submit has close-attribute
  175. if (event.submitter.attributes.close) {
  176. this.$('#sidebar-user-form-close').click()
  177. }
  178. }).catch((error) => {
  179. if (error.response.status === 422) {
  180. }
  181. }).then(() => {
  182. this.state.loading = false
  183. this.update()
  184. })
  185. },
  186. /**
  187. *
  188. *
  189. */
  190. reset() {
  191. this.state.user = { }
  192. this.update()
  193. }
  194. }
  195. </script>
  196. </app-users-form>