CreateCommunityStation.vue 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. <template>
  2. <modal title="Create Community Station">
  3. <template v-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
  8. v-model="newCommunity.name"
  9. class="input"
  10. type="text"
  11. placeholder="Name..."
  12. autofocus
  13. />
  14. </p>
  15. <label class="label">Display Name</label>
  16. <p class="control">
  17. <input
  18. v-model="newCommunity.displayName"
  19. class="input"
  20. type="text"
  21. placeholder="Display name..."
  22. />
  23. </p>
  24. <label class="label">Description</label>
  25. <p class="control">
  26. <input
  27. v-model="newCommunity.description"
  28. class="input"
  29. type="text"
  30. placeholder="Description..."
  31. @keyup.enter="submitModal()"
  32. />
  33. </p>
  34. </template>
  35. <template v-slot:footer>
  36. <a class="button is-primary" v-on:click="submitModal()">Create</a>
  37. </template>
  38. </modal>
  39. </template>
  40. <script>
  41. import { mapActions } from "vuex";
  42. import { Toast } from "vue-roaster";
  43. import Modal from "./Modal.vue";
  44. import io from "../../io";
  45. import validation from "../../validation";
  46. export default {
  47. components: { Modal },
  48. data() {
  49. return {
  50. newCommunity: {
  51. name: "",
  52. displayName: "",
  53. description: ""
  54. }
  55. };
  56. },
  57. mounted() {
  58. const _this = this;
  59. io.getSocket(socket => {
  60. _this.socket = socket;
  61. });
  62. },
  63. methods: {
  64. submitModal() {
  65. const { name, displayName, description } = this.newCommunity;
  66. if (!name || !displayName || !description)
  67. return Toast.methods.addToast(
  68. "Please fill in all fields",
  69. 8000
  70. );
  71. if (!validation.isLength(name, 2, 16))
  72. return Toast.methods.addToast(
  73. "Name must have between 2 and 16 characters.",
  74. 8000
  75. );
  76. if (!validation.regex.az09_.test(name))
  77. return Toast.methods.addToast(
  78. "Invalid name format. Allowed characters: a-z, 0-9 and _.",
  79. 8000
  80. );
  81. if (!validation.isLength(displayName, 2, 32))
  82. return Toast.methods.addToast(
  83. "Display name must have between 2 and 32 characters.",
  84. 8000
  85. );
  86. if (!validation.regex.azAZ09_.test(displayName))
  87. return Toast.methods.addToast(
  88. "Invalid display name format. Allowed characters: a-z, A-Z, 0-9 and _.",
  89. 8000
  90. );
  91. if (!validation.isLength(description, 2, 200))
  92. return Toast.methods.addToast(
  93. "Description must have between 2 and 200 characters.",
  94. 8000
  95. );
  96. let characters = description.split("");
  97. characters = characters.filter(character => {
  98. return character.charCodeAt(0) === 21328;
  99. });
  100. if (characters.length !== 0)
  101. return Toast.methods.addToast(
  102. "Invalid description format. Swastika's are not allowed.",
  103. 8000
  104. );
  105. const _this = this;
  106. return this.socket.emit(
  107. "stations.create",
  108. {
  109. name,
  110. type: "community",
  111. displayName,
  112. description
  113. },
  114. res => {
  115. if (res.status === "success") {
  116. Toast.methods.addToast(
  117. `You have added the station successfully`,
  118. 4000
  119. );
  120. _this.closeModal({
  121. sector: "home",
  122. modal: "createCommunityStation"
  123. });
  124. } else Toast.methods.addToast(res.message, 4000);
  125. }
  126. );
  127. },
  128. ...mapActions("modals", ["closeModal"])
  129. }
  130. };
  131. </script>