CreateCommunityStation.vue 3.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. <template>
  2. <modal title='Create Community Station'>
  3. <div slot='body'>
  4. <!-- validation to check if exists http://bulma.io/documentation/elements/form/ -->
  5. <label class='label'>Name (unique lowercase station id)</label>
  6. <p class='control'>
  7. <input class='input' type='text' placeholder='Name...' v-model='newCommunity.name' autofocus>
  8. </p>
  9. <label class='label'>Display Name</label>
  10. <p class='control'>
  11. <input class='input' type='text' placeholder='Display name...' v-model='newCommunity.displayName'>
  12. </p>
  13. <label class='label'>Description</label>
  14. <p class='control'>
  15. <input class='input' type='text' placeholder='Description...' v-model='newCommunity.description' @keyup.enter="submitModal()">
  16. </p>
  17. </div>
  18. <div slot='footer'>
  19. <a class='button is-primary' @click='submitModal()'>Create</a>
  20. </div>
  21. </modal>
  22. </template>
  23. <script>
  24. import { Toast } from 'vue-roaster';
  25. import Modal from './Modal.vue';
  26. import io from '../../io';
  27. import validation from '../../validation';
  28. export default {
  29. components: { Modal },
  30. data() {
  31. return {
  32. newCommunity: {
  33. name: '',
  34. displayName: '',
  35. description: ''
  36. }
  37. }
  38. },
  39. ready: function () {
  40. let _this = this;
  41. io.getSocket((socket) => {
  42. _this.socket = socket;
  43. });
  44. },
  45. methods: {
  46. toggleModal: function () {
  47. this.$parent.modals.createCommunityStation = !this.$parent.modals.createCommunityStation;
  48. },
  49. submitModal: function () {
  50. const name = this.newCommunity.name;
  51. const displayName = this.newCommunity.displayName;
  52. const description = this.newCommunity.description;
  53. if (!name || !displayName || !description) return Toast.methods.addToast('Please fill in all fields', 8000);
  54. if (!validation.isLength(name, 2, 16)) return Toast.methods.addToast('Name must have between 2 and 16 characters.', 8000);
  55. if (!validation.regex.az09_.test(name)) return Toast.methods.addToast('Invalid name format. Allowed characters: a-z, 0-9 and _.', 8000);
  56. if (!validation.isLength(displayName, 2, 32)) return Toast.methods.addToast('Display name must have between 2 and 32 characters.', 8000);
  57. if (!validation.regex.azAZ09_.test(displayName)) return Toast.methods.addToast('Invalid display name format. Allowed characters: a-z, A-Z, 0-9 and _.', 8000);
  58. if (!validation.isLength(description, 2, 200)) return Toast.methods.addToast('Description must have between 2 and 200 characters.', 8000);
  59. let characters = description.split("");
  60. characters = characters.filter(function(character) {
  61. return character.charCodeAt(0) === 21328;
  62. });
  63. if (characters.length !== 0) return Toast.methods.addToast('Invalid description format. Swastika\'s are not allowed.', 8000);
  64. this.socket.emit('stations.create', {
  65. name: name,
  66. type: 'community',
  67. displayName: displayName,
  68. description: description
  69. }, res => {
  70. if (res.status === 'success') Toast.methods.addToast(`You have added the station successfully`, 4000);
  71. else Toast.methods.addToast(res.message, 4000);
  72. });
  73. this.toggleModal();
  74. }
  75. },
  76. events: {
  77. closeModal: function() {
  78. this.$parent.modals.createCommunityStation = !this.$parent.modals.createCommunityStation;
  79. }
  80. }
  81. }
  82. </script>