CreateCommunityStation.vue 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. <template>
  2. <modal title="Create Community Station">
  3. <template #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 #footer>
  36. <a class="button is-primary" @click="submitModal()">Create</a>
  37. </template>
  38. </modal>
  39. </template>
  40. <script>
  41. import { mapGetters, mapActions } from "vuex";
  42. import Toast from "toasters";
  43. import validation from "@/validation";
  44. import Modal from "../Modal.vue";
  45. export default {
  46. components: { Modal },
  47. data() {
  48. return {
  49. newCommunity: {
  50. name: "",
  51. displayName: "",
  52. description: ""
  53. }
  54. };
  55. },
  56. computed: mapGetters({
  57. socket: "websockets/getSocket"
  58. }),
  59. methods: {
  60. submitModal() {
  61. this.newCommunity.name = this.newCommunity.name.toLowerCase();
  62. const { name, displayName, description } = this.newCommunity;
  63. if (!name || !displayName || !description)
  64. return new Toast("Please fill in all fields");
  65. if (!validation.isLength(name, 2, 16))
  66. return new Toast("Name must have between 2 and 16 characters.");
  67. if (!validation.regex.az09_.test(name))
  68. return new Toast(
  69. "Invalid name format. Allowed characters: a-z, 0-9 and _."
  70. );
  71. if (!validation.isLength(displayName, 2, 32))
  72. return new Toast(
  73. "Display name must have between 2 and 32 characters."
  74. );
  75. if (!validation.regex.ascii.test(displayName))
  76. return new Toast(
  77. "Invalid display name format. Only ASCII characters are allowed."
  78. );
  79. if (!validation.isLength(description, 2, 200))
  80. return new Toast(
  81. "Description must have between 2 and 200 characters."
  82. );
  83. let characters = description.split("");
  84. characters = characters.filter(character => {
  85. return character.charCodeAt(0) === 21328;
  86. });
  87. if (characters.length !== 0)
  88. return new Toast("Invalid description format.");
  89. return this.socket.dispatch(
  90. "stations.create",
  91. {
  92. name,
  93. type: "community",
  94. displayName,
  95. description
  96. },
  97. res => {
  98. if (res.status === "success") {
  99. new Toast(`You have added the station successfully`);
  100. this.closeModal({
  101. sector: "home",
  102. modal: "createCommunityStation"
  103. });
  104. } else new Toast(res.message);
  105. }
  106. );
  107. },
  108. ...mapActions("modalVisibility", ["closeModal"])
  109. }
  110. };
  111. </script>
  112. <style lang="scss" scoped>
  113. .station-id {
  114. text-transform: lowercase;
  115. &::placeholder {
  116. text-transform: none;
  117. }
  118. }
  119. </style>