CreateStation.vue 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. <template>
  2. <modal
  3. :title="
  4. official ? 'Create Official Station' : 'Create Community Station'
  5. "
  6. >
  7. <template #body>
  8. <label class="label">Name (unique lowercase station id)</label>
  9. <p class="control">
  10. <input
  11. v-model="newStation.name"
  12. class="input station-id"
  13. type="text"
  14. placeholder="Name..."
  15. autofocus
  16. />
  17. </p>
  18. <label class="label">Display Name</label>
  19. <p class="control">
  20. <input
  21. v-model="newStation.displayName"
  22. class="input"
  23. type="text"
  24. placeholder="Display name..."
  25. />
  26. </p>
  27. <label class="label">Description</label>
  28. <p class="control">
  29. <input
  30. v-model="newStation.description"
  31. class="input"
  32. type="text"
  33. placeholder="Description..."
  34. @keyup.enter="submitModal()"
  35. />
  36. </p>
  37. </template>
  38. <template #footer>
  39. <a class="button is-primary" @click="submitModal()">Create</a>
  40. </template>
  41. </modal>
  42. </template>
  43. <script>
  44. import { mapGetters, mapActions } from "vuex";
  45. import Toast from "toasters";
  46. import validation from "@/validation";
  47. import Modal from "../Modal.vue";
  48. export default {
  49. components: { Modal },
  50. props: {
  51. official: { type: Boolean, default: false }
  52. },
  53. data() {
  54. return {
  55. newStation: {
  56. name: "",
  57. displayName: "",
  58. description: ""
  59. }
  60. };
  61. },
  62. computed: mapGetters({
  63. socket: "websockets/getSocket"
  64. }),
  65. methods: {
  66. submitModal() {
  67. this.newStation.name = this.newStation.name.toLowerCase();
  68. const { name, displayName, description } = this.newStation;
  69. if (!name || !displayName || !description)
  70. return new Toast("Please fill in all fields");
  71. if (!validation.isLength(name, 2, 16))
  72. return new Toast("Name must have between 2 and 16 characters.");
  73. if (!validation.regex.az09_.test(name))
  74. return new Toast(
  75. "Invalid name format. Allowed characters: a-z, 0-9 and _."
  76. );
  77. if (!validation.isLength(displayName, 2, 32))
  78. return new Toast(
  79. "Display name must have between 2 and 32 characters."
  80. );
  81. if (!validation.regex.ascii.test(displayName))
  82. return new Toast(
  83. "Invalid display name format. Only ASCII characters are allowed."
  84. );
  85. if (!validation.isLength(description, 2, 200))
  86. return new Toast(
  87. "Description must have between 2 and 200 characters."
  88. );
  89. let characters = description.split("");
  90. characters = characters.filter(
  91. character => character.charCodeAt(0) === 21328
  92. );
  93. if (characters.length !== 0)
  94. return new Toast("Invalid description format.");
  95. return this.socket.dispatch(
  96. "stations.create",
  97. {
  98. name,
  99. type: this.official ? "official" : "community",
  100. displayName,
  101. description
  102. },
  103. res => {
  104. if (res.status === "success") {
  105. new Toast(`You have added the station successfully`);
  106. this.closeModal("createStation");
  107. } else new Toast(res.message);
  108. }
  109. );
  110. },
  111. ...mapActions("modalVisibility", ["closeModal"])
  112. }
  113. };
  114. </script>
  115. <style lang="less" scoped>
  116. .station-id {
  117. text-transform: lowercase;
  118. &::placeholder {
  119. text-transform: none;
  120. }
  121. }
  122. </style>