Advertisement
clga29261

Recordlist.vue

May 28th, 2017
112
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.59 KB | None | 0 0
  1. <template>
  2.  
  3. <div class="container contents">
  4. <div className="row">
  5. <div className="col-md-12">
  6. <h2>API Connector</h2>
  7. <button id="add-new" type="button" class="btn btn-success pull-right" @click="openNewModal">
  8. Add Connector
  9. </button>
  10. <div className="panel with-nav-tabs panel-danger">
  11. <table class="table table-bordered">
  12. <thead>
  13. <tr>
  14. <th>URL</th>
  15. <th>API Key</th>
  16. <th>Rollbase Username</th>
  17. <th>Rollbase Password</th>
  18. <th>Action</th>
  19. </tr>
  20. </thead>
  21. <tbody>
  22. <tr v-for="connector in connectors" :key="connector['.key']">
  23. <td> <a v-bind:href="connector.url">{{ connector.url | snippet }}</a> </td>
  24. <td> {{ connector.apikey | snippet }} </td>
  25. <td> {{ connector.username }} </td>
  26. <td> {{ connector.password }} </td>
  27. <td>
  28. <center>
  29. <button class="btn btn-primary btn-xs" data-title="Edit" data-toggle="modal" data-target="#edit" ><span class="glyphicon glyphicon-pencil"></span></button>
  30. <button class="btn btn-danger btn-xs" data-title="Delete" data-toggle="modal" data-target="#delete" v-on:click="removeBook(connector.id)"><span class="glyphicon glyphicon-trash"></span></button>
  31. </center>
  32. </td>
  33. </tr>
  34. </tbody>
  35. </table>
  36. </div>
  37. </div>
  38. </div>
  39.  
  40. <div class="modal fade" id="add-modal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
  41. <div class="modal-dialog">
  42. <div class="modal-content">
  43. <div class="modal-header">
  44. <button type="button" class="close" data-dismiss="modal">
  45. <span aria-hidden="true">&times;</span>
  46. <span class="sr-only">Close</span>
  47. </button>
  48. <h4 class="modal-title" id="myModalLabel">
  49. Add New API Connector
  50. </h4>
  51. </div>
  52.  
  53. <div class="modal-body" :class="{ 'error' : hasErrors }">
  54. <form class="form-horizontal" role="form">
  55. <div class="form-group">
  56. <label class="col-sm-2 control-label" accesskey=""for="inputEmail3">URL</label>
  57. <div class="col-sm-10">
  58. <input type="text" v-model="url" class="form-control" id="url" placeholder="URL"/>
  59. </div>
  60. </div>
  61.  
  62. <div class="form-group">
  63. <label class="col-sm-2 control-label" accesskey=""for="inputEmail3">Username</label>
  64. <div class="col-sm-10">
  65. <input type="text" v-model="username" class="form-control" id="username" placeholder="Email"/>
  66. </div>
  67. </div>
  68.  
  69. <div class="form-group">
  70. <label class="col-sm-2 control-label" for="inputPassword3" >Password</label>
  71. <div class="col-sm-10">
  72. <input type="text" v-model="password" class="form-control" id="password" placeholder="Password"/>
  73. </div>
  74. </div>
  75. </form>
  76. </div>
  77. <div class="alert alert-danger" v-if="hasErrors">
  78. <p v-for="error in errors">
  79. {{ error }}
  80. </p>
  81. </div>
  82. <div class="modal-footer">
  83. <button type="button" class="btn btn-default" data-dismiss="modal">
  84. Cancel
  85. </button>
  86. <button type="button" class="btn btn-primary" @click="addConnector">
  87. Save Connector
  88. </button>
  89. </div>
  90. </div>
  91. </div>
  92. </div>
  93.  
  94. </div>
  95.  
  96. </template>
  97.  
  98. <script>
  99.  
  100. export default {
  101. name: 'recordList',
  102. data () {
  103. return {
  104. connectors: [],
  105. url: '',
  106. apikey: '',
  107. username: '',
  108. password: '',
  109. connectorsRef: firebase.database().ref('connectors'),
  110. errors: []
  111. }
  112. },
  113. computed: {
  114. hasErrors () {
  115. return this.errors.length > 0
  116. }
  117. },
  118. mounted () {
  119. this.viewConnectors();
  120. },
  121. filters: {
  122. truncate: function(string, value) {
  123. return string.substring(0, value) + '...';
  124. }
  125. },
  126. methods: {
  127. openNewModal () {
  128. $('#add-modal').modal('show')
  129. },
  130. addConnector () {
  131. this.errors = []
  132.  
  133. if ( this.isFormValid() ) {
  134. let key = this.connectorsRef.push().key
  135. this.apikey = this.generateHaskKey()
  136. let newConnector = { id : key, url : this.url, apikey : this.apikey, username : this.username, password : this.password }
  137.  
  138. this.connectorsRef.child(key).update(newConnector).then( () => {
  139. this.url = ''
  140. this.apikey = ''
  141. this.username = ''
  142. this.password = ''
  143. $('#add-modal').modal('hide')
  144. }).catch( error => {
  145. this.errors.push(error.message)
  146. })
  147. }
  148.  
  149. },
  150. generateHaskKey () {
  151. var text = "";
  152. var possible = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
  153.  
  154. for( var i=0; i < 100; i++ )
  155. text += possible.charAt(Math.floor(Math.random() * possible.length));
  156.  
  157. return text;
  158. },
  159. removeBook: function (connector) {
  160. this.connectorsRef.child(connector).remove()
  161. console.table(this.connectors);
  162. },
  163. viewConnectors () {
  164. this.connectorsRef.on('child_added', snap => {
  165. this.connectors.push(snap.val())
  166. })
  167. },
  168. isEmpty () {
  169. if ( this.url.length == 0 || this.username.length == 0 || this.password.length == 0 ) {
  170. return true;
  171. }
  172. return false;
  173. },
  174. passMatch () {
  175. if ( this.password.length < 6 ) {
  176. return false;
  177. }
  178. return true;
  179. },
  180. isFormValid () {
  181. if ( this.isEmpty() ) {
  182. this.errors.push('Must not be empty');
  183. return false;
  184. }
  185. if ( !this.passMatch() ) {
  186. this.errors.push('Not less than 6 characters');
  187. return false;
  188. }
  189. return true;
  190. }
  191. }
  192. }
  193.  
  194. </script>
  195.  
  196.  
  197. <style scoped>
  198. .contents {
  199. margin-top: 5rem;
  200. }
  201. table {
  202. width: 100%
  203. }
  204. th {
  205. text-align: center;
  206. }
  207. #add-new {
  208. margin-bottom: 10px
  209. }
  210. </style>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement