2
0

CreateStation.vue 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  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. export default {
  48. props: {
  49. official: { type: Boolean, default: false }
  50. },
  51. data() {
  52. return {
  53. newStation: {
  54. name: "",
  55. displayName: "",
  56. description: ""
  57. }
  58. };
  59. },
  60. computed: mapGetters({
  61. socket: "websockets/getSocket"
  62. }),
  63. methods: {
  64. submitModal() {
  65. this.newStation.name = this.newStation.name.toLowerCase();
  66. const { name, displayName, description } = this.newStation;
  67. if (!name || !displayName || !description)
  68. return new Toast("Please fill in all fields");
  69. if (!validation.isLength(name, 2, 16))
  70. return new Toast("Name must have between 2 and 16 characters.");
  71. if (!validation.regex.az09_.test(name))
  72. return new Toast(
  73. "Invalid name format. Allowed characters: a-z, 0-9 and _."
  74. );
  75. if (!validation.isLength(displayName, 2, 32))
  76. return new Toast(
  77. "Display name must have between 2 and 32 characters."
  78. );
  79. if (!validation.regex.ascii.test(displayName))
  80. return new Toast(
  81. "Invalid display name format. Only ASCII characters are allowed."
  82. );
  83. if (!validation.isLength(description, 2, 200))
  84. return new Toast(
  85. "Description must have between 2 and 200 characters."
  86. );
  87. let characters = description.split("");
  88. characters = characters.filter(
  89. character => character.charCodeAt(0) === 21328
  90. );
  91. if (characters.length !== 0)
  92. return new Toast("Invalid description format.");
  93. return this.socket.dispatch(
  94. "stations.create",
  95. {
  96. name,
  97. type: this.official ? "official" : "community",
  98. displayName,
  99. description
  100. },
  101. res => {
  102. if (res.status === "success") {
  103. new Toast(`You have added the station successfully`);
  104. this.closeModal("createStation");
  105. } else new Toast(res.message);
  106. }
  107. );
  108. },
  109. ...mapActions("modalVisibility", ["closeModal"])
  110. }
  111. };
  112. </script>
  113. <style lang="less" scoped>
  114. .station-id {
  115. text-transform: lowercase;
  116. &::placeholder {
  117. text-transform: none;
  118. }
  119. }
  120. </style>