Stations.vue 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374
  1. <template>
  2. <div>
  3. <div class="container">
  4. <table class="table is-striped">
  5. <thead>
  6. <tr>
  7. <td>ID</td>
  8. <td>Name</td>
  9. <td>Type</td>
  10. <td>Display Name</td>
  11. <td>Description</td>
  12. <td>Owner</td>
  13. <td>Options</td>
  14. </tr>
  15. </thead>
  16. <tbody>
  17. <tr v-for="(station, index) in stations" :key="index">
  18. <td>
  19. <span>{{ station._id }}</span>
  20. </td>
  21. <td>
  22. <span>
  23. <router-link
  24. :to="{
  25. name: 'station',
  26. params: { id: station.name }
  27. }"
  28. >
  29. {{ station.name }}
  30. </router-link>
  31. </span>
  32. </td>
  33. <td>
  34. <span>{{ station.type }}</span>
  35. </td>
  36. <td>
  37. <span>{{ station.displayName }}</span>
  38. </td>
  39. <td>
  40. <span>{{ station.description }}</span>
  41. </td>
  42. <td>
  43. <span
  44. v-if="station.type === 'official'"
  45. title="Musare"
  46. >Musare</span
  47. >
  48. <user-id-to-username
  49. v-else
  50. :userId="station.owner"
  51. :link="true"
  52. />
  53. </td>
  54. <td>
  55. <a class="button is-info" v-on:click="edit(station)"
  56. >Edit</a
  57. >
  58. <a
  59. class="button is-danger"
  60. href="#"
  61. @click="removeStation(index)"
  62. >Remove</a
  63. >
  64. </td>
  65. </tr>
  66. </tbody>
  67. </table>
  68. </div>
  69. <div class="container">
  70. <div class="card is-fullwidth">
  71. <header class="card-header">
  72. <p class="card-header-title">
  73. Create official station
  74. </p>
  75. </header>
  76. <div class="card-content">
  77. <div class="content">
  78. <div class="control is-horizontal">
  79. <div class="control is-grouped">
  80. <p class="control is-expanded">
  81. <input
  82. v-model="newStation.name"
  83. class="input"
  84. type="text"
  85. placeholder="Name"
  86. />
  87. </p>
  88. <p class="control is-expanded">
  89. <input
  90. v-model="newStation.displayName"
  91. class="input"
  92. type="text"
  93. placeholder="Display Name"
  94. />
  95. </p>
  96. </div>
  97. </div>
  98. <label class="label">Description</label>
  99. <p class="control is-expanded">
  100. <input
  101. v-model="newStation.description"
  102. class="input"
  103. type="text"
  104. placeholder="Short description"
  105. />
  106. </p>
  107. <div class="control is-grouped genre-wrapper">
  108. <div class="sector">
  109. <p class="control has-addons">
  110. <input
  111. id="new-genre"
  112. class="input"
  113. type="text"
  114. placeholder="Genre"
  115. @keyup.enter="addGenre()"
  116. />
  117. <a
  118. class="button is-info"
  119. href="#"
  120. @click="addGenre()"
  121. >Add genre</a
  122. >
  123. </p>
  124. <span
  125. v-for="(genre, index) in newStation.genres"
  126. :key="index"
  127. class="tag is-info"
  128. >
  129. {{ genre }}
  130. <button
  131. class="delete is-info"
  132. @click="removeGenre(index)"
  133. />
  134. </span>
  135. </div>
  136. <div class="sector">
  137. <p class="control has-addons">
  138. <input
  139. id="new-blacklisted-genre"
  140. class="input"
  141. type="text"
  142. placeholder="Blacklisted Genre"
  143. @keyup.enter="addBlacklistedGenre()"
  144. />
  145. <a
  146. class="button is-info"
  147. href="#"
  148. @click="addBlacklistedGenre()"
  149. >Add blacklisted genre</a
  150. >
  151. </p>
  152. <span
  153. v-for="(genre,
  154. index) in newStation.blacklistedGenres"
  155. :key="index"
  156. class="tag is-info"
  157. >
  158. {{ genre }}
  159. <button
  160. class="delete is-info"
  161. @click="removeBlacklistedGenre(index)"
  162. />
  163. </span>
  164. </div>
  165. </div>
  166. </div>
  167. </div>
  168. <footer class="card-footer">
  169. <a
  170. class="card-footer-item"
  171. href="#"
  172. @click="createStation()"
  173. >Create</a
  174. >
  175. </footer>
  176. </div>
  177. </div>
  178. <edit-station v-if="modals.editStation" />
  179. </div>
  180. </template>
  181. <script>
  182. import { mapState, mapActions } from "vuex";
  183. import { Toast } from "vue-roaster";
  184. import io from "../../io";
  185. import EditStation from "./EditStation.vue";
  186. import UserIdToUsername from "../UserIdToUsername.vue";
  187. export default {
  188. components: { EditStation, UserIdToUsername },
  189. data() {
  190. return {
  191. stations: [],
  192. newStation: {
  193. genres: [],
  194. blacklistedGenres: []
  195. }
  196. };
  197. },
  198. computed: {
  199. ...mapState("modals", {
  200. modals: state => state.modals.station
  201. })
  202. },
  203. methods: {
  204. createStation() {
  205. const {
  206. newStation: {
  207. name,
  208. displayName,
  209. description,
  210. genres,
  211. blacklistedGenres
  212. }
  213. } = this;
  214. if (name === undefined)
  215. return Toast.methods.addToast(
  216. "Field (Name) cannot be empty",
  217. 3000
  218. );
  219. if (displayName === undefined)
  220. return Toast.methods.addToast(
  221. "Field (Display Name) cannot be empty",
  222. 3000
  223. );
  224. if (description === undefined)
  225. return Toast.methods.addToast(
  226. "Field (Description) cannot be empty",
  227. 3000
  228. );
  229. return this.socket.emit(
  230. "stations.create",
  231. {
  232. name,
  233. type: "official",
  234. displayName,
  235. description,
  236. genres,
  237. blacklistedGenres
  238. },
  239. result => {
  240. Toast.methods.addToast(result.message, 3000);
  241. if (result.status === "success")
  242. this.newStation = {
  243. genres: [],
  244. blacklistedGenres: []
  245. };
  246. }
  247. );
  248. },
  249. removeStation(index) {
  250. this.socket.emit(
  251. "stations.remove",
  252. this.stations[index]._id,
  253. res => {
  254. Toast.methods.addToast(res.message, 3000);
  255. }
  256. );
  257. },
  258. edit(station) {
  259. this.editStation({
  260. _id: station._id,
  261. name: station.name,
  262. type: station.type,
  263. partyMode: station.partyMode,
  264. description: station.description,
  265. privacy: station.privacy,
  266. displayName: station.displayName,
  267. genres: station.genres,
  268. blacklistedGenres: station.blacklistedGenres
  269. });
  270. this.openModal({
  271. sector: "station",
  272. modal: "editStation"
  273. });
  274. },
  275. addGenre() {
  276. const genre = document
  277. .getElementById(`new-genre`)
  278. .value.toLowerCase()
  279. .trim();
  280. if (this.newStation.genres.indexOf(genre) !== -1)
  281. return Toast.methods.addToast("Genre already exists", 3000);
  282. if (genre) {
  283. this.newStation.genres.push(genre);
  284. document.getElementById(`new-genre`).value = "";
  285. return true;
  286. }
  287. return Toast.methods.addToast("Genre cannot be empty", 3000);
  288. },
  289. removeGenre(index) {
  290. this.newStation.genres.splice(index, 1);
  291. },
  292. addBlacklistedGenre() {
  293. const genre = document
  294. .getElementById(`new-blacklisted-genre`)
  295. .value.toLowerCase()
  296. .trim();
  297. if (this.newStation.blacklistedGenres.indexOf(genre) !== -1)
  298. return Toast.methods.addToast("Genre already exists", 3000);
  299. if (genre) {
  300. this.newStation.blacklistedGenres.push(genre);
  301. document.getElementById(`new-blacklisted-genre`).value = "";
  302. return true;
  303. }
  304. return Toast.methods.addToast("Genre cannot be empty", 3000);
  305. },
  306. removeBlacklistedGenre(index) {
  307. this.newStation.blacklistedGenres.splice(index, 1);
  308. },
  309. init() {
  310. this.socket.emit("stations.index", data => {
  311. this.stations = data.stations;
  312. });
  313. this.socket.emit("apis.joinAdminRoom", "stations", () => {});
  314. },
  315. ...mapActions("modals", ["openModal"]),
  316. ...mapActions("admin/stations", ["editStation"])
  317. },
  318. mounted() {
  319. io.getSocket(socket => {
  320. this.socket = socket;
  321. if (this.socket.connected) this.init();
  322. this.socket.on("event:admin.station.added", station => {
  323. this.stations.push(station);
  324. });
  325. this.socket.on("event:admin.station.removed", stationId => {
  326. this.stations = this.stations.filter(station => {
  327. return station._id !== stationId;
  328. });
  329. });
  330. io.onConnect(() => {
  331. this.init();
  332. });
  333. });
  334. }
  335. };
  336. </script>
  337. <style lang="scss" scoped>
  338. @import "styles/global.scss";
  339. .tag {
  340. margin-top: 5px;
  341. &:not(:last-child) {
  342. margin-right: 5px;
  343. }
  344. }
  345. td {
  346. word-wrap: break-word;
  347. max-width: 10vw;
  348. vertical-align: middle;
  349. }
  350. .is-info:focus {
  351. background-color: $primary-color;
  352. }
  353. .genre-wrapper {
  354. display: flex;
  355. justify-content: space-around;
  356. }
  357. .card-footer-item {
  358. color: $primary-color;
  359. }
  360. </style>