CreateCommunityStation.vue 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  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 station-id"
  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 "toasters";
  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. io.getSocket(socket => {
  59. this.socket = socket;
  60. });
  61. },
  62. methods: {
  63. submitModal() {
  64. this.newCommunity.name = this.newCommunity.name.toLowerCase();
  65. const { name, displayName, description } = this.newCommunity;
  66. if (!name || !displayName || !description)
  67. return new Toast({
  68. content: "Please fill in all fields",
  69. timeout: 8000
  70. });
  71. if (!validation.isLength(name, 2, 16))
  72. return new Toast({
  73. content: "Name must have between 2 and 16 characters.",
  74. timeout: 8000
  75. });
  76. if (!validation.regex.az09_.test(name))
  77. return new Toast({
  78. content:
  79. "Invalid name format. Allowed characters: a-z, 0-9 and _.",
  80. timeout: 8000
  81. });
  82. if (!validation.isLength(displayName, 2, 32))
  83. return new Toast({
  84. content:
  85. "Display name must have between 2 and 32 characters.",
  86. timeout: 8000
  87. });
  88. if (!validation.regex.ascii.test(displayName))
  89. return new Toast({
  90. content:
  91. "Invalid display name format. Only ASCII characters are allowed.",
  92. timeout: 8000
  93. });
  94. if (!validation.isLength(description, 2, 200))
  95. return new Toast({
  96. content:
  97. "Description must have between 2 and 200 characters.",
  98. timeout: 8000
  99. });
  100. let characters = description.split("");
  101. characters = characters.filter(character => {
  102. return character.charCodeAt(0) === 21328;
  103. });
  104. if (characters.length !== 0)
  105. return new Toast({
  106. content: "Invalid description format.",
  107. timeout: 8000
  108. });
  109. return this.socket.emit(
  110. "stations.create",
  111. {
  112. name,
  113. type: "community",
  114. displayName,
  115. description
  116. },
  117. res => {
  118. if (res.status === "success") {
  119. new Toast({
  120. content: `You have added the station successfully`,
  121. timeout: 4000
  122. });
  123. this.closeModal({
  124. sector: "home",
  125. modal: "createCommunityStation"
  126. });
  127. } else new Toast({ content: res.message, timeout: 4000 });
  128. }
  129. );
  130. },
  131. ...mapActions("modals", ["closeModal"])
  132. }
  133. };
  134. </script>
  135. <style lang="scss" scoped>
  136. .station-id {
  137. text-transform: lowercase;
  138. &::placeholder {
  139. text-transform: none;
  140. }
  141. }
  142. </style>